Skip to content

Commit d78c3b5

Browse files
Merge pull request #247 from 0xPolygonID/unit/EcdsaSecp256k1RecoveryMethod2020-unit-test
add unit test for EcdsaSecp256k1RecoveryMethod2020 verification method
2 parents 8b89a30 + 36be227 commit d78c3b5

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

tests/iden3comm/jws.test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import {
99
byteEncoder,
1010
bytesToBase64url,
1111
hexToBytes,
12+
isEthereumIdentity,
1213
keyPath
1314
} from '../../src';
1415
import { expect } from 'chai';
1516
import { DIDResolutionResult } from 'did-resolver';
1617
import { ES256KSigner } from 'did-jwt';
18+
import { DID, getChainId, Id } from '@iden3/js-iden3-core';
19+
import { Hex } from '@iden3/js-crypto';
1720

1821
const didExample = {
1922
'@context': [
@@ -52,6 +55,23 @@ const didExample = {
5255
authentication: ['did:example:123#JUvpllMEYUZ2joO59UNui_XYDqxVqiFLLAJ8klWuPBw']
5356
};
5457

58+
const didExampleRecovery = {
59+
'@context': [
60+
'https://www.w3.org/ns/did/v1',
61+
'https://w3id.org/security/suites/secp256k1recovery-2020/v2'
62+
],
63+
id: 'did:iden3:privado:main:2SZDsdYordSH49VhS6hGo164RLwfcQe9FGow5ftSUG',
64+
verificationMethod: [
65+
{
66+
id: 'did:iden3:privado:main:2SZDsdYordSH49VhS6hGo164RLwfcQe9FGow5ftSUG#vm-1',
67+
controller: 'did:iden3:privado:main:2SZDsdYordSH49VhS6hGo164RLwfcQe9FGow5ftSUG',
68+
type: 'EcdsaSecp256k1RecoveryMethod2020',
69+
blockchainAccountId: 'eip155:21000:0x964e496a1b2541ed029abd5e49fd01e41cd02995'
70+
}
71+
],
72+
authentication: ['did:iden3:privado:main:2SZDsdYordSH49VhS6hGo164RLwfcQe9FGow5ftSUG#vm-1']
73+
};
74+
5575
describe('jws packer tests', () => {
5676
const did = 'did:example:123';
5777
let kms: KMS;
@@ -93,6 +113,61 @@ describe('jws packer tests', () => {
93113
expect(data).to.not.be.undefined;
94114
});
95115

116+
it('pack / unpack: EcdsaSecp256k1RecoveryMethod2020 verification method', async () => {
117+
const recoveryDIDDocument = {
118+
resolve: () => Promise.resolve({ didDocument: didExampleRecovery } as DIDResolutionResult)
119+
};
120+
const recoveryPacker = new JWSPacker(kms, recoveryDIDDocument);
121+
const ethDidString = 'did:iden3:privado:main:2SZDsdYordSH49VhS6hGo164RLwfcQe9FGow5ftSUG';
122+
const ethDidPk = '7365656473656564656565657365656473656564736565647365656475736572';
123+
const ethDid = DID.parse(ethDidString);
124+
if (isEthereumIdentity(ethDid)) {
125+
const bodyMsgStrRecovery = JSON.parse(bodyMsgStr);
126+
bodyMsgStrRecovery.from = ethDidString;
127+
const msgBytes = byteEncoder.encode(JSON.stringify(bodyMsgStrRecovery));
128+
129+
const signer: SignerFn = async (_, data) => {
130+
const signatureBase64 = await ES256KSigner(hexToBytes(ethDidPk), false)(data);
131+
return base64UrlToBytes(signatureBase64.toString());
132+
};
133+
134+
const tokenBytes = await recoveryPacker.pack(msgBytes, {
135+
alg: 'ES256K',
136+
did: ethDidString,
137+
issuer: did,
138+
signer
139+
});
140+
141+
const data = await recoveryPacker.unpack(tokenBytes);
142+
expect(data).to.not.be.undefined;
143+
} else {
144+
throw new Error('Ethereum identity expected');
145+
}
146+
});
147+
148+
it('returns EcdsaSecp256k1RecoveryMethod2020 VM from eth identity', async () => {
149+
const ethDidString = 'did:iden3:privado:main:2SZDsdYordSH49VhS6hGo164RLwfcQe9FGow5ftSUG';
150+
const ethDid = DID.parse(ethDidString);
151+
if (isEthereumIdentity(ethDid)) {
152+
const id = DID.idFromDID(ethDid);
153+
const chainId = getChainId(DID.blockchainFromId(id), DID.networkIdFromId(id));
154+
const address = Hex.encodeString(Id.ethAddressFromId(id));
155+
const vms = [
156+
{
157+
id: `${ethDidString}#vm-1`,
158+
controller: ethDidString,
159+
type: 'EcdsaSecp256k1RecoveryMethod2020',
160+
blockchainAccountId: `eip155:${chainId}:0x${address}`
161+
}
162+
];
163+
expect(vms[0].blockchainAccountId).to.be.eq(
164+
didExampleRecovery.verificationMethod[0].blockchainAccountId
165+
);
166+
} else {
167+
throw new Error('Ethereum identity expected');
168+
}
169+
});
170+
96171
it('pack / unpack: no kid', async () => {
97172
const msgBytes = byteEncoder.encode(bodyMsgStr);
98173

0 commit comments

Comments
 (0)