Skip to content

Commit

Permalink
toEthereumAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
jshuo committed Jun 25, 2024
1 parent f68988f commit deaa2a7
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 41 deletions.
9 changes: 1 addition & 8 deletions lib/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var bytes = require('@ethersproject/bytes');
var basex = require('@ethersproject/basex');
var strings = require('@ethersproject/strings');
var ethrDidResolver = require('ethr-did-resolver');
var crypto = require('crypto');
var elliptic = require('elliptic');

function _interopNamespace(e) {
Expand All @@ -28,12 +27,6 @@ function _interopNamespace(e) {

var base64__namespace = /*#__PURE__*/_interopNamespace(base64);

// Function to compute Ethereum address from public key
function computeAddress(publicKey) {
const hash = crypto.createHash('sha256').update(publicKey.slice(2), 'hex').digest();
const address = crypto.createHash('rmd160').update(hash).digest('hex');
return `0x${address}`;
}
exports.DelegateTypes = void 0;
(function (DelegateTypes) {
DelegateTypes["veriKey"] = "veriKey";
Expand Down Expand Up @@ -87,7 +80,7 @@ class EthrDID {
const privateKey = '736f625c9dda78a94bb16840c82779bb7bc18014b8ede52f0f03429902fc4ba8';
const key = ec.keyFromPrivate(privateKey);
const publicKey = key.getPublic().encode("hex", false);
const address = computeAddress(publicKey);
const address = didJwt.toEthereumAddress(publicKey);
const net = typeof chainNameOrId === 'number' ? bytes.hexValue(chainNameOrId) : chainNameOrId;
const identifier = net ? `did:ethr:${net}:${publicKey}` : publicKey;
return {
Expand Down
2 changes: 1 addition & 1 deletion lib/index.cjs.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 2 additions & 9 deletions lib/index.module.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/index.module.js.map

Large diffs are not rendered by default.

16 changes: 5 additions & 11 deletions lib/index.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/index.umd.js.map

Large diffs are not rendered by default.

11 changes: 2 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createJWT, ES256KSigner, ES256Signer, hexToBytes, JWTVerified, Signer as JWTSigner, verifyJWT } from 'did-jwt'
import { createJWT, ES256KSigner, ES256Signer, hexToBytes, JWTVerified, Signer as JWTSigner, verifyJWT, toEthereumAddress } from 'did-jwt'
import { Signer as TxSigner } from '@ethersproject/abstract-signer'
import { CallOverrides } from '@ethersproject/contracts'
import { Provider } from '@ethersproject/providers'
Expand All @@ -9,14 +9,7 @@ import { Base58 } from '@ethersproject/basex'
import { toUtf8Bytes } from '@ethersproject/strings'
import { EthrDidController, interpretIdentifier, MetaSignature, REGISTRY } from 'ethr-did-resolver'
import { Resolvable } from 'did-resolver'
import { createHash } from 'crypto';
import { ec as EC } from 'elliptic';
// Function to compute Ethereum address from public key
function computeAddress(publicKey: string): string {
const hash = createHash('sha256').update(publicKey.slice(2), 'hex').digest();
const address = createHash('rmd160').update(hash).digest('hex');
return `0x${address}`;
}

export enum DelegateTypes {
veriKey = 'veriKey',
Expand Down Expand Up @@ -110,7 +103,7 @@ export class EthrDID {
'736f625c9dda78a94bb16840c82779bb7bc18014b8ede52f0f03429902fc4ba8';
const key = ec.keyFromPrivate(privateKey);
const publicKey = key.getPublic().encode("hex", false);
const address = computeAddress(publicKey)
const address = toEthereumAddress(publicKey)
const net = typeof chainNameOrId === 'number' ? hexValue(chainNameOrId) : chainNameOrId
const identifier = net ? `did:ethr:${net}:${publicKey}` : publicKey
return { address, privateKey, publicKey, identifier }
Expand Down

0 comments on commit deaa2a7

Please sign in to comment.