@sphereon/ssi-sdk-ext.did-resolver-key 0.10.2-unstable.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +10 -0
  3. package/dist/drivers/bls12381g2.d.ts +8 -0
  4. package/dist/drivers/bls12381g2.d.ts.map +1 -0
  5. package/dist/drivers/bls12381g2.js +49 -0
  6. package/dist/drivers/bls12381g2.js.map +1 -0
  7. package/dist/drivers/ed25519.d.ts +8 -0
  8. package/dist/drivers/ed25519.d.ts.map +1 -0
  9. package/dist/drivers/ed25519.js +118 -0
  10. package/dist/drivers/ed25519.js.map +1 -0
  11. package/dist/drivers/jwk.jcs.d.ts +8 -0
  12. package/dist/drivers/jwk.jcs.d.ts.map +1 -0
  13. package/dist/drivers/jwk.jcs.js +23 -0
  14. package/dist/drivers/jwk.jcs.js.map +1 -0
  15. package/dist/drivers/secp256k1.d.ts +8 -0
  16. package/dist/drivers/secp256k1.d.ts.map +1 -0
  17. package/dist/drivers/secp256k1.js +49 -0
  18. package/dist/drivers/secp256k1.js.map +1 -0
  19. package/dist/drivers/secp256r1.d.ts +24 -0
  20. package/dist/drivers/secp256r1.d.ts.map +1 -0
  21. package/dist/drivers/secp256r1.js +100 -0
  22. package/dist/drivers/secp256r1.js.map +1 -0
  23. package/dist/drivers/secp384r1.d.ts +24 -0
  24. package/dist/drivers/secp384r1.d.ts.map +1 -0
  25. package/dist/drivers/secp384r1.js +100 -0
  26. package/dist/drivers/secp384r1.js.map +1 -0
  27. package/dist/drivers/secp521r1.d.ts +23 -0
  28. package/dist/drivers/secp521r1.d.ts.map +1 -0
  29. package/dist/drivers/secp521r1.js +88 -0
  30. package/dist/drivers/secp521r1.js.map +1 -0
  31. package/dist/index.d.ts +8 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +94 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/types.d.ts +14 -0
  36. package/dist/types.d.ts.map +1 -0
  37. package/dist/types.js +6 -0
  38. package/dist/types.js.map +1 -0
  39. package/package.json +55 -0
  40. package/src/__tests__/__snapshots__/secp256r1.test.ts.snap +218 -0
  41. package/src/__tests__/__snapshots__/secp384r1.test.ts.snap +249 -0
  42. package/src/__tests__/__snapshots__/secp521r1.test.ts.snap +280 -0
  43. package/src/__tests__/fixtures/bls_did_doc.json +29 -0
  44. package/src/__tests__/fixtures/ed25519-x25519.json +240 -0
  45. package/src/__tests__/fixtures/ed25519_did_doc.json +24 -0
  46. package/src/__tests__/fixtures/jwk_jcs_did_doc.json +35 -0
  47. package/src/__tests__/fixtures/secp256k1_did_doc.json +21 -0
  48. package/src/__tests__/key_resolver.test.ts +44 -0
  49. package/src/__tests__/secp256r1.test.ts +207 -0
  50. package/src/__tests__/secp384r1.test.ts +233 -0
  51. package/src/__tests__/secp521r1.test.ts +202 -0
  52. package/src/drivers/bls12381g2.ts +24 -0
  53. package/src/drivers/ed25519.ts +115 -0
  54. package/src/drivers/jwk.jcs.ts +28 -0
  55. package/src/drivers/secp256k1.ts +25 -0
  56. package/src/drivers/secp256r1.ts +82 -0
  57. package/src/drivers/secp384r1.ts +82 -0
  58. package/src/drivers/secp521r1.ts +68 -0
  59. package/src/index.ts +65 -0
  60. package/src/types.ts +22 -0
@@ -0,0 +1,44 @@
1
+ import { Resolver } from 'did-resolver'
2
+ import { DID_JSON, DID_LD_JSON, getResolver } from '../index'
3
+ import * as fs from 'fs'
4
+
5
+ const ed25519Fixtures = JSON.parse(fs.readFileSync(`${__dirname}/fixtures/ed25519-x25519.json`, { encoding: 'utf-8' }))
6
+
7
+ describe('@sphereon/ssi-sdk-ext-key-did-resolver', () => {
8
+ it('should resolve a BLS did:key', async () => {
9
+ const resolver = new Resolver({ ...getResolver() })
10
+ const doc = await resolver.resolve(
11
+ 'did:key:zUC7Gc59EawPuAbe1gcbmpTtYeyRvRLUsCfkmHwmNaiQyQtQp9f4G4KHurpHaa6QUvm1mL1rZvKXQWpfRcTBfLsstL2kmMN3rkFSzYuzbxwD4LespdY8NKdsghxeiRNtNSbzKic',
12
+ { accept: DID_JSON }
13
+ )
14
+ expect(doc).toEqual(JSON.parse(fs.readFileSync(`${__dirname}/fixtures/bls_did_doc.json`, { encoding: 'utf-8' })))
15
+ })
16
+ it('should resolve a Ed25519 did:key with 2018 format', async () => {
17
+ const resolver = new Resolver({ ...getResolver() })
18
+ const doc = await resolver.resolve('did:key:z6MkvqoYXQfDDJRv8L4wKzxYeuKyVZBfi9Qo6Ro8MiLH3kDQ', { publicKeyFormat: 'Ed25519VerificationKey2018' })
19
+ expect(doc.didDocument).toEqual(ed25519Fixtures['did:key:z6MkvqoYXQfDDJRv8L4wKzxYeuKyVZBfi9Qo6Ro8MiLH3kDQ'].didDocument)
20
+ })
21
+ it('should resolve a Ed25519 did:key with 2020 format', async () => {
22
+ const resolver = new Resolver({ ...getResolver() })
23
+ const doc = await resolver.resolve('did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK', { publicKeyFormat: 'Ed25519VerificationKey2020' })
24
+ expect(doc.didDocument).toEqual(JSON.parse(fs.readFileSync(`${__dirname}/fixtures/ed25519_did_doc.json`, { encoding: 'utf-8' })))
25
+ })
26
+ it('should resolve a Ed25519 did:key without format', async () => {
27
+ const resolver = new Resolver({ ...getResolver() })
28
+ const doc = await resolver.resolve('did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK')
29
+ expect(doc.didDocument).toEqual(JSON.parse(fs.readFileSync(`${__dirname}/fixtures/ed25519_did_doc.json`, { encoding: 'utf-8' })))
30
+ })
31
+ it('should resolve a secp256k1 did:key', async () => {
32
+ const resolver = new Resolver({ ...getResolver() })
33
+ const doc = await resolver.resolve('did:key:zQ3shokFTS3brHcDQrn82RUDfCZESWL1ZdCEJwekUDPQiYBme', { accept: DID_JSON })
34
+ expect(doc).toEqual(JSON.parse(fs.readFileSync(`${__dirname}/fixtures/secp256k1_did_doc.json`, { encoding: 'utf-8' })))
35
+ })
36
+ it('should resolve a jcs JWK did:key', async () => {
37
+ const resolver = new Resolver({ ...getResolver() })
38
+ const doc = await resolver.resolve(
39
+ 'did:key:z2dmzD81cgPx8Vki7JbuuMmFYrWPgYoytykUZ3eyqht1j9KbsEYvdrjxMjQ4tpnje9BDBTzuNDP3knn6qLZErzd4bJ5go2CChoPjd5GAH3zpFJP5fuwSk66U5Pq6EhF4nKnHzDnznEP8fX99nZGgwbAh1o7Gj1X52Tdhf7U4KTk66xsA5r',
40
+ { accept: DID_LD_JSON }
41
+ )
42
+ expect(doc).toEqual(JSON.parse(fs.readFileSync(`${__dirname}/fixtures/jwk_jcs_did_doc.json`, { encoding: 'utf-8' })))
43
+ })
44
+ })
@@ -0,0 +1,207 @@
1
+ // @ts-nocheck
2
+ // Brent Shambaugh <brent.shambaugh@gmail.com>. 2021.
3
+
4
+ import * as varint from 'varint'
5
+ // import { base58btc } from 'multiformats/bases/base58'
6
+ import * as mapper from '../drivers/secp256r1'
7
+ import * as u8a from 'uint8arrays'
8
+ import { base58btc } from 'multiformats/bases/base58'
9
+
10
+ describe('Secp256r1 mapper', () => {
11
+ it('successfully resolves the document from did:key from raw public key', async () => {
12
+ const id = 'zruuPojWkzGPb8sVc42f2YxcTXKUTpAUbdrzVovaTBmGGNyK6cGFaA4Kp7SSLKecrxYz8Sc9d77Rss7rayYt1oFCaNJ'
13
+
14
+ const multiformatPubKey = base58btc.decode(id)
15
+ varint.decode(multiformatPubKey) // decode is changing param multiformatPubKey as well
16
+ const pubKeyBytes = multiformatPubKey.slice(varint.decode.bytes)
17
+ const doc = await mapper.keyToDidDoc({ pubKeyBytes, fingerprint: id })
18
+ expect(doc).toMatchSnapshot()
19
+ })
20
+
21
+ it('successfully resolves the document from did:key from raw public key #2', async () => {
22
+ const id = 'zrusAFgBbf84b8mBz8Cmy8UoFWKV52EaeRnK86vnLo4Z5QoRypE6hXVPN2urevZMAMtcTaCDFLWBaE1Q3jmdb1FHgve'
23
+
24
+ const multiformatPubKey = base58btc.decode(id)
25
+ varint.decode(multiformatPubKey) // decode is changing param multiformatPubKey as well
26
+ const pubKeyBytes = multiformatPubKey.slice(varint.decode.bytes)
27
+ const doc = await mapper.keyToDidDoc({ pubKeyBytes, fingerprint: id })
28
+ expect(doc).toMatchSnapshot()
29
+ })
30
+
31
+ it('successfully resolves the document from did:key from raw public key #3', async () => {
32
+ const id = 'zrurwcJZss4ruepVNu1H3xmSirvNbzgBk9qrCktB6kaewXnJAhYWwtP3bxACqBpzjZdN7TyHNzzGGSSH5qvZsSDir9z'
33
+
34
+ const multiformatPubKey = base58btc.decode(id)
35
+ varint.decode(multiformatPubKey) // decode is changing param multiformatPubKey as well
36
+ const pubKeyBytes = multiformatPubKey.slice(varint.decode.bytes)
37
+ const doc = await mapper.keyToDidDoc({ pubKeyBytes, fingerprint: id })
38
+ expect(doc).toMatchSnapshot()
39
+ })
40
+
41
+ it('successfully resolves the document from did:key from compressed public key', async () => {
42
+ const id = 'zDnaeUKTWUXc1HDpGfKbEK31nKLN19yX5aunFd7VK1CUMeyJu'
43
+
44
+ const multiformatPubKey = base58btc.decode(id)
45
+ varint.decode(multiformatPubKey) // decode is changing param multiformatPubKey as well
46
+ const pubKeyBytes = multiformatPubKey.slice(varint.decode.bytes)
47
+ const doc = await mapper.keyToDidDoc({ pubKeyBytes, fingerprint: id })
48
+ expect(doc).toMatchSnapshot()
49
+ })
50
+
51
+ it('successfully resolves the document from did:key from compressed public key #2', async () => {
52
+ const id = 'zDnaerx9CtbPJ1q36T5Ln5wYt3MQYeGRG5ehnPAmxcf5mDZpv'
53
+
54
+ const multiformatPubKey = base58btc.decode(id)
55
+ varint.decode(multiformatPubKey) // decode is changing param multiformatPubKey as well
56
+ const pubKeyBytes = multiformatPubKey.slice(varint.decode.bytes)
57
+ const doc = await mapper.keyToDidDoc({ pubKeyBytes, fingerprint: id })
58
+ expect(doc).toMatchSnapshot()
59
+ })
60
+
61
+ it('successfully resolves the document from did:key from compressed public key #3', async () => {
62
+ const id = 'zDnaerDaTF5BXEavCrfRZEk316dpbLsfPDZ3WJ5hRTPFU2169'
63
+
64
+ const multiformatPubKey = base58btc.decode(id)
65
+ varint.decode(multiformatPubKey) // decode is changing param multiformatPubKey as well
66
+ const pubKeyBytes = multiformatPubKey.slice(varint.decode.bytes)
67
+ const doc = await mapper.keyToDidDoc({ pubKeyBytes, fingerprint: id })
68
+ expect(doc).toMatchSnapshot()
69
+ })
70
+
71
+ it('successfully resolves the document from did:key from uncompressed public key', async () => {
72
+ const id = 'z4oJ8emo5e6mGPCUS5wncFZXAyuVzGRyJZvoduwq7FrdZYPd1LZQbDKsp1YAMX8x14zBwy3yHMSpfecJCMDeRFUgFqYsY'
73
+
74
+ const multiformatPubKey = base58btc.decode(id)
75
+ varint.decode(multiformatPubKey) // decode is changing param multiformatPubKey as well
76
+ const pubKeyBytes = multiformatPubKey.slice(varint.decode.bytes)
77
+ const doc = await mapper.keyToDidDoc({ pubKeyBytes, fingerprint: id })
78
+ expect(doc).toMatchSnapshot()
79
+ })
80
+ })
81
+
82
+ test('expect publicKeyBytesToXY to throw an error for undefined', () => {
83
+ expect(() => {
84
+ mapper.pubKeyBytesToXY()
85
+ }).toThrowError('input must be a Uint8Array')
86
+ })
87
+
88
+ test('expect publicKeyBytesToXY to throw an error for null', () => {
89
+ expect(() => {
90
+ mapper.pubKeyBytesToXY(null)
91
+ }).toThrowError('input must be a Uint8Array')
92
+ })
93
+
94
+ test('expect publicKeyBytesToXY to throw an error for and integer input', () => {
95
+ expect(() => {
96
+ mapper.pubKeyBytesToXY(5)
97
+ }).toThrowError('input must be a Uint8Array')
98
+ })
99
+
100
+ test('test a uncompressed public key in hex to an x,y point with x, and y url encoded with an unsupported prefix', () => {
101
+ const inputPublicKeyHex =
102
+ '03f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f'
103
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
104
+ expect(() => {
105
+ mapper.pubKeyBytesToXY(publicKey_u8a)
106
+ }).toThrowError('Unexpected pubKeyBytes')
107
+ })
108
+
109
+ test('test a compressed public key in hex to an x,y point with x, and y url encoded with an unsupported prefixi: try2', () => {
110
+ const inputPublicKeyHex = '05f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb6'
111
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
112
+ expect(() => {
113
+ mapper.pubKeyBytesToXY(publicKey_u8a)
114
+ }).toThrowError('Unexpected pubKeyBytes')
115
+ })
116
+
117
+ test('test a compressed public key in hex to an x,y point with x, and y url encoded with an unsupported prefixi: try3', () => {
118
+ const inputPublicKeyHex = '04f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb6'
119
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
120
+ expect(() => {
121
+ mapper.pubKeyBytesToXY(publicKey_u8a)
122
+ }).toThrowError('Unexpected pubKeyBytes')
123
+ })
124
+
125
+ test('test a compressed public key in hex to an x,y point with x, and y url encoded with an unsupported prefix', () => {
126
+ const inputPublicKeyHex = '04f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb6'
127
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
128
+ expect(() => {
129
+ mapper.pubKeyBytesToXY(publicKey_u8a)
130
+ }).toThrowError('Unexpected pubKeyBytes')
131
+ })
132
+
133
+ test('test a compressed public key in hex to an x,y point with x, and y url encoded with an unexpected length', () => {
134
+ const inputPublicKeyHex = '0239c3dd74131729446dc1b3da67d49fc046fcbf072fcc5b9fa51c05b974307f9642'
135
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
136
+ expect(() => {
137
+ mapper.pubKeyBytesToXY(publicKey_u8a)
138
+ }).toThrowError('Unexpected pubKeyBytes')
139
+ })
140
+
141
+ test('test a hex string longer than 65 bytes', () => {
142
+ const inputPublicKeyHex =
143
+ '0704f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f'
144
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
145
+ expect(() => {
146
+ mapper.pubKeyBytesToXY(publicKey_u8a)
147
+ }).toThrowError('Unexpected pubKeyBytes')
148
+ })
149
+
150
+ test('test a hex string longer than 65 bytes: try2', () => {
151
+ const inputPublicKeyHex =
152
+ '04f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f07'
153
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
154
+ expect(() => {
155
+ mapper.pubKeyBytesToXY(publicKey_u8a)
156
+ }).toThrowError('Unexpected pubKeyBytes')
157
+ })
158
+
159
+ test('test a compressed public key in hex to an x,y point with x, and y url encoded', () => {
160
+ const inputPublicKeyHex = '03f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb6'
161
+ const output = {
162
+ xm: '-cNviWRiM3i9wGjUvOB-0XyPpIb5rAwmE8o8jDBte7Y',
163
+ ym: 'HNNnF7isXk_qitI9yNB4PCMY7krXqA224AJq0LByok8',
164
+ }
165
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
166
+ const pubKeyBytesToXY = mapper.pubKeyBytesToXY(publicKey_u8a)
167
+ expect(pubKeyBytesToXY).toEqual(output)
168
+ })
169
+
170
+ test('test a uncompressed public key in hex to an x,y point with x, and y url encoded', () => {
171
+ const inputPublicKeyHex =
172
+ '04f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f'
173
+ const output = {
174
+ xm: '-cNviWRiM3i9wGjUvOB-0XyPpIb5rAwmE8o8jDBte7Y',
175
+ ym: 'HNNnF7isXk_qitI9yNB4PCMY7krXqA224AJq0LByok8',
176
+ }
177
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
178
+ const pubKeyBytesToXY = mapper.pubKeyBytesToXY(publicKey_u8a)
179
+ expect(pubKeyBytesToXY).toEqual(output)
180
+ })
181
+
182
+ test('test a raw public key in hex to an x,y point with x, and y url encoded', () => {
183
+ const inputPublicKeyHex =
184
+ 'f9c36f8964623378bdc068d4bce07ed17c8fa486f9ac0c2613ca3c8c306d7bb61cd36717b8ac5e4fea8ad23dc8d0783c2318ee4ad7a80db6e0026ad0b072a24f'
185
+ const output = {
186
+ xm: '-cNviWRiM3i9wGjUvOB-0XyPpIb5rAwmE8o8jDBte7Y',
187
+ ym: 'HNNnF7isXk_qitI9yNB4PCMY7krXqA224AJq0LByok8',
188
+ }
189
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
190
+ const pubKeyBytesToXY = mapper.pubKeyBytesToXY(publicKey_u8a)
191
+ expect(pubKeyBytesToXY).toEqual(output)
192
+ })
193
+
194
+ //**** end of tests
195
+
196
+ // Function for test. Eliminate this when key-did-resolver is written.
197
+
198
+ function pubKeyHexToUint8Array(publicKeyHex: string) {
199
+ if (!publicKeyHex) {
200
+ throw new TypeError('input cannot be null or undefined.')
201
+ }
202
+ if (publicKeyHex.length % 2 == 0) {
203
+ return u8a.fromString(publicKeyHex, 'base16')
204
+ } else {
205
+ return u8a.fromString('0' + publicKeyHex, 'base16')
206
+ }
207
+ }
@@ -0,0 +1,233 @@
1
+ // Brent Shambaugh <brent.shambaugh@gmail.com>. 2021.
2
+
3
+ import * as varint from 'varint'
4
+ // import { base58btc } from 'multiformats/bases/base58'
5
+ import * as mapper from '../drivers/secp384r1'
6
+ import * as u8a from 'uint8arrays'
7
+
8
+ import { base58btc } from 'multiformats/bases/base58'
9
+
10
+ describe('Secp384r1 mapper', () => {
11
+ it('Secp384r1 mapper successfully resolves the document from did:key from raw public key', async () => {
12
+ const id =
13
+ 'zFwfeyrSyWdksRYykTGGtagWazFB5zS4CjQcxDMQSNmCTQB5QMqokx2VJz4vBB2hN1nUrYDTuYq3kd1BM5cUCfFD4awiNuzEBuoy6rZZTMCsZsdvWkDXY6832qcAnzE7YGw43KU'
14
+
15
+ const multiformatPubKey = base58btc.decode(id)
16
+ varint.decode(multiformatPubKey) // decode is changing param multiformatPubKey as well
17
+ const pubKeyBytes = multiformatPubKey.slice(varint.decode.bytes)
18
+ const doc = await mapper.keyToDidDoc({ pubKeyBytes, fingerprint: id })
19
+ expect(doc).toMatchSnapshot()
20
+ })
21
+
22
+ it('Secp384r1 mapper successfully resolves the document from did:key from raw public key #2', async () => {
23
+ const id =
24
+ 'zFwepbBSaPFjt5T1zWptHaXugLNxHYABfJrDoAZRYxKjNkpdfrniF3pvYQAXwxVB7afhmsgzYtSCzTVZQ3F5SPHzP5PuHgtBGNYucZTSrnA7yTTDr7WGQZaTTkJWfiH47jW5ahU'
25
+
26
+ const multiformatPubKey = base58btc.decode(id)
27
+ varint.decode(multiformatPubKey) // decode is changing param multiformatPubKey as well
28
+ const pubKeyBytes = multiformatPubKey.slice(varint.decode.bytes)
29
+ const doc = await mapper.keyToDidDoc({ pubKeyBytes, fingerprint: id })
30
+ expect(doc).toMatchSnapshot()
31
+ })
32
+
33
+ it('Secp384r1 mapper successfully resolves the document from did:key from raw public key #3', async () => {
34
+ const id =
35
+ 'zFwfwzpxzCAUjJK6X7cLDFjxbp6G3iJy6AcntWLBu5SxJeGBjge7jVkmARyUqkJideMFofkhGF94wLopAmuqCH1JQ3fbzxmrBwKK52qF5w429kUJk5NdR8BJwDxpeWryV4oAH27'
36
+
37
+ const multiformatPubKey = base58btc.decode(id)
38
+ varint.decode(multiformatPubKey) // decode is changing param multiformatPubKey as well
39
+ const pubKeyBytes = multiformatPubKey.slice(varint.decode.bytes)
40
+ const doc = await mapper.keyToDidDoc({ pubKeyBytes, fingerprint: id })
41
+ expect(doc).toMatchSnapshot()
42
+ })
43
+
44
+ it('Secp384r1 mapper successfully resolves the document from did:key from compressed public key', async () => {
45
+ const id = 'z82Lm1MpAkeJcix9K8TMiLd5NMAhnwkjjCBeWHXyu3U4oT2MVJJKXkcVBgjGhnLBn2Kaau9'
46
+
47
+ const multiformatPubKey = base58btc.decode(id)
48
+ varint.decode(multiformatPubKey) // decode is changing param multiformatPubKey as well
49
+ const pubKeyBytes = multiformatPubKey.slice(varint.decode.bytes)
50
+ const doc = await mapper.keyToDidDoc({ pubKeyBytes, fingerprint: id })
51
+ expect(doc).toMatchSnapshot()
52
+ })
53
+
54
+ it('Secp384r1 mapper successfully resolves the document from did:key from compressed public key #2', async () => {
55
+ const id = 'z82LkvCwHNreneWpsgPEbV3gu1C6NFJEBg4srfJ5gdxEsMGRJUz2sG9FE42shbn2xkZJh54'
56
+
57
+ const multiformatPubKey = base58btc.decode(id)
58
+ varint.decode(multiformatPubKey) // decode is changing param multiformatPubKey as well
59
+ const pubKeyBytes = multiformatPubKey.slice(varint.decode.bytes)
60
+ const doc = await mapper.keyToDidDoc({ pubKeyBytes, fingerprint: id })
61
+ expect(doc).toMatchSnapshot()
62
+ })
63
+
64
+ it('Secp384r1 mapper successfully resolves the document from did:key from compressed public key #3', async () => {
65
+ const id = 'z82Lm2BuneDPATu4BSWzhZwuandHAwY4DJrv3gAbo8RvG6yBTLJx6AhgoSmKy8XSK4HaPvA'
66
+
67
+ const multiformatPubKey = base58btc.decode(id)
68
+ varint.decode(multiformatPubKey) // decode is changing param multiformatPubKey as well
69
+ const pubKeyBytes = multiformatPubKey.slice(varint.decode.bytes)
70
+ const doc = await mapper.keyToDidDoc({ pubKeyBytes, fingerprint: id })
71
+ expect(doc).toMatchSnapshot()
72
+ })
73
+
74
+ // testing the key from the did:key from the uncompressed public key
75
+ it('successfully resolves the document from did', async () => {
76
+ const id =
77
+ 'z28xDr9xiQCrXbooH2aC3eMVv74QKvxBgP1DHCMBWz6CvTHmdt4rtsH9JSHGsyPzdQpfMBJSSAHFh1zTjiyLhKchrMnNfBVEtCziwX2yy3YiQY9t6WcVUpSdVHaxeRz5x6JYoGGPJ'
78
+
79
+ const multiformatPubKey = base58btc.decode(id)
80
+ varint.decode(multiformatPubKey) // decode is changing param multiformatPubKey as well
81
+ const pubKeyBytes = multiformatPubKey.slice(varint.decode.bytes)
82
+ const doc = await mapper.keyToDidDoc({ pubKeyBytes, fingerprint: id })
83
+ expect(doc).toMatchSnapshot()
84
+ })
85
+
86
+ // testing the key from the did:key from the uncompressed public key
87
+ it('successfully resolves the document from did', async () => {
88
+ const id =
89
+ 'z28xDrNf4RYwmuLQmfFBWWwiaxZtqyfME8BGUHemrsKUn6ShdzCLZWq2ZhmmSpVK2rtSLoeA1CJjrwGjZ64yCjJ9odVTYDdAMSu2LsTL1c5ehyQdkatFonfv3d7VNCByDrqntBoVz'
90
+
91
+ const multiformatPubKey = base58btc.decode(id)
92
+ varint.decode(multiformatPubKey) // decode is changing param multiformatPubKey as well
93
+ const pubKeyBytes = multiformatPubKey.slice(varint.decode.bytes)
94
+ const doc = await mapper.keyToDidDoc({ pubKeyBytes, fingerprint: id })
95
+ expect(doc).toMatchSnapshot()
96
+ })
97
+ })
98
+
99
+ test('expect publicKeyBytesToXY to throw an error for undefined', () => {
100
+ expect(() => {
101
+ mapper.pubKeyBytesToXY(undefined as never)
102
+ }).toThrowError('input must be a Uint8Array')
103
+ })
104
+
105
+ test('expect publicKeyBytesToXY to throw an error for null', () => {
106
+ expect(() => {
107
+ mapper.pubKeyBytesToXY(null as never)
108
+ }).toThrowError('input must be a Uint8Array')
109
+ })
110
+
111
+ test('expect publicKeyBytesToXY to throw an error for and integer input', () => {
112
+ expect(() => {
113
+ mapper.pubKeyBytesToXY(5 as never)
114
+ }).toThrowError('input must be a Uint8Array')
115
+ })
116
+
117
+ test('test a uncompressed public key in hex to an x,y point with x, and y url with an unsupported prefix', () => {
118
+ const inputPublicKeyHex =
119
+ '05041d73367caac24ba6ef7a2cc6b32cb525ef806dbf4a9044507863fbc2e441ad9425f17021104e76637f844db9aec27168d967d6543947d9fbdb82021b9942a0a9f0e48cfd6075e69ae3674f6724368e42561bf73dbf107a0ed17e92858aa36f'
120
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
121
+ expect(() => {
122
+ mapper.pubKeyBytesToXY(publicKey_u8a)
123
+ }).toThrowError('Unexpected pubKeyBytes')
124
+ })
125
+
126
+ test('test a uncompressed public key in hex to an x,y point with x, and y url encoded with an unsupported prefix that is too long', () => {
127
+ const inputPublicKeyHex =
128
+ '03041d73367caac24ba6ef7a2cc6b32cb525ef806dbf4a9044507863fbc2e441ad9425f17021104e76637f844db9aec27168d967d6543947d9fbdb82021b9942a0a9f0e48cfd6075e69ae3674f6724368e42561bf73dbf107a0ed17e92858aa36f07'
129
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
130
+ expect(() => {
131
+ mapper.pubKeyBytesToXY(publicKey_u8a)
132
+ }).toThrowError('Unexpected pubKeyBytes')
133
+ })
134
+
135
+ test('test a compressed public key in hex to an x,y point with x, and y url encoded with an unsupported prefixi: try2', () => {
136
+ const inputPublicKeyHex = '050e167184c0436f5940e63d89f3827bb90978cbd3b26ef66d52da7638ce0f83492fe3f27794d8dd75a8b9553161b8613a'
137
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
138
+ expect(() => {
139
+ mapper.pubKeyBytesToXY(publicKey_u8a)
140
+ }).toThrowError('Unexpected pubKeyBytes')
141
+ })
142
+
143
+ test('test a compressed public key in hex to an x,y point with x, and y url encoded with an unsupported prefixi: try3', () => {
144
+ const inputPublicKeyHex = '040e167184c0436f5940e63d89f3827bb90978cbd3b26ef66d52da7638ce0f83492fe3f27794d8dd75a8b9553161b8613a'
145
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
146
+ expect(() => {
147
+ mapper.pubKeyBytesToXY(publicKey_u8a)
148
+ }).toThrowError('Unexpected pubKeyBytes')
149
+ })
150
+
151
+ test('test a compressed public key in hex to an x,y point with x, and y url encoded with an unsupported prefix', () => {
152
+ const inputPublicKeyHex = '040e167184c0436f5940e63d89f3827bb90978cbd3b26ef66d52da7638ce0f83492fe3f27794d8dd75a8b9553161b8613a'
153
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
154
+ expect(() => {
155
+ mapper.pubKeyBytesToXY(publicKey_u8a)
156
+ }).toThrowError('Unexpected pubKeyBytes')
157
+ })
158
+
159
+ test('test a compressed public key in hex to an x,y point with x, and y url encoded with an unexpected length', () => {
160
+ const inputPublicKeyHex = '020e167184c0436f5940e63d89f3827bb90978cbd3b26ef66d52da7638ce0f83492fe3f27794d8dd75a8b9553161b8613a07'
161
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
162
+ expect(() => {
163
+ mapper.pubKeyBytesToXY(publicKey_u8a)
164
+ }).toThrowError('Unexpected pubKeyBytes')
165
+ })
166
+
167
+ test('test a hex string longer than 97 bytes', () => {
168
+ const inputPublicKeyHex =
169
+ '0704041d73367caac24ba6ef7a2cc6b32cb525ef806dbf4a9044507863fbc2e441ad9425f17021104e76637f844db9aec27168d967d6543947d9fbdb82021b9942a0a9f0e48cfd6075e69ae3674f6724368e42561bf73dbf107a0ed17e92858aa36f'
170
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
171
+ expect(() => {
172
+ mapper.pubKeyBytesToXY(publicKey_u8a)
173
+ }).toThrowError('Unexpected pubKeyBytes')
174
+ })
175
+
176
+ test('test a hex string longer than 97 bytes: try2', () => {
177
+ const inputPublicKeyHex =
178
+ '04041d73367caac24ba6ef7a2cc6b32cb525ef806dbf4a9044507863fbc2e441ad9425f17021104e76637f844db9aec27168d967d6543947d9fbdb82021b9942a0a9f0e48cfd6075e69ae3674f6724368e42561bf73dbf107a0ed17e92858aa36f07'
179
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
180
+ expect(() => {
181
+ mapper.pubKeyBytesToXY(publicKey_u8a)
182
+ }).toThrowError('Unexpected pubKeyBytes')
183
+ })
184
+
185
+ test('test a compressed public key in hex to an x,y point with x, and y url encoded', () => {
186
+ const inputPublicKeyHex = '020e167184c0436f5940e63d89f3827bb90978cbd3b26ef66d52da7638ce0f83492fe3f27794d8dd75a8b9553161b8613a'
187
+ const output = {
188
+ xm: 'DhZxhMBDb1lA5j2J84J7uQl4y9OybvZtUtp2OM4Pg0kv4_J3lNjddai5VTFhuGE6',
189
+ ym: 'QAzuG_cfV32pd8adVMpKYk5x9a9ZA5DMntSKQjl46SaLEkRPs2_gWoIu0cCuY8cy',
190
+ }
191
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
192
+ const pubKeyBytesToXY = mapper.pubKeyBytesToXY(publicKey_u8a)
193
+ expect(pubKeyBytesToXY).toEqual(output)
194
+ })
195
+
196
+ test('test a uncompressed public key in hex to an x,y point with x, and y url encoded', () => {
197
+ const inputPublicKeyHex =
198
+ '04041d73367caac24ba6ef7a2cc6b32cb525ef806dbf4a9044507863fbc2e441ad9425f17021104e76637f844db9aec27168d967d6543947d9fbdb82021b9942a0a9f0e48cfd6075e69ae3674f6724368e42561bf73dbf107a0ed17e92858aa36f'
199
+ const output = {
200
+ xm: 'BB1zNnyqwkum73osxrMstSXvgG2_SpBEUHhj-8LkQa2UJfFwIRBOdmN_hE25rsJx',
201
+ ym: 'aNln1lQ5R9n724ICG5lCoKnw5Iz9YHXmmuNnT2ckNo5CVhv3Pb8Qeg7RfpKFiqNv',
202
+ }
203
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
204
+ const pubKeyBytesToXY = mapper.pubKeyBytesToXY(publicKey_u8a)
205
+ expect(pubKeyBytesToXY).toEqual(output)
206
+ })
207
+
208
+ test('test a raw public key in hex to an x,y point with x, and y url encoded', () => {
209
+ const inputPublicKeyHex =
210
+ '041d73367caac24ba6ef7a2cc6b32cb525ef806dbf4a9044507863fbc2e441ad9425f17021104e76637f844db9aec27168d967d6543947d9fbdb82021b9942a0a9f0e48cfd6075e69ae3674f6724368e42561bf73dbf107a0ed17e92858aa36f'
211
+ const output = {
212
+ xm: 'BB1zNnyqwkum73osxrMstSXvgG2_SpBEUHhj-8LkQa2UJfFwIRBOdmN_hE25rsJx',
213
+ ym: 'aNln1lQ5R9n724ICG5lCoKnw5Iz9YHXmmuNnT2ckNo5CVhv3Pb8Qeg7RfpKFiqNv',
214
+ }
215
+ const publicKey_u8a = pubKeyHexToUint8Array(inputPublicKeyHex)
216
+ const pubKeyBytesToXY = mapper.pubKeyBytesToXY(publicKey_u8a)
217
+ expect(pubKeyBytesToXY).toEqual(output)
218
+ })
219
+
220
+ //**** end of tests
221
+
222
+ // Function for test. Eliminate this when key-did-resolver is written.
223
+
224
+ function pubKeyHexToUint8Array(publicKeyHex: string) {
225
+ if (!publicKeyHex) {
226
+ throw new TypeError('input cannot be null or undefined.')
227
+ }
228
+ if (publicKeyHex.length % 2 == 0) {
229
+ return u8a.fromString(publicKeyHex, 'base16')
230
+ } else {
231
+ return u8a.fromString('0' + publicKeyHex, 'base16')
232
+ }
233
+ }