Skip to content

Commit adb1b08

Browse files
committed
update tests
1 parent 9f572c8 commit adb1b08

File tree

4 files changed

+34
-17
lines changed

4 files changed

+34
-17
lines changed

src/identity/identity-wallet.ts

+2-14
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ import {
77
ClaimOptions,
88
DID,
99
DidMethod,
10-
genesisFromEthAddress,
1110
getUnixTimestamp,
1211
Id,
1312
NetworkId,
1413
SchemaHash
1514
} from '@iden3/js-iden3-core';
1615
import { poseidon, PublicKey, sha256, Signature, Hex, getRandomBytes } from '@iden3/js-crypto';
1716
import { Hash, hashElems, ZERO_HASH } from '@iden3/js-merkletree';
18-
1917
import { generateProfileDID, subjectPositionIndex } from './common';
2018
import * as uuid from 'uuid';
2119
import { JSONSchema, JsonSchemaValidator, cacheLoader } from '../schema-processor';
@@ -44,15 +42,14 @@ import {
4442
TreesModel
4543
} from '../credentials';
4644
import { TreeState } from '../circuits';
47-
import { byteEncoder } from '../utils';
45+
import { buildDIDFromEthPubKey, byteEncoder } from '../utils';
4846
import { Options } from '@iden3/js-jsonld-merklization';
4947
import { TransactionReceipt } from 'ethers';
5048
import {
5149
CredentialStatusPublisherRegistry,
5250
Iden3SmtRhsCredentialStatusPublisher
5351
} from '../credentials/status/credential-status-publisher';
5452
import { ProofService } from '../proof';
55-
import { keccak256 } from 'js-sha3';
5653

5754
/**
5855
* DID creation options
@@ -636,16 +633,7 @@ export class IdentityWallet implements IIdentityWallet {
636633

637634
const keyIdEth = await this._kms.createKeyFromSeed(KmsKeyType.Secp256k1, opts.seed);
638635
const pubKeyHexEth = (await this._kms.publicKey(keyIdEth)).slice(2); // 04 + x + y (uncompressed key)
639-
// Use Keccak-256 hash function to get public key hash
640-
const hashOfPublicKey = keccak256(Buffer.from(pubKeyHexEth, 'hex'));
641-
// Convert hash to buffer
642-
const ethAddressBuffer = Buffer.from(hashOfPublicKey, 'hex');
643-
// Ethereum Address is '0x' concatenated with last 20 bytes
644-
// of the public key hash
645-
const ethAddr = ethAddressBuffer.slice(-20);
646-
const genesis = genesisFromEthAddress(ethAddr);
647-
const identifier = new Id(didType, genesis);
648-
const did = DID.parseFromId(identifier);
636+
const did = buildDIDFromEthPubKey(didType, pubKeyHexEth);
649637

650638
await this._storage.mt.createIdentityMerkleTrees(did.string());
651639

src/storage/blockchain/state.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { RootInfo, StateProof } from './../entities/state';
22
import { ZKProof } from '@iden3/js-jwz';
33
import { IStateStorage, UserStateTransitionInfo } from '../interfaces/state';
4-
import { Contract, ContractTransaction, JsonRpcProvider, Signer, TransactionRequest } from 'ethers';
4+
import { Contract, JsonRpcProvider, Signer, TransactionRequest } from 'ethers';
55
import { StateInfo } from '../entities/state';
66
import { StateTransitionPubSignals } from '../../circuits';
77
import { byteEncoder } from '../../utils';
@@ -165,7 +165,8 @@ export class EthStateStorage implements IStateStorage {
165165
signer: Signer,
166166
userStateTranstionInfo: UserStateTransitionInfo
167167
): Promise<string> {
168-
const { userId, oldUserState, newUserState, isOldStateGenesis, methodId, methodParams } = userStateTranstionInfo;
168+
const { userId, oldUserState, newUserState, isOldStateGenesis, methodId, methodParams } =
169+
userStateTranstionInfo;
169170
const { stateContract, provider } = this.getStateContractAndProviderForId(userId.bigInt());
170171
const contract = stateContract.connect(signer) as Contract;
171172
const feeData = await provider.getFeeData();

src/utils/did-helper.ts

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Hex } from '@iden3/js-crypto';
22
import { Id, buildDIDType, genesisFromEthAddress, DID } from '@iden3/js-iden3-core';
33
import { Hash } from '@iden3/js-merkletree';
44
import { DIDResolutionResult, VerificationMethod } from 'did-resolver';
5+
import { keccak256 } from 'js-sha3';
56

67
/**
78
* Checks if state is genesis state
@@ -83,3 +84,16 @@ export const resolveDIDDocumentAuth = async (
8384
(i) => i.type === 'Iden3StateInfo2023'
8485
);
8586
};
87+
88+
export const buildDIDFromEthPubKey = (didType: Uint8Array, pubKeyEth: string): DID => {
89+
// Use Keccak-256 hash function to get public key hash
90+
const hashOfPublicKey = keccak256(Buffer.from(pubKeyEth, 'hex'));
91+
// Convert hash to buffer
92+
const ethAddressBuffer = Buffer.from(hashOfPublicKey, 'hex');
93+
// Ethereum Address is '0x' concatenated with last 20 bytes
94+
// of the public key hash
95+
const ethAddr = ethAddressBuffer.slice(-20);
96+
const genesis = genesisFromEthAddress(ethAddr);
97+
const identifier = new Id(didType, genesis);
98+
return DID.parseFromId(identifier);
99+
};

tests/utils/utils.test.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from 'chai';
2-
import { JSONObject, mergeObjects } from '../../src';
2+
import { buildDIDFromEthPubKey, JSONObject, mergeObjects } from '../../src';
3+
import { Blockchain, buildDIDType, DidMethod, NetworkId } from '@iden3/js-iden3-core';
34

45
describe('merge credential subjects to create query', () => {
56
it('should merge two valid JSONObjects correctly', () => {
@@ -149,3 +150,16 @@ describe('merge credential subjects to create query', () => {
149150
}
150151
});
151152
});
153+
154+
describe('build did from ethereum public key', () => {
155+
it.only('should build did from ethereum public key correctly', async () => {
156+
const pubKeyHexEth =
157+
'8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed753547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5';
158+
const didType = buildDIDType(DidMethod.Iden3, Blockchain.Polygon, NetworkId.Amoy);
159+
const did = buildDIDFromEthPubKey(didType, pubKeyHexEth);
160+
161+
expect(did.string()).to.equal(
162+
'did:iden3:polygon:amoy:x6x5sor7zpycB7z7Q9348dXJxZ9s5b9AgmPeSccZz'
163+
);
164+
});
165+
});

0 commit comments

Comments
 (0)