@@ -9,11 +9,14 @@ import {
9
9
byteEncoder ,
10
10
bytesToBase64url ,
11
11
hexToBytes ,
12
+ isEthereumIdentity ,
12
13
keyPath
13
14
} from '../../src' ;
14
15
import { expect } from 'chai' ;
15
16
import { DIDResolutionResult } from 'did-resolver' ;
16
17
import { ES256KSigner } from 'did-jwt' ;
18
+ import { DID , getChainId , Id } from '@iden3/js-iden3-core' ;
19
+ import { Hex } from '@iden3/js-crypto' ;
17
20
18
21
const didExample = {
19
22
'@context' : [
@@ -52,6 +55,23 @@ const didExample = {
52
55
authentication : [ 'did:example:123#JUvpllMEYUZ2joO59UNui_XYDqxVqiFLLAJ8klWuPBw' ]
53
56
} ;
54
57
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
+
55
75
describe ( 'jws packer tests' , ( ) => {
56
76
const did = 'did:example:123' ;
57
77
let kms : KMS ;
@@ -93,6 +113,61 @@ describe('jws packer tests', () => {
93
113
expect ( data ) . to . not . be . undefined ;
94
114
} ) ;
95
115
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
+
96
171
it ( 'pack / unpack: no kid' , async ( ) => {
97
172
const msgBytes = byteEncoder . encode ( bodyMsgStr ) ;
98
173
0 commit comments