@cheqd/sdk 1.5.0-develop.3 → 2.0.0-develop.1
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.
- package/CHANGELOG.md +11 -0
- package/build/index.d.ts +4 -2
- package/build/index.d.ts.map +1 -1
- package/build/index.js +2 -0
- package/build/index.js.map +1 -1
- package/build/modules/did.d.ts +31 -4
- package/build/modules/did.d.ts.map +1 -1
- package/build/modules/did.js +163 -14
- package/build/modules/did.js.map +1 -1
- package/build/modules/resource.d.ts +22 -0
- package/build/modules/resource.d.ts.map +1 -1
- package/build/modules/resource.js +43 -3
- package/build/modules/resource.js.map +1 -1
- package/build/signer.d.ts.map +1 -1
- package/build/signer.js +0 -13
- package/build/signer.js.map +1 -1
- package/build/types.d.ts +10 -236
- package/build/types.d.ts.map +1 -1
- package/build/types.js +1 -120
- package/build/types.js.map +1 -1
- package/build/utils.d.ts +5 -5
- package/build/utils.d.ts.map +1 -1
- package/build/utils.js +70 -20
- package/build/utils.js.map +1 -1
- package/package.json +3 -2
- package/src/index.ts +5 -2
- package/src/modules/did.ts +189 -16
- package/src/modules/resource.ts +48 -2
- package/src/signer.ts +2 -17
- package/src/types.ts +14 -156
- package/src/utils.ts +85 -38
- package/tests/index.test.ts +5 -6
- package/tests/modules/did.test.ts +390 -80
- package/tests/modules/resource.test.ts +160 -27
- package/tests/signer.test.ts +55 -31
- package/tests/testutils.test.ts +3 -148
- package/tests/utils.test.ts +4 -4
|
@@ -4,10 +4,11 @@ import { fromString, toString } from 'uint8arrays'
|
|
|
4
4
|
import { DIDModule, ResourceModule } from "../../src"
|
|
5
5
|
import { createDefaultCheqdRegistry } from "../../src/registry"
|
|
6
6
|
import { CheqdSigningStargateClient } from "../../src/signer"
|
|
7
|
-
import {
|
|
8
|
-
import { createDidPayload, createDidVerificationMethod, createKeyPairBase64, createVerificationKeys
|
|
7
|
+
import { ISignInputs, MethodSpecificIdAlgo, VerificationMethods } from '../../src/types';
|
|
8
|
+
import { createDidPayload, createDidVerificationMethod, createKeyPairBase64, createVerificationKeys } from "../../src/utils"
|
|
9
|
+
import { localnet, faucet, image_content, default_content } from "../testutils.test"
|
|
9
10
|
import { MsgCreateResourcePayload } from '@cheqd/ts-proto/cheqd/resource/v2';
|
|
10
|
-
import {
|
|
11
|
+
import { v4 } from "uuid"
|
|
11
12
|
|
|
12
13
|
const defaultAsyncTxTimeout = 30000
|
|
13
14
|
|
|
@@ -15,50 +16,41 @@ describe('ResourceModule', () => {
|
|
|
15
16
|
describe('constructor', () => {
|
|
16
17
|
it('should instantiate standalone module', async () => {
|
|
17
18
|
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic)
|
|
18
|
-
const signer = await CheqdSigningStargateClient.connectWithSigner(
|
|
19
|
+
const signer = await CheqdSigningStargateClient.connectWithSigner(localnet.rpcUrl, wallet)
|
|
19
20
|
const resourceModule = new ResourceModule(signer)
|
|
20
21
|
expect(resourceModule).toBeInstanceOf(ResourceModule)
|
|
21
22
|
})
|
|
22
23
|
})
|
|
23
24
|
|
|
24
25
|
describe('createResourceTx', () => {
|
|
25
|
-
it('should create a new Resource', async () => {
|
|
26
|
-
//
|
|
26
|
+
it('should create a new Resource - case: json', async () => {
|
|
27
|
+
// create an associated did document
|
|
27
28
|
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic, {prefix: faucet.prefix})
|
|
28
29
|
|
|
29
30
|
const registry = createDefaultCheqdRegistry(Array.from(DIDModule.registryTypes).concat(Array.from(ResourceModule.registryTypes)))
|
|
30
31
|
|
|
31
|
-
const signer = await CheqdSigningStargateClient.connectWithSigner(
|
|
32
|
+
const signer = await CheqdSigningStargateClient.connectWithSigner(localnet.rpcUrl, wallet, { registry })
|
|
32
33
|
|
|
33
34
|
const didModule = new DIDModule(signer)
|
|
34
35
|
|
|
35
36
|
const keyPair = createKeyPairBase64()
|
|
36
|
-
const verificationKeys = createVerificationKeys(keyPair, MethodSpecificIdAlgo.Base58, 'key-1'
|
|
37
|
+
const verificationKeys = createVerificationKeys(keyPair.publicKey, MethodSpecificIdAlgo.Base58, 'key-1')
|
|
37
38
|
const verificationMethods = createDidVerificationMethod([VerificationMethods.Ed255192020], [verificationKeys])
|
|
38
39
|
const didPayload = createDidPayload(verificationMethods, [verificationKeys])
|
|
39
40
|
|
|
40
41
|
const signInputs: ISignInputs[] = [
|
|
41
42
|
{
|
|
42
|
-
verificationMethodId: didPayload.verificationMethod[0].id,
|
|
43
|
+
verificationMethodId: didPayload.verificationMethod![0].id,
|
|
43
44
|
privateKeyHex: toString(fromString(keyPair.privateKey, 'base64'), 'hex')
|
|
44
45
|
}
|
|
45
46
|
]
|
|
46
47
|
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
{
|
|
50
|
-
denom: 'ncheq',
|
|
51
|
-
amount: '2500000000'
|
|
52
|
-
}
|
|
53
|
-
],
|
|
54
|
-
gas: '200000',
|
|
55
|
-
payer: (await wallet.getAccounts())[0].address
|
|
56
|
-
}
|
|
57
|
-
|
|
48
|
+
const feePayer = (await wallet.getAccounts())[0].address
|
|
49
|
+
const fee = await DIDModule.generateCreateDidDocFees(feePayer)
|
|
58
50
|
const didTx: DeliverTxResponse = await didModule.createDidTx(
|
|
59
51
|
signInputs,
|
|
60
52
|
didPayload,
|
|
61
|
-
|
|
53
|
+
feePayer,
|
|
62
54
|
fee
|
|
63
55
|
)
|
|
64
56
|
|
|
@@ -67,37 +59,178 @@ describe('ResourceModule', () => {
|
|
|
67
59
|
|
|
68
60
|
expect(didTx.code).toBe(0)
|
|
69
61
|
|
|
70
|
-
//
|
|
71
|
-
|
|
62
|
+
// create a did linked resource
|
|
72
63
|
const resourceModule = new ResourceModule(signer)
|
|
73
64
|
|
|
74
65
|
const resourcePayload: MsgCreateResourcePayload = {
|
|
75
66
|
collectionId: didPayload.id.split(":").reverse()[0],
|
|
76
|
-
id:
|
|
67
|
+
id: v4(),
|
|
77
68
|
version: "1.0",
|
|
78
69
|
alsoKnownAs: [],
|
|
79
70
|
name: 'Test Resource',
|
|
80
71
|
resourceType: 'test-resource-type',
|
|
81
|
-
data: new TextEncoder().encode("{
|
|
72
|
+
data: new TextEncoder().encode("{\"message\": \"hello world\"}")
|
|
82
73
|
}
|
|
83
74
|
|
|
75
|
+
const resourceSignInputs: ISignInputs[] = [
|
|
76
|
+
{
|
|
77
|
+
verificationMethodId: didPayload.verificationMethod![0].id,
|
|
78
|
+
keyType: 'Ed25519',
|
|
79
|
+
privateKeyHex: toString(fromString(keyPair.privateKey, 'base64'), 'hex')
|
|
80
|
+
}
|
|
81
|
+
]
|
|
82
|
+
|
|
83
|
+
const feeResourceJson = await ResourceModule.generateCreateResourceJsonFees(feePayer)
|
|
84
|
+
const resourceTx = await resourceModule.createResourceTx(
|
|
85
|
+
resourceSignInputs,
|
|
86
|
+
resourcePayload,
|
|
87
|
+
feePayer,
|
|
88
|
+
feeResourceJson
|
|
89
|
+
)
|
|
90
|
+
|
|
84
91
|
console.warn(`Using payload: ${JSON.stringify(resourcePayload)}`)
|
|
92
|
+
console.warn(`DID Tx: ${JSON.stringify(resourceTx)}`)
|
|
93
|
+
|
|
94
|
+
expect(resourceTx.code).toBe(0)
|
|
95
|
+
}, defaultAsyncTxTimeout)
|
|
96
|
+
|
|
97
|
+
it('should create a new Resource - case: image', async () => {
|
|
98
|
+
// create an associated did document
|
|
99
|
+
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic, {prefix: faucet.prefix})
|
|
100
|
+
|
|
101
|
+
const registry = createDefaultCheqdRegistry(Array.from(DIDModule.registryTypes).concat(Array.from(ResourceModule.registryTypes)))
|
|
102
|
+
|
|
103
|
+
const signer = await CheqdSigningStargateClient.connectWithSigner(localnet.rpcUrl, wallet, { registry })
|
|
104
|
+
|
|
105
|
+
const didModule = new DIDModule(signer)
|
|
106
|
+
|
|
107
|
+
const keyPair = createKeyPairBase64()
|
|
108
|
+
const verificationKeys = createVerificationKeys(keyPair.publicKey, MethodSpecificIdAlgo.Base58, 'key-1')
|
|
109
|
+
const verificationMethods = createDidVerificationMethod([VerificationMethods.Ed255192020], [verificationKeys])
|
|
110
|
+
const didPayload = createDidPayload(verificationMethods, [verificationKeys])
|
|
111
|
+
|
|
112
|
+
const signInputs: ISignInputs[] = [
|
|
113
|
+
{
|
|
114
|
+
verificationMethodId: didPayload.verificationMethod![0].id,
|
|
115
|
+
privateKeyHex: toString(fromString(keyPair.privateKey, 'base64'), 'hex')
|
|
116
|
+
}
|
|
117
|
+
]
|
|
118
|
+
|
|
119
|
+
const feePayer = (await wallet.getAccounts())[0].address
|
|
120
|
+
const fee = await DIDModule.generateCreateDidDocFees(feePayer)
|
|
121
|
+
const didTx: DeliverTxResponse = await didModule.createDidTx(
|
|
122
|
+
signInputs,
|
|
123
|
+
didPayload,
|
|
124
|
+
feePayer,
|
|
125
|
+
fee
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
console.warn(`Using payload: ${JSON.stringify(didPayload)}`)
|
|
129
|
+
console.warn(`DID Tx: ${JSON.stringify(didTx)}`)
|
|
130
|
+
|
|
131
|
+
expect(didTx.code).toBe(0)
|
|
132
|
+
|
|
133
|
+
// create a did linked resource
|
|
134
|
+
const resourceModule = new ResourceModule(signer)
|
|
135
|
+
|
|
136
|
+
const resourcePayload: MsgCreateResourcePayload = {
|
|
137
|
+
collectionId: didPayload.id.split(":").reverse()[0],
|
|
138
|
+
id: v4(),
|
|
139
|
+
version: "1.0",
|
|
140
|
+
alsoKnownAs: [],
|
|
141
|
+
name: 'Test Resource',
|
|
142
|
+
resourceType: 'test-resource-type',
|
|
143
|
+
data: fromString(image_content, 'base64')
|
|
144
|
+
}
|
|
85
145
|
|
|
86
146
|
const resourceSignInputs: ISignInputs[] = [
|
|
87
147
|
{
|
|
88
|
-
verificationMethodId: didPayload.verificationMethod[0].id,
|
|
148
|
+
verificationMethodId: didPayload.verificationMethod![0].id,
|
|
89
149
|
keyType: 'Ed25519',
|
|
90
150
|
privateKeyHex: toString(fromString(keyPair.privateKey, 'base64'), 'hex')
|
|
91
151
|
}
|
|
92
152
|
]
|
|
93
153
|
|
|
154
|
+
const feeResourceImage = await ResourceModule.generateCreateResourceImageFees(feePayer)
|
|
94
155
|
const resourceTx = await resourceModule.createResourceTx(
|
|
95
156
|
resourceSignInputs,
|
|
96
157
|
resourcePayload,
|
|
97
|
-
|
|
158
|
+
feePayer,
|
|
159
|
+
feeResourceImage
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
console.warn(`Using payload: ${JSON.stringify(resourcePayload)}`)
|
|
163
|
+
console.warn(`DID Tx: ${JSON.stringify(resourceTx)}`)
|
|
164
|
+
|
|
165
|
+
expect(resourceTx.code).toBe(0)
|
|
166
|
+
}, defaultAsyncTxTimeout)
|
|
167
|
+
|
|
168
|
+
it('should create a new Resource - case: default', async () => {
|
|
169
|
+
// create an associated did document
|
|
170
|
+
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic, {prefix: faucet.prefix})
|
|
171
|
+
|
|
172
|
+
const registry = createDefaultCheqdRegistry(Array.from(DIDModule.registryTypes).concat(Array.from(ResourceModule.registryTypes)))
|
|
173
|
+
|
|
174
|
+
const signer = await CheqdSigningStargateClient.connectWithSigner(localnet.rpcUrl, wallet, { registry })
|
|
175
|
+
|
|
176
|
+
const didModule = new DIDModule(signer)
|
|
177
|
+
|
|
178
|
+
const keyPair = createKeyPairBase64()
|
|
179
|
+
const verificationKeys = createVerificationKeys(keyPair.publicKey, MethodSpecificIdAlgo.Base58, 'key-1')
|
|
180
|
+
const verificationMethods = createDidVerificationMethod([VerificationMethods.Ed255192020], [verificationKeys])
|
|
181
|
+
const didPayload = createDidPayload(verificationMethods, [verificationKeys])
|
|
182
|
+
|
|
183
|
+
const signInputs: ISignInputs[] = [
|
|
184
|
+
{
|
|
185
|
+
verificationMethodId: didPayload.verificationMethod![0].id,
|
|
186
|
+
privateKeyHex: toString(fromString(keyPair.privateKey, 'base64'), 'hex')
|
|
187
|
+
}
|
|
188
|
+
]
|
|
189
|
+
|
|
190
|
+
const feePayer = (await wallet.getAccounts())[0].address
|
|
191
|
+
const fee = await DIDModule.generateCreateDidDocFees(feePayer)
|
|
192
|
+
const didTx: DeliverTxResponse = await didModule.createDidTx(
|
|
193
|
+
signInputs,
|
|
194
|
+
didPayload,
|
|
195
|
+
feePayer,
|
|
98
196
|
fee
|
|
99
197
|
)
|
|
100
198
|
|
|
199
|
+
console.warn(`Using payload: ${JSON.stringify(didPayload)}`)
|
|
200
|
+
console.warn(`DID Tx: ${JSON.stringify(didTx)}`)
|
|
201
|
+
|
|
202
|
+
expect(didTx.code).toBe(0)
|
|
203
|
+
|
|
204
|
+
// create a did linked resource
|
|
205
|
+
const resourceModule = new ResourceModule(signer)
|
|
206
|
+
|
|
207
|
+
const resourcePayload: MsgCreateResourcePayload = {
|
|
208
|
+
collectionId: didPayload.id.split(":").reverse()[0],
|
|
209
|
+
id: v4(),
|
|
210
|
+
version: "1.0",
|
|
211
|
+
alsoKnownAs: [],
|
|
212
|
+
name: 'Test Resource',
|
|
213
|
+
resourceType: 'test-resource-type',
|
|
214
|
+
data: new TextEncoder().encode(default_content)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const resourceSignInputs: ISignInputs[] = [
|
|
218
|
+
{
|
|
219
|
+
verificationMethodId: didPayload.verificationMethod![0].id,
|
|
220
|
+
keyType: 'Ed25519',
|
|
221
|
+
privateKeyHex: toString(fromString(keyPair.privateKey, 'base64'), 'hex')
|
|
222
|
+
}
|
|
223
|
+
]
|
|
224
|
+
|
|
225
|
+
const feeResourceDefault = await ResourceModule.generateCreateResourceDefaultFees(feePayer)
|
|
226
|
+
const resourceTx = await resourceModule.createResourceTx(
|
|
227
|
+
resourceSignInputs,
|
|
228
|
+
resourcePayload,
|
|
229
|
+
feePayer,
|
|
230
|
+
feeResourceDefault
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
console.warn(`Using payload: ${JSON.stringify(resourcePayload)}`)
|
|
101
234
|
console.warn(`DID Tx: ${JSON.stringify(resourceTx)}`)
|
|
102
235
|
|
|
103
236
|
expect(resourceTx.code).toBe(0)
|
package/tests/signer.test.ts
CHANGED
|
@@ -3,10 +3,12 @@ import { DirectSecp256k1HdWallet, Registry } from "@cosmjs/proto-signing"
|
|
|
3
3
|
import { EdDSASigner } from "did-jwt"
|
|
4
4
|
import { typeUrlMsgCreateDidDoc } from '../src/modules/did'
|
|
5
5
|
import { CheqdSigningStargateClient } from "../src/signer"
|
|
6
|
-
import { ISignInputs, MethodSpecificIdAlgo,
|
|
6
|
+
import { ISignInputs, MethodSpecificIdAlgo, VerificationMethods } from "../src/types"
|
|
7
7
|
import { fromString, toString } from 'uint8arrays'
|
|
8
|
-
import { createDidPayload, createDidVerificationMethod, createKeyPairBase64, createVerificationKeys,
|
|
8
|
+
import { createDidPayload, createDidVerificationMethod, createKeyPairBase64, createVerificationKeys, validateSpecCompliantPayload } from '../src/utils';
|
|
9
|
+
import { localnet, faucet } from "./testutils.test"
|
|
9
10
|
import { verify } from "@stablelib/ed25519"
|
|
11
|
+
import { v4 } from "uuid"
|
|
10
12
|
|
|
11
13
|
const nonExistingDid = "did:cHeQd:fantasticnet:123"
|
|
12
14
|
const nonExistingKeyId = 'did:cHeQd:fantasticnet:123#key-678'
|
|
@@ -34,7 +36,7 @@ describe('CheqdSigningStargateClient', () => {
|
|
|
34
36
|
describe('constructor', () => {
|
|
35
37
|
it('can be instantiated & works for cheqd networks', async () => {
|
|
36
38
|
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic)
|
|
37
|
-
const signer = await CheqdSigningStargateClient.connectWithSigner(
|
|
39
|
+
const signer = await CheqdSigningStargateClient.connectWithSigner(localnet.rpcUrl, wallet)
|
|
38
40
|
expect(signer).toBeInstanceOf(CheqdSigningStargateClient)
|
|
39
41
|
})
|
|
40
42
|
|
|
@@ -42,7 +44,7 @@ describe('CheqdSigningStargateClient', () => {
|
|
|
42
44
|
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic)
|
|
43
45
|
const registry = new Registry()
|
|
44
46
|
registry.register(typeUrlMsgCreateDidDoc, MsgCreateDidDoc)
|
|
45
|
-
const signer = await CheqdSigningStargateClient.connectWithSigner(
|
|
47
|
+
const signer = await CheqdSigningStargateClient.connectWithSigner(localnet.rpcUrl, wallet, { registry })
|
|
46
48
|
expect(signer.registry.lookupType(typeUrlMsgCreateDidDoc)).toBe(MsgCreateDidDoc)
|
|
47
49
|
})
|
|
48
50
|
})
|
|
@@ -50,61 +52,67 @@ describe('CheqdSigningStargateClient', () => {
|
|
|
50
52
|
describe('getDidSigner', () => {
|
|
51
53
|
it('can get a signer for a did', async () => {
|
|
52
54
|
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic)
|
|
53
|
-
const signer = await CheqdSigningStargateClient.connectWithSigner(
|
|
55
|
+
const signer = await CheqdSigningStargateClient.connectWithSigner(localnet.rpcUrl, wallet)
|
|
54
56
|
const keyPair = createKeyPairBase64()
|
|
55
|
-
const verificationKeys = createVerificationKeys(keyPair, MethodSpecificIdAlgo.Base58, 'key-1'
|
|
57
|
+
const verificationKeys = createVerificationKeys(keyPair.publicKey, MethodSpecificIdAlgo.Base58, 'key-1')
|
|
56
58
|
const verificationMethods = createDidVerificationMethod([VerificationMethods.Ed255192020], [verificationKeys])
|
|
57
|
-
const didPayload =
|
|
58
|
-
const
|
|
59
|
+
const didPayload = createDidPayload(verificationMethods, [verificationKeys])
|
|
60
|
+
const { protobufVerificationMethod } = validateSpecCompliantPayload(didPayload)
|
|
61
|
+
|
|
62
|
+
const didSigner = await signer.getDidSigner(didPayload.verificationMethod![0].id, protobufVerificationMethod!)
|
|
59
63
|
|
|
60
64
|
expect(didSigner).toBe(EdDSASigner)
|
|
61
65
|
})
|
|
62
66
|
|
|
63
67
|
it('should throw for a non-supported verification method', async () => {
|
|
64
68
|
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic)
|
|
65
|
-
const signer = await CheqdSigningStargateClient.connectWithSigner(
|
|
69
|
+
const signer = await CheqdSigningStargateClient.connectWithSigner(localnet.rpcUrl, wallet)
|
|
66
70
|
|
|
67
71
|
await expect(signer.getDidSigner(nonExistingVerificationDidDocument.verificationMethod[0].id, nonExistingVerificationDidDocument.verificationMethod)).rejects.toThrow()
|
|
68
72
|
})
|
|
69
73
|
|
|
70
74
|
it('should throw for non-matching verification method id', async () => {
|
|
71
75
|
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic)
|
|
72
|
-
const signer = await CheqdSigningStargateClient.connectWithSigner(
|
|
76
|
+
const signer = await CheqdSigningStargateClient.connectWithSigner(localnet.rpcUrl, wallet)
|
|
73
77
|
const keyPair = createKeyPairBase64()
|
|
74
|
-
const verificationKeys = createVerificationKeys(keyPair, MethodSpecificIdAlgo.Base58, 'key-1'
|
|
78
|
+
const verificationKeys = createVerificationKeys(keyPair.publicKey, MethodSpecificIdAlgo.Base58, 'key-1')
|
|
75
79
|
const verificationMethods = createDidVerificationMethod([VerificationMethods.Ed255192020], [verificationKeys])
|
|
76
|
-
const
|
|
77
|
-
|
|
80
|
+
const payload = createDidPayload(verificationMethods, [verificationKeys])
|
|
81
|
+
const { protobufVerificationMethod } = validateSpecCompliantPayload(payload)
|
|
82
|
+
|
|
83
|
+
await expect(signer.getDidSigner(nonExistingKeyId, protobufVerificationMethod!)).rejects.toThrow()
|
|
78
84
|
})
|
|
79
85
|
})
|
|
80
86
|
|
|
81
87
|
describe('checkDidSigners', () => {
|
|
82
88
|
it('it should instantiate a signer for a did', async () => {
|
|
83
89
|
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic)
|
|
84
|
-
const signer = await CheqdSigningStargateClient.connectWithSigner(
|
|
90
|
+
const signer = await CheqdSigningStargateClient.connectWithSigner(localnet.rpcUrl, wallet)
|
|
85
91
|
const keyPair = createKeyPairBase64()
|
|
86
|
-
const verificationKeys = createVerificationKeys(keyPair, MethodSpecificIdAlgo.Base58, 'key-1'
|
|
92
|
+
const verificationKeys = createVerificationKeys(keyPair.publicKey, MethodSpecificIdAlgo.Base58, 'key-1')
|
|
87
93
|
const verificationMethods = createDidVerificationMethod([VerificationMethods.Ed255192020], [verificationKeys])
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
const didSigners = await signer.checkDidSigners(
|
|
94
|
+
const payload = createDidPayload(verificationMethods, [verificationKeys])
|
|
95
|
+
const { protobufVerificationMethod } = validateSpecCompliantPayload(payload)
|
|
96
|
+
const didSigners = await signer.checkDidSigners(protobufVerificationMethod)
|
|
91
97
|
|
|
92
98
|
expect(didSigners[VerificationMethods.Ed255192020]).toBe(EdDSASigner)
|
|
93
99
|
})
|
|
94
100
|
|
|
95
101
|
it('should instantiate multiple signers for a did with multiple verification methods', async () => {
|
|
96
102
|
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic)
|
|
97
|
-
const signer = await CheqdSigningStargateClient.connectWithSigner(
|
|
103
|
+
const signer = await CheqdSigningStargateClient.connectWithSigner(localnet.rpcUrl, wallet)
|
|
98
104
|
const keyPair1 = createKeyPairBase64()
|
|
99
105
|
const keyPair2 = createKeyPairBase64()
|
|
100
106
|
const keyPair3 = createKeyPairBase64()
|
|
101
|
-
const verificationKeys1 = createVerificationKeys(keyPair1, MethodSpecificIdAlgo.Base58, 'key-1'
|
|
102
|
-
const verificationKeys2 = createVerificationKeys(keyPair2, MethodSpecificIdAlgo.Base58, 'key-2'
|
|
103
|
-
const verificationKeys3 = createVerificationKeys(keyPair3, MethodSpecificIdAlgo.Base58, 'key-3'
|
|
107
|
+
const verificationKeys1 = createVerificationKeys(keyPair1.publicKey, MethodSpecificIdAlgo.Base58, 'key-1')
|
|
108
|
+
const verificationKeys2 = createVerificationKeys(keyPair2.publicKey, MethodSpecificIdAlgo.Base58, 'key-2')
|
|
109
|
+
const verificationKeys3 = createVerificationKeys(keyPair3.publicKey, MethodSpecificIdAlgo.Base58, 'key-3')
|
|
104
110
|
const verificationMethods = createDidVerificationMethod([VerificationMethods.Ed255192020, VerificationMethods.JWK, VerificationMethods.Ed255192018], [verificationKeys1, verificationKeys2, verificationKeys3])
|
|
105
|
-
const didPayload = MsgCreateDidPayload.transformPayload(createDidPayload(verificationMethods, [verificationKeys1, verificationKeys2, verificationKeys3]))
|
|
106
111
|
|
|
107
|
-
const
|
|
112
|
+
const payload = createDidPayload(verificationMethods, [verificationKeys1, verificationKeys2, verificationKeys3])
|
|
113
|
+
const { protobufVerificationMethod } = validateSpecCompliantPayload(payload)
|
|
114
|
+
|
|
115
|
+
const didSigners = await signer.checkDidSigners(protobufVerificationMethod)
|
|
108
116
|
|
|
109
117
|
expect(didSigners[VerificationMethods.Ed255192020]).toBe(EdDSASigner)
|
|
110
118
|
expect(didSigners[VerificationMethods.JWK]).toBe(EdDSASigner)
|
|
@@ -113,7 +121,7 @@ describe('CheqdSigningStargateClient', () => {
|
|
|
113
121
|
|
|
114
122
|
it('should throw for non-supported verification method', async () => {
|
|
115
123
|
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic)
|
|
116
|
-
const signer = await CheqdSigningStargateClient.connectWithSigner(
|
|
124
|
+
const signer = await CheqdSigningStargateClient.connectWithSigner(localnet.rpcUrl, wallet)
|
|
117
125
|
const verificationMethod: Partial<VerificationMethod> = {
|
|
118
126
|
id: nonExistingKeyId,
|
|
119
127
|
verificationMethodType: nonExistingVerificationMethod,
|
|
@@ -128,20 +136,36 @@ describe('CheqdSigningStargateClient', () => {
|
|
|
128
136
|
describe('signCreateDidTx', () => {
|
|
129
137
|
it('should sign a did tx with valid signature', async () => {
|
|
130
138
|
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic)
|
|
131
|
-
const signer = await CheqdSigningStargateClient.connectWithSigner(
|
|
139
|
+
const signer = await CheqdSigningStargateClient.connectWithSigner(localnet.rpcUrl, wallet)
|
|
132
140
|
const keyPair = createKeyPairBase64()
|
|
133
|
-
const verificationKeys = createVerificationKeys(keyPair, MethodSpecificIdAlgo.Base58, 'key-1'
|
|
141
|
+
const verificationKeys = createVerificationKeys(keyPair.publicKey, MethodSpecificIdAlgo.Base58, 'key-1')
|
|
134
142
|
const verificationMethods = createDidVerificationMethod([VerificationMethods.Ed255192020], [verificationKeys])
|
|
135
|
-
const didPayload =
|
|
143
|
+
const didPayload = createDidPayload(verificationMethods, [verificationKeys])
|
|
136
144
|
const signInputs: ISignInputs[] = [
|
|
137
145
|
{
|
|
138
|
-
verificationMethodId: didPayload.verificationMethod[0].id,
|
|
146
|
+
verificationMethodId: didPayload.verificationMethod![0].id,
|
|
139
147
|
privateKeyHex: toString(fromString(keyPair.privateKey, 'base64'), 'hex')
|
|
140
148
|
}
|
|
141
149
|
]
|
|
142
|
-
const
|
|
150
|
+
const { protobufVerificationMethod, protobufService } = validateSpecCompliantPayload(didPayload)
|
|
151
|
+
const versionId = v4()
|
|
152
|
+
const payload = MsgCreateDidDocPayload.fromPartial({
|
|
153
|
+
context: <string[]>didPayload?.['@context'],
|
|
154
|
+
id: didPayload.id,
|
|
155
|
+
controller: <string[]>didPayload.controller,
|
|
156
|
+
verificationMethod: protobufVerificationMethod,
|
|
157
|
+
authentication: <string[]>didPayload.authentication,
|
|
158
|
+
assertionMethod: <string[]>didPayload.assertionMethod,
|
|
159
|
+
capabilityInvocation: <string[]>didPayload.capabilityInvocation,
|
|
160
|
+
capabilityDelegation: <string[]>didPayload.capabilityDelegation,
|
|
161
|
+
keyAgreement: <string[]>didPayload.keyAgreement,
|
|
162
|
+
service: protobufService,
|
|
163
|
+
alsoKnownAs: <string[]>didPayload.alsoKnownAs,
|
|
164
|
+
versionId: versionId
|
|
165
|
+
})
|
|
166
|
+
const signInfos = await signer.signCreateDidTx(signInputs, payload)
|
|
143
167
|
const publicKeyRaw = fromString(keyPair.publicKey, 'base64')
|
|
144
|
-
const messageRaw = MsgCreateDidDocPayload.encode(
|
|
168
|
+
const messageRaw = MsgCreateDidDocPayload.encode(payload).finish()
|
|
145
169
|
|
|
146
170
|
const verified = verify(
|
|
147
171
|
publicKeyRaw,
|
package/tests/testutils.test.ts
CHANGED
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
import { CheqdNetwork, IKeyPair, IVerificationKeys, MethodSpecificIdAlgo, MsgCreateDidPayload, TMethodSpecificId, TVerificationKey, TVerificationKeyPrefix, VerificationMethodPayload, VerificationMethods } from "../src/types"
|
|
2
|
-
import { bases } from 'multiformats/basics'
|
|
3
|
-
import { base64ToBytes } from "did-jwt"
|
|
4
|
-
import { fromString, toString } from 'uint8arrays'
|
|
5
|
-
import { generateKeyPair, KeyPair } from '@stablelib/ed25519'
|
|
6
1
|
import { GasPrice } from "@cosmjs/stargate"
|
|
7
|
-
import { v4 } from 'uuid'
|
|
8
|
-
import { createHash } from 'crypto'
|
|
9
2
|
|
|
10
3
|
export const faucet = {
|
|
11
4
|
prefix: 'cheqd',
|
|
@@ -14,150 +7,12 @@ export const faucet = {
|
|
|
14
7
|
address: 'cheqd1rnr5jrt4exl0samwj0yegv99jeskl0hsxmcz96',
|
|
15
8
|
}
|
|
16
9
|
|
|
17
|
-
export const
|
|
10
|
+
export const localnet = {
|
|
18
11
|
network: 'testnet',
|
|
19
12
|
rpcUrl: 'http://localhost:26657',
|
|
20
13
|
gasPrice: GasPrice.fromString( `50${faucet.minimalDenom}` )
|
|
21
14
|
}
|
|
22
15
|
|
|
23
|
-
|
|
24
|
-
const MULTICODEC_ED25519_PUB_HEADER = new Uint8Array([0xed, 0x01]);
|
|
16
|
+
export const image_content = 'iVBORw0KGgoAAAANSUhEUgAAAQAAAAEAAQMAAABmvDolAAAAA1BMVEW10NBjBBbqAAAAH0lEQVRoge3BAQ0AAADCoPdPbQ43oAAAAAAAAAAAvg0hAAABmmDh1QAAAABJRU5ErkJggg' as const
|
|
25
17
|
|
|
26
|
-
|
|
27
|
-
*? General test utils. Look for src/utils.ts for stable utils exports.
|
|
28
|
-
*? Used for testing purposes.
|
|
29
|
-
** NOTE: The following utils are stable but subject to change at any given moment.
|
|
30
|
-
*/
|
|
31
|
-
export function createKeyPairRaw(): KeyPair {
|
|
32
|
-
return generateKeyPair()
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
*? General test utils. Look for src/utils.ts for stable utils exports.
|
|
37
|
-
*? Used for testing purposes.
|
|
38
|
-
** NOTE: The following utils are stable but subject to change at any given moment.
|
|
39
|
-
*/
|
|
40
|
-
export function createKeyPairBase64(): IKeyPair {
|
|
41
|
-
const keyPair = generateKeyPair()
|
|
42
|
-
return {
|
|
43
|
-
publicKey: toString(keyPair.publicKey, 'base64'),
|
|
44
|
-
privateKey: toString(keyPair.secretKey, 'base64'),
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
*? General test utils. Look for src/utils.ts for stable utils exports.
|
|
50
|
-
*? Used for testing purposes.
|
|
51
|
-
** NOTE: The following utils are stable but subject to change at any given moment.
|
|
52
|
-
*/
|
|
53
|
-
export function createKeyPairHex(): IKeyPair {
|
|
54
|
-
const keyPair = generateKeyPair()
|
|
55
|
-
return {
|
|
56
|
-
publicKey: toString(keyPair.publicKey, 'hex'),
|
|
57
|
-
privateKey: toString(keyPair.secretKey, 'hex'),
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
*? General test utils. Look for src/utils.ts for stable utils exports.
|
|
63
|
-
*? Used for testing purposes.
|
|
64
|
-
** NOTE: The following utils are stable but subject to change at any given moment.
|
|
65
|
-
*/
|
|
66
|
-
export function createVerificationKeys(keyPair: IKeyPair, algo: MethodSpecificIdAlgo, key: TVerificationKey<TVerificationKeyPrefix, number>, length: number = 32, network: CheqdNetwork = CheqdNetwork.Testnet): IVerificationKeys {
|
|
67
|
-
let methodSpecificId: TMethodSpecificId
|
|
68
|
-
let didUrl: IVerificationKeys['didUrl']
|
|
69
|
-
switch (algo) {
|
|
70
|
-
case MethodSpecificIdAlgo.Base58:
|
|
71
|
-
methodSpecificId = bases['base58btc'].encode(base64ToBytes(keyPair.publicKey))
|
|
72
|
-
didUrl = `did:cheqd:${network}:${(bases['base58btc'].encode((fromString(sha256(keyPair.publicKey))).slice(0,16))).slice(1)}`
|
|
73
|
-
return {
|
|
74
|
-
methodSpecificId,
|
|
75
|
-
didUrl,
|
|
76
|
-
keyId: `${didUrl}#${key}`,
|
|
77
|
-
publicKey: keyPair.publicKey,
|
|
78
|
-
}
|
|
79
|
-
case MethodSpecificIdAlgo.Uuid:
|
|
80
|
-
methodSpecificId = bases['base58btc'].encode(base64ToBytes(keyPair.publicKey))
|
|
81
|
-
didUrl = `did:cheqd:${network}:${v4()}`
|
|
82
|
-
return {
|
|
83
|
-
methodSpecificId,
|
|
84
|
-
didUrl,
|
|
85
|
-
keyId: `${didUrl}#${key}`,
|
|
86
|
-
publicKey: keyPair.publicKey,
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
*? General test utils. Look for src/utils.ts for stable utils exports.
|
|
93
|
-
*? Used for testing purposes.
|
|
94
|
-
** NOTE: The following utils are stable but subject to change at any given moment.
|
|
95
|
-
*/
|
|
96
|
-
export function createDidVerificationMethod(verificationMethodTypes: VerificationMethods[], verificationKeys: IVerificationKeys[]): VerificationMethodPayload[] {
|
|
97
|
-
return verificationMethodTypes.map((type, _) => {
|
|
98
|
-
switch (type) {
|
|
99
|
-
case VerificationMethods.Ed255192020:
|
|
100
|
-
return {
|
|
101
|
-
id: verificationKeys[_].keyId,
|
|
102
|
-
type,
|
|
103
|
-
controller: verificationKeys[_].didUrl,
|
|
104
|
-
publicKeyMultibase: _encodeMbKey(MULTICODEC_ED25519_PUB_HEADER, base64ToBytes(verificationKeys[_].publicKey))
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
case VerificationMethods.Ed255192018:
|
|
108
|
-
return {
|
|
109
|
-
id: verificationKeys[_].keyId,
|
|
110
|
-
type,
|
|
111
|
-
controller: verificationKeys[_].didUrl,
|
|
112
|
-
publicKeyBase58: verificationKeys[_].methodSpecificId.slice(1)
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
case VerificationMethods.JWK:
|
|
116
|
-
return {
|
|
117
|
-
id: verificationKeys[_].keyId,
|
|
118
|
-
type,
|
|
119
|
-
controller: verificationKeys[_].didUrl,
|
|
120
|
-
publicKeyJWK: {
|
|
121
|
-
crv: 'Ed25519',
|
|
122
|
-
kty: 'OKP',
|
|
123
|
-
x: toString( fromString( verificationKeys[_].publicKey, 'base64pad' ), 'base64url' )
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}) ?? []
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
*? General test utils. Look for src/utils.ts for stable utils exports.
|
|
132
|
-
*? Used for testing purposes.
|
|
133
|
-
** NOTE: The following utils are stable but subject to change at any given moment.
|
|
134
|
-
*/
|
|
135
|
-
export function createDidPayload(verificationMethods: VerificationMethodPayload[], verificationKeys: IVerificationKeys[]): MsgCreateDidPayload {
|
|
136
|
-
if (!verificationMethods || verificationMethods.length === 0)
|
|
137
|
-
throw new Error('No verification methods provided')
|
|
138
|
-
if (!verificationKeys || verificationKeys.length === 0)
|
|
139
|
-
throw new Error('No verification keys provided')
|
|
140
|
-
const did = verificationKeys[0].didUrl
|
|
141
|
-
return MsgCreateDidPayload.fromPartial(
|
|
142
|
-
{
|
|
143
|
-
id: did,
|
|
144
|
-
controller: verificationKeys.map(key => key.didUrl),
|
|
145
|
-
verificationMethod: verificationMethods,
|
|
146
|
-
authentication: verificationKeys.map(key => key.keyId),
|
|
147
|
-
versionId: v4()
|
|
148
|
-
}
|
|
149
|
-
)
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
function sha256(message: string) {
|
|
153
|
-
return createHash('sha256').update(message).digest('hex')
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function _encodeMbKey(header: any, key: Uint8Array) {
|
|
157
|
-
const mbKey = new Uint8Array(header.length + key.length);
|
|
158
|
-
|
|
159
|
-
mbKey.set(header);
|
|
160
|
-
mbKey.set(key, header.length);
|
|
161
|
-
|
|
162
|
-
return bases['base58btc'].encode(mbKey);
|
|
163
|
-
}
|
|
18
|
+
export const default_content = '<p>Test file content</p>'
|
package/tests/utils.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TImportableEd25519Key, createSignInputsFromImportableEd25519Key } from '../src/utils'
|
|
2
|
-
import { createDidVerificationMethod, createVerificationKeys, createKeyPairRaw } from '
|
|
2
|
+
import { createDidVerificationMethod, createVerificationKeys, createKeyPairRaw } from '../src/utils'
|
|
3
3
|
import { toString } from 'uint8arrays'
|
|
4
4
|
import { IKeyPair, MethodSpecificIdAlgo, VerificationMethods } from '../src/types'
|
|
5
5
|
|
|
@@ -17,7 +17,7 @@ describe('createSignInputsFromImportableEd25519Key', () => {
|
|
|
17
17
|
privateKey: toString(keyPair.secretKey, 'base64'),
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
const verificationKeys = createVerificationKeys(keyPairBase64, MethodSpecificIdAlgo.Base58, 'key-1'
|
|
20
|
+
const verificationKeys = createVerificationKeys(keyPairBase64.publicKey, MethodSpecificIdAlgo.Base58, 'key-1')
|
|
21
21
|
const verificationMethod = createDidVerificationMethod([VerificationMethods.Ed255192020], [verificationKeys])
|
|
22
22
|
const signInput = createSignInputsFromImportableEd25519Key(importableEd25519Key, verificationMethod)
|
|
23
23
|
|
|
@@ -37,7 +37,7 @@ describe('createSignInputsFromImportableEd25519Key', () => {
|
|
|
37
37
|
privateKey: toString(keyPair.secretKey, 'base64'),
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
const verificationKeys = createVerificationKeys(keyPairBase64, MethodSpecificIdAlgo.Base58, 'key-1'
|
|
40
|
+
const verificationKeys = createVerificationKeys(keyPairBase64.publicKey, MethodSpecificIdAlgo.Base58, 'key-1')
|
|
41
41
|
const verificationMethod = createDidVerificationMethod([VerificationMethods.Ed255192018], [verificationKeys])
|
|
42
42
|
const signInput = createSignInputsFromImportableEd25519Key(importableEd25519Key, verificationMethod)
|
|
43
43
|
|
|
@@ -57,7 +57,7 @@ describe('createSignInputsFromImportableEd25519Key', () => {
|
|
|
57
57
|
privateKey: toString(keyPair.secretKey, 'base64'),
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
const verificationKeys = createVerificationKeys(keyPairBase64, MethodSpecificIdAlgo.Base58, 'key-1'
|
|
60
|
+
const verificationKeys = createVerificationKeys(keyPairBase64.publicKey, MethodSpecificIdAlgo.Base58, 'key-1')
|
|
61
61
|
const verificationMethod = createDidVerificationMethod([VerificationMethods.JWK], [verificationKeys])
|
|
62
62
|
const signInput = createSignInputsFromImportableEd25519Key(importableEd25519Key, verificationMethod)
|
|
63
63
|
|