@cheqd/sdk 1.4.0 → 1.5.0-develop.2
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/.github/workflows/build.yml +3 -2
- package/.github/workflows/dispatch.yml +7 -1
- package/.github/workflows/test.yml +66 -0
- package/CHANGELOG.md +22 -0
- package/build/index.d.ts +1 -1
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1 -3
- package/build/index.js.map +1 -1
- package/build/modules/did.d.ts +41 -28
- package/build/modules/did.d.ts.map +1 -1
- package/build/modules/did.js +78 -30
- package/build/modules/did.js.map +1 -1
- package/build/modules/resource.d.ts +4 -4
- package/build/modules/resource.d.ts.map +1 -1
- package/build/modules/resource.js +14 -33
- package/build/modules/resource.js.map +1 -1
- package/build/signer.d.ts +4 -4
- package/build/signer.d.ts.map +1 -1
- package/build/signer.js +27 -16
- package/build/signer.js.map +1 -1
- package/build/types.d.ts +239 -1
- package/build/types.d.ts.map +1 -1
- package/build/types.js +127 -2
- package/build/types.js.map +1 -1
- package/build/utils.d.ts +5 -17
- package/build/utils.d.ts.map +1 -1
- package/build/utils.js +52 -60
- package/build/utils.js.map +1 -1
- package/docker/Dockerfile +55 -0
- package/docker/entrypoint.sh +58 -0
- package/docker/localnet/build-latest.env +7 -0
- package/docker/localnet/container-env/observer-0.env +13 -0
- package/docker/localnet/container-env/seed-0.env +17 -0
- package/docker/localnet/container-env/validator-0.env +13 -0
- package/docker/localnet/container-env/validator-1.env +13 -0
- package/docker/localnet/container-env/validator-2.env +13 -0
- package/docker/localnet/container-env/validator-3.env +13 -0
- package/docker/localnet/docker-compose.yml +281 -0
- package/docker/localnet/gen-network-config.sh +259 -0
- package/docker/localnet/import-keys.sh +31 -0
- package/package.json +2 -2
- package/src/index.ts +1 -3
- package/src/modules/did.ts +118 -44
- package/src/modules/resource.ts +10 -33
- package/src/signer.ts +29 -16
- package/src/types.ts +160 -2
- package/src/utils.ts +67 -70
- package/tests/modules/did.test.ts +69 -9
- package/tests/modules/resource.test.ts +6 -4
- package/tests/signer.test.ts +25 -23
- package/tests/testutils.test.ts +43 -25
- package/tests/utils.test.ts +22 -2
package/tests/testutils.test.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import { MsgCreateDidPayload } from "
|
|
2
|
-
import { CheqdNetwork, IKeyPair, IVerificationKeys, MethodSpecificIdAlgo, TMethodSpecificId, TVerificationKey, TVerificationKeyPrefix, VerificationMethods } from "../src/types"
|
|
1
|
+
import { CheqdNetwork, IKeyPair, IVerificationKeys, MethodSpecificIdAlgo, MsgCreateDidPayload, TMethodSpecificId, TVerificationKey, TVerificationKeyPrefix, VerificationMethodPayload, VerificationMethods } from "../src/types"
|
|
3
2
|
import { bases } from 'multiformats/basics'
|
|
4
3
|
import { base64ToBytes } from "did-jwt"
|
|
5
4
|
import { fromString, toString } from 'uint8arrays'
|
|
6
5
|
import { generateKeyPair, KeyPair } from '@stablelib/ed25519'
|
|
7
6
|
import { GasPrice } from "@cosmjs/stargate"
|
|
8
7
|
import { v4 } from 'uuid'
|
|
9
|
-
import {
|
|
10
|
-
import { parseToKeyValuePair } from '../src/utils'
|
|
8
|
+
import { createHash } from 'crypto'
|
|
11
9
|
|
|
12
10
|
export const faucet = {
|
|
13
11
|
prefix: 'cheqd',
|
|
@@ -18,10 +16,13 @@ export const faucet = {
|
|
|
18
16
|
|
|
19
17
|
export const exampleCheqdNetwork = {
|
|
20
18
|
network: 'testnet',
|
|
21
|
-
rpcUrl: '
|
|
22
|
-
gasPrice: GasPrice.fromString( `
|
|
19
|
+
rpcUrl: 'http://localhost:26657',
|
|
20
|
+
gasPrice: GasPrice.fromString( `50${faucet.minimalDenom}` )
|
|
23
21
|
}
|
|
24
22
|
|
|
23
|
+
// multicodec ed25519-pub header as varint
|
|
24
|
+
const MULTICODEC_ED25519_PUB_HEADER = new Uint8Array([0xed, 0x01]);
|
|
25
|
+
|
|
25
26
|
/**
|
|
26
27
|
*? General test utils. Look for src/utils.ts for stable utils exports.
|
|
27
28
|
*? Used for testing purposes.
|
|
@@ -68,7 +69,7 @@ export function createVerificationKeys(keyPair: IKeyPair, algo: MethodSpecificId
|
|
|
68
69
|
switch (algo) {
|
|
69
70
|
case MethodSpecificIdAlgo.Base58:
|
|
70
71
|
methodSpecificId = bases['base58btc'].encode(base64ToBytes(keyPair.publicKey))
|
|
71
|
-
didUrl = `did:cheqd:${network}:${
|
|
72
|
+
didUrl = `did:cheqd:${network}:${(bases['base58btc'].encode((fromString(sha256(keyPair.publicKey))).slice(0,16))).slice(1)}`
|
|
72
73
|
return {
|
|
73
74
|
methodSpecificId,
|
|
74
75
|
didUrl,
|
|
@@ -92,31 +93,35 @@ export function createVerificationKeys(keyPair: IKeyPair, algo: MethodSpecificId
|
|
|
92
93
|
*? Used for testing purposes.
|
|
93
94
|
** NOTE: The following utils are stable but subject to change at any given moment.
|
|
94
95
|
*/
|
|
95
|
-
export function createDidVerificationMethod(verificationMethodTypes: VerificationMethods[], verificationKeys: IVerificationKeys[]):
|
|
96
|
+
export function createDidVerificationMethod(verificationMethodTypes: VerificationMethods[], verificationKeys: IVerificationKeys[]): VerificationMethodPayload[] {
|
|
96
97
|
return verificationMethodTypes.map((type, _) => {
|
|
97
98
|
switch (type) {
|
|
98
|
-
case VerificationMethods.
|
|
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:
|
|
99
108
|
return {
|
|
100
109
|
id: verificationKeys[_].keyId,
|
|
101
|
-
type
|
|
110
|
+
type,
|
|
102
111
|
controller: verificationKeys[_].didUrl,
|
|
103
|
-
|
|
104
|
-
publicKeyJwk: []
|
|
112
|
+
publicKeyBase58: verificationKeys[_].methodSpecificId.slice(1)
|
|
105
113
|
}
|
|
106
114
|
|
|
107
115
|
case VerificationMethods.JWK:
|
|
108
116
|
return {
|
|
109
117
|
id: verificationKeys[_].keyId,
|
|
110
|
-
type
|
|
118
|
+
type,
|
|
111
119
|
controller: verificationKeys[_].didUrl,
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
),
|
|
119
|
-
publicKeyMultibase: ''
|
|
120
|
+
publicKeyJWK: {
|
|
121
|
+
crv: 'Ed25519',
|
|
122
|
+
kty: 'OKP',
|
|
123
|
+
x: toString( fromString( verificationKeys[_].publicKey, 'base64pad' ), 'base64url' )
|
|
124
|
+
}
|
|
120
125
|
}
|
|
121
126
|
}
|
|
122
127
|
}) ?? []
|
|
@@ -127,19 +132,32 @@ export function createDidVerificationMethod(verificationMethodTypes: Verificatio
|
|
|
127
132
|
*? Used for testing purposes.
|
|
128
133
|
** NOTE: The following utils are stable but subject to change at any given moment.
|
|
129
134
|
*/
|
|
130
|
-
export function createDidPayload(verificationMethods:
|
|
135
|
+
export function createDidPayload(verificationMethods: VerificationMethodPayload[], verificationKeys: IVerificationKeys[]): MsgCreateDidPayload {
|
|
131
136
|
if (!verificationMethods || verificationMethods.length === 0)
|
|
132
137
|
throw new Error('No verification methods provided')
|
|
133
138
|
if (!verificationKeys || verificationKeys.length === 0)
|
|
134
139
|
throw new Error('No verification keys provided')
|
|
135
|
-
|
|
136
140
|
const did = verificationKeys[0].didUrl
|
|
137
141
|
return MsgCreateDidPayload.fromPartial(
|
|
138
142
|
{
|
|
139
143
|
id: did,
|
|
140
144
|
controller: verificationKeys.map(key => key.didUrl),
|
|
141
145
|
verificationMethod: verificationMethods,
|
|
142
|
-
authentication: verificationKeys.map(key => key.keyId)
|
|
146
|
+
authentication: verificationKeys.map(key => key.keyId),
|
|
147
|
+
versionId: v4()
|
|
143
148
|
}
|
|
144
149
|
)
|
|
145
|
-
}
|
|
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
|
+
}
|
package/tests/utils.test.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { toString } from 'uint8arrays'
|
|
|
4
4
|
import { IKeyPair, MethodSpecificIdAlgo, VerificationMethods } from '../src/types'
|
|
5
5
|
|
|
6
6
|
describe('createSignInputsFromImportableEd25519Key', () => {
|
|
7
|
-
it('should create a sign input from an importable ed25519 key', async () => {
|
|
7
|
+
it('should create a sign input from an importable ed25519 key 2020', async () => {
|
|
8
8
|
const keyPair = createKeyPairRaw()
|
|
9
9
|
const importableEd25519Key: TImportableEd25519Key = {
|
|
10
10
|
publicKeyHex: toString(keyPair.publicKey, 'hex'),
|
|
@@ -18,7 +18,27 @@ describe('createSignInputsFromImportableEd25519Key', () => {
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
const verificationKeys = createVerificationKeys(keyPairBase64, MethodSpecificIdAlgo.Base58, 'key-1', 16)
|
|
21
|
-
const verificationMethod = createDidVerificationMethod([VerificationMethods.
|
|
21
|
+
const verificationMethod = createDidVerificationMethod([VerificationMethods.Ed255192020], [verificationKeys])
|
|
22
|
+
const signInput = createSignInputsFromImportableEd25519Key(importableEd25519Key, verificationMethod)
|
|
23
|
+
|
|
24
|
+
expect(signInput).toEqual({ verificationMethodId: verificationKeys.keyId, privateKeyHex: importableEd25519Key.privateKeyHex })
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('should create a sign input from an importable ed25519 key 2018', async () => {
|
|
28
|
+
const keyPair = createKeyPairRaw()
|
|
29
|
+
const importableEd25519Key: TImportableEd25519Key = {
|
|
30
|
+
publicKeyHex: toString(keyPair.publicKey, 'hex'),
|
|
31
|
+
privateKeyHex: toString(keyPair.secretKey, 'hex'),
|
|
32
|
+
kid: toString(keyPair.publicKey, 'hex'),
|
|
33
|
+
type: 'Ed25519'
|
|
34
|
+
}
|
|
35
|
+
const keyPairBase64: IKeyPair = {
|
|
36
|
+
publicKey: toString(keyPair.publicKey, 'base64'),
|
|
37
|
+
privateKey: toString(keyPair.secretKey, 'base64'),
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const verificationKeys = createVerificationKeys(keyPairBase64, MethodSpecificIdAlgo.Base58, 'key-1', 16)
|
|
41
|
+
const verificationMethod = createDidVerificationMethod([VerificationMethods.Ed255192018], [verificationKeys])
|
|
22
42
|
const signInput = createSignInputsFromImportableEd25519Key(importableEd25519Key, verificationMethod)
|
|
23
43
|
|
|
24
44
|
expect(signInput).toEqual({ verificationMethodId: verificationKeys.keyId, privateKeyHex: importableEd25519Key.privateKeyHex })
|