@cheqd/sdk 3.0.2-develop.1 → 3.0.2-develop.3

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 (56) hide show
  1. package/package.json +9 -3
  2. package/.github/ISSUE_TEMPLATE/bug-report.yml +0 -74
  3. package/.github/ISSUE_TEMPLATE/config.yml +0 -14
  4. package/.github/ISSUE_TEMPLATE/feature-request.yaml +0 -27
  5. package/.github/dependabot.yml +0 -43
  6. package/.github/linters/.commitlint.rules.cjs +0 -37
  7. package/.github/linters/.eslintrc.json +0 -18
  8. package/.github/linters/.markdown-lint.yml +0 -139
  9. package/.github/linters/mlc_config.json +0 -13
  10. package/.github/workflows/build.yml +0 -26
  11. package/.github/workflows/cleanup-actions.yml +0 -45
  12. package/.github/workflows/cleanup-cache-automatic.yml +0 -24
  13. package/.github/workflows/cleanup-cache-manual.yml +0 -21
  14. package/.github/workflows/codeql.yml +0 -40
  15. package/.github/workflows/dispatch.yml +0 -30
  16. package/.github/workflows/lint.yml +0 -51
  17. package/.github/workflows/pull-request.yml +0 -48
  18. package/.github/workflows/release.yml +0 -42
  19. package/.github/workflows/test.yml +0 -66
  20. package/.releaserc.json +0 -61
  21. package/CHANGELOG.md +0 -320
  22. package/CODE_OF_CONDUCT.md +0 -81
  23. package/NOTICE.md +0 -10
  24. package/SECURITY.md +0 -12
  25. package/docker/Dockerfile +0 -55
  26. package/docker/entrypoint.sh +0 -58
  27. package/docker/localnet/build-latest.env +0 -7
  28. package/docker/localnet/container-env/observer-0.env +0 -13
  29. package/docker/localnet/container-env/seed-0.env +0 -17
  30. package/docker/localnet/container-env/validator-0.env +0 -13
  31. package/docker/localnet/container-env/validator-1.env +0 -13
  32. package/docker/localnet/container-env/validator-2.env +0 -13
  33. package/docker/localnet/container-env/validator-3.env +0 -13
  34. package/docker/localnet/docker-compose.yml +0 -281
  35. package/docker/localnet/gen-network-config.sh +0 -259
  36. package/docker/localnet/import-keys.sh +0 -31
  37. package/jest.config.cjs +0 -20
  38. package/src/index.ts +0 -192
  39. package/src/modules/_.ts +0 -61
  40. package/src/modules/did.ts +0 -601
  41. package/src/modules/resource.ts +0 -247
  42. package/src/querier.ts +0 -36
  43. package/src/registry.ts +0 -13
  44. package/src/signer.ts +0 -297
  45. package/src/types.ts +0 -110
  46. package/src/utils.ts +0 -246
  47. package/tests/index.test.ts +0 -155
  48. package/tests/modules/did.test.ts +0 -985
  49. package/tests/modules/resource.test.ts +0 -991
  50. package/tests/signer.test.ts +0 -202
  51. package/tests/testutils.test.ts +0 -29
  52. package/tests/utils.test.ts +0 -77
  53. package/tsconfig.cjs.json +0 -8
  54. package/tsconfig.esm.json +0 -8
  55. package/tsconfig.json +0 -84
  56. package/tsconfig.types.json +0 -10
package/src/utils.ts DELETED
@@ -1,246 +0,0 @@
1
- import {
2
- IKeyPair,
3
- IKeyValuePair,
4
- ISignInputs,
5
- VerificationMethods,
6
- TMethodSpecificId,
7
- MethodSpecificIdAlgo,
8
- TVerificationKey,
9
- TVerificationKeyPrefix,
10
- CheqdNetwork,
11
- IVerificationKeys,
12
- VerificationMethod,
13
- DIDDocument,
14
- SpecValidationResult,
15
- JsonWebKey,
16
- } from "./types.js"
17
- import {
18
- fromString,
19
- toString
20
- } from 'uint8arrays'
21
- import { bases } from "multiformats/basics"
22
- import { base64ToBytes } from "did-jwt"
23
- import {
24
- generateKeyPair,
25
- generateKeyPairFromSeed,
26
- KeyPair
27
- } from '@stablelib/ed25519'
28
- import { sha256 } from '@cosmjs/crypto'
29
- import { v4 } from 'uuid'
30
- import {
31
- VerificationMethod as ProtoVerificationMethod,
32
- Service as ProtoService,
33
- } from "@cheqd/ts-proto/cheqd/did/v2/index.js"
34
-
35
- export type TImportableEd25519Key = {
36
- publicKeyHex: string
37
- privateKeyHex: string
38
- kid: string
39
- type: "Ed25519"
40
- }
41
-
42
- const MULTICODEC_ED25519_HEADER = new Uint8Array([0xed, 0x01]);
43
-
44
- export function isEqualKeyValuePair(kv1: IKeyValuePair[], kv2: IKeyValuePair[]): boolean {
45
- return kv1.every((item, index) => item.key === kv2[index].key && item.value === kv2[index].value)
46
- }
47
-
48
- export function createSignInputsFromImportableEd25519Key(key: TImportableEd25519Key, verificationMethod: VerificationMethod[]): ISignInputs {
49
- if (verificationMethod?.length === 0) throw new Error('No verification methods provided')
50
-
51
- const publicKey = fromString(key.publicKeyHex, 'hex')
52
-
53
- for(const method of verificationMethod) {
54
- switch (method?.type) {
55
- case VerificationMethods.Ed255192020:
56
- const publicKeyMultibase = toMultibaseRaw(publicKey)
57
- if (method.publicKeyMultibase === publicKeyMultibase) {
58
- return {
59
- verificationMethodId: method.id,
60
- privateKeyHex: key.privateKeyHex
61
- }
62
- }
63
- case VerificationMethods.Ed255192018:
64
- const publicKeyBase58 = bases['base58btc'].encode(publicKey).slice(1)
65
- if (method.publicKeyBase58 === publicKeyBase58) {
66
- return {
67
- verificationMethodId: method.id,
68
- privateKeyHex: key.privateKeyHex
69
- }
70
- }
71
- case VerificationMethods.JWK:
72
- const publicKeyJwk: JsonWebKey = {
73
- crv: 'Ed25519',
74
- kty: 'OKP',
75
- x: toString( publicKey, 'base64url' )
76
- }
77
- if (JSON.stringify(method.publicKeyJwk) === JSON.stringify(publicKeyJwk)) {
78
- return {
79
- verificationMethodId: method.id,
80
- privateKeyHex: key.privateKeyHex
81
- }
82
- }
83
- }
84
- }
85
-
86
- throw new Error('No verification method type provided')
87
- }
88
-
89
- export function createKeyPairRaw(seed?: string): KeyPair {
90
- return seed ? generateKeyPairFromSeed(fromString(seed)) : generateKeyPair()
91
- }
92
-
93
- export function createKeyPairBase64(seed?: string): IKeyPair {
94
- const keyPair = seed ? generateKeyPairFromSeed(fromString(seed)) : generateKeyPair()
95
- return {
96
- publicKey: toString(keyPair.publicKey, 'base64'),
97
- privateKey: toString(keyPair.secretKey, 'base64'),
98
- }
99
- }
100
-
101
- export function createKeyPairHex(seed?: string): IKeyPair {
102
- const keyPair = createKeyPairRaw(seed)
103
- return {
104
- publicKey: toString(keyPair.publicKey, 'hex'),
105
- privateKey: toString(keyPair.secretKey, 'hex'),
106
- }
107
- }
108
-
109
- export function createVerificationKeys(publicKey: string, algo: MethodSpecificIdAlgo, key: TVerificationKey<TVerificationKeyPrefix, number>, network: CheqdNetwork = CheqdNetwork.Testnet): IVerificationKeys {
110
- let methodSpecificId: TMethodSpecificId
111
- let didUrl: IVerificationKeys['didUrl']
112
-
113
- publicKey = publicKey.length == 43 ? publicKey : toString(fromString(publicKey, 'hex'), 'base64')
114
- switch (algo) {
115
- case MethodSpecificIdAlgo.Base58:
116
- methodSpecificId = bases['base58btc'].encode(base64ToBytes(publicKey))
117
- didUrl = `did:cheqd:${network}:${(bases['base58btc'].encode((sha256(base64ToBytes(publicKey))).slice(0,16))).slice(1)}`
118
- return {
119
- methodSpecificId,
120
- didUrl,
121
- keyId: `${didUrl}#${key}`,
122
- publicKey,
123
- }
124
- case MethodSpecificIdAlgo.Uuid:
125
- methodSpecificId = bases['base58btc'].encode(base64ToBytes(publicKey))
126
- didUrl = `did:cheqd:${network}:${v4()}`
127
- return {
128
- methodSpecificId,
129
- didUrl,
130
- keyId: `${didUrl}#${key}`,
131
- publicKey,
132
- }
133
- }
134
- }
135
-
136
- export function createDidVerificationMethod(verificationMethodTypes: VerificationMethods[], verificationKeys: IVerificationKeys[]): VerificationMethod[] {
137
- return verificationMethodTypes.map((type, _) => {
138
- switch (type) {
139
- case VerificationMethods.Ed255192020:
140
- return {
141
- id: verificationKeys[_].keyId,
142
- type,
143
- controller: verificationKeys[_].didUrl,
144
- publicKeyMultibase: toMultibaseRaw(base64ToBytes(verificationKeys[_].publicKey))
145
- } as VerificationMethod
146
- case VerificationMethods.Ed255192018:
147
- return {
148
- id: verificationKeys[_].keyId,
149
- type,
150
- controller: verificationKeys[_].didUrl,
151
- publicKeyBase58: verificationKeys[_].methodSpecificId.slice(1)
152
- } as VerificationMethod
153
- case VerificationMethods.JWK:
154
- return {
155
- id: verificationKeys[_].keyId,
156
- type,
157
- controller: verificationKeys[_].didUrl,
158
- publicKeyJwk: {
159
- crv: 'Ed25519',
160
- kty: 'OKP',
161
- x: toString( fromString( verificationKeys[_].publicKey, 'base64pad' ), 'base64url' )
162
- }
163
- } as VerificationMethod
164
- }
165
- }) ?? []
166
- }
167
-
168
- export function createDidPayload(verificationMethods: VerificationMethod[], verificationKeys: IVerificationKeys[]): DIDDocument {
169
- if (!verificationMethods || verificationMethods.length === 0)
170
- throw new Error('No verification methods provided')
171
- if (!verificationKeys || verificationKeys.length === 0)
172
- throw new Error('No verification keys provided')
173
-
174
- const did = verificationKeys[0].didUrl
175
- return {
176
- id: did,
177
- controller: verificationKeys.map(key => key.didUrl),
178
- verificationMethod: verificationMethods,
179
- authentication: verificationKeys.map(key => key.keyId),
180
- } as DIDDocument
181
- }
182
-
183
- export function validateSpecCompliantPayload(didDocument: DIDDocument): SpecValidationResult {
184
- // id is required, validated on both compile and runtime
185
- if (!didDocument?.id) return { valid: false, error: 'id is required' }
186
-
187
- // verificationMethod is required
188
- if (!didDocument?.verificationMethod) return { valid: false, error: 'verificationMethod is required' }
189
-
190
- // verificationMethod must be an array
191
- if (!Array.isArray(didDocument?.verificationMethod)) return { valid: false, error: 'verificationMethod must be an array' }
192
-
193
- // verificationMethod types must be supported
194
- const protoVerificationMethod = didDocument.verificationMethod.map((vm) => {
195
- switch (vm?.type) {
196
- case VerificationMethods.Ed255192020:
197
- if (!vm.publicKeyMultibase) throw new Error('publicKeyMultibase is required')
198
-
199
- return ProtoVerificationMethod.fromPartial({
200
- id: vm.id,
201
- controller: vm.controller,
202
- verificationMethodType: VerificationMethods.Ed255192020,
203
- verificationMaterial: vm.publicKeyMultibase,
204
- })
205
- case VerificationMethods.JWK:
206
- if (!vm.publicKeyJwk) throw new Error('publicKeyJwk is required')
207
-
208
- return ProtoVerificationMethod.fromPartial({
209
- id: vm.id,
210
- controller: vm.controller,
211
- verificationMethodType: VerificationMethods.JWK,
212
- verificationMaterial: JSON.stringify(vm.publicKeyJwk),
213
- })
214
- case VerificationMethods.Ed255192018:
215
- if (!vm.publicKeyBase58) throw new Error('publicKeyBase58 is required')
216
-
217
- return ProtoVerificationMethod.fromPartial({
218
- id: vm.id,
219
- controller: vm.controller,
220
- verificationMethodType: VerificationMethods.Ed255192018,
221
- verificationMaterial: vm.publicKeyBase58,
222
- })
223
- default:
224
- throw new Error(`Unsupported verificationMethod type: ${vm?.type}`)
225
- }
226
- })
227
-
228
- const protoService = didDocument?.service?.map((s) => {
229
- return ProtoService.fromPartial({
230
- id: s?.id,
231
- serviceType: s?.type,
232
- serviceEndpoint: <string[]>s?.serviceEndpoint,
233
- })
234
- })
235
-
236
- return { valid: true, protobufVerificationMethod: protoVerificationMethod, protobufService: protoService }
237
- }
238
-
239
- function toMultibaseRaw(key: Uint8Array) {
240
- const multibase = new Uint8Array(MULTICODEC_ED25519_HEADER.length + key.length);
241
-
242
- multibase.set(MULTICODEC_ED25519_HEADER);
243
- multibase.set(key, MULTICODEC_ED25519_HEADER.length);
244
-
245
- return bases['base58btc'].encode(multibase);
246
- }
@@ -1,155 +0,0 @@
1
- import {
2
- DirectSecp256k1HdWallet,
3
- GeneratedType
4
- } from '@cosmjs/proto-signing'
5
- import {
6
- createCheqdSDK,
7
- DIDModule,
8
- ICheqdSDKOptions,
9
- ResourceModule
10
- } from '../src/index'
11
- import {
12
- localnet,
13
- faucet
14
- } from './testutils.test'
15
- import { AbstractCheqdSDKModule } from '../src/modules/_'
16
- import { CheqdSigningStargateClient } from '../src/signer'
17
- import { createDefaultCheqdRegistry } from '../src/registry'
18
- import { CheqdQuerier } from '../src/querier'
19
- import {
20
- setupDidExtension,
21
- DidExtension,
22
- defaultDidExtensionKey
23
- } from '../src/modules/did';
24
- import { QueryExtensionSetup } from '../src/types'
25
- import {
26
- setupResourceExtension,
27
- ResourceExtension,
28
- defaultResourceExtensionKey
29
- } from '../src/modules/resource'
30
- import { jest } from '@jest/globals'
31
-
32
- describe(
33
- 'CheqdSDK', () => {
34
- describe('constructor', () => {
35
- it('can be instantiated with modules', async () => {
36
- const options = {
37
- modules: [DIDModule as unknown as AbstractCheqdSDKModule],
38
- rpcUrl: localnet.rpcUrl,
39
- wallet: await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic)
40
- } as ICheqdSDKOptions
41
- const cheqdSDK = await createCheqdSDK(options)
42
-
43
- const sdkMethods = Object.keys(cheqdSDK.methods)
44
- const testSigner = await CheqdSigningStargateClient.connectWithSigner(options.rpcUrl, options.wallet)
45
- const testQuerier = await CheqdQuerier.connectWithExtension(options.rpcUrl, setupDidExtension) as CheqdQuerier & DidExtension
46
- const moduleMethods = Object.keys(new DIDModule(testSigner, testQuerier).methods)
47
-
48
- moduleMethods.forEach((method) => {
49
- expect(sdkMethods).toContain(method)
50
- })
51
- })
52
-
53
- it('should use module methods', async () => {
54
- const rpcUrl = localnet.rpcUrl
55
- const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic)
56
-
57
- class TestModule extends AbstractCheqdSDKModule {
58
- registryTypes: Iterable<[string, GeneratedType]> = []
59
- methods = {
60
- doSomething: this.doSomething.bind(this)
61
- }
62
- constructor(signer: CheqdSigningStargateClient, querier: CheqdQuerier) {
63
- super(signer, querier)
64
- }
65
- public getRegistryTypes(): Iterable<[string, GeneratedType]> {
66
- return TestModule.registryTypes
67
- }
68
- public getQuerierExtensionSetup(): QueryExtensionSetup<{}> {
69
- return () => ({})
70
- }
71
- async doSomething(): Promise<string> {
72
- return 'did something'
73
- }
74
- }
75
- const options = {
76
- modules: [TestModule as unknown as AbstractCheqdSDKModule],
77
- rpcUrl,
78
- wallet
79
- } as ICheqdSDKOptions
80
-
81
- const cheqdSDK = await createCheqdSDK(options)
82
-
83
- //@ts-ignore
84
- const doSomething = await cheqdSDK.doSomething()
85
- expect(doSomething).toBe('did something')
86
-
87
- const spy = jest.spyOn(cheqdSDK.methods, 'doSomething')
88
- //@ts-ignore
89
- await cheqdSDK.doSomething()
90
- expect(spy).toHaveBeenCalled()
91
- })
92
-
93
- it('should instantiate registry from passed modules', async () => {
94
- const options = {
95
- modules: [DIDModule as unknown as AbstractCheqdSDKModule],
96
- rpcUrl: localnet.rpcUrl,
97
- wallet: await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic)
98
- } as ICheqdSDKOptions
99
- const cheqdSDK = await createCheqdSDK(options)
100
-
101
- const didRegistryTypes = DIDModule.registryTypes
102
- const cheqdRegistry = createDefaultCheqdRegistry(didRegistryTypes)
103
-
104
- expect(cheqdSDK.signer.registry).toStrictEqual(cheqdRegistry)
105
- })
106
-
107
- it('should instantiate registry from multiple passed modules', async () => {
108
- const options = {
109
- modules: [DIDModule as unknown as AbstractCheqdSDKModule, ResourceModule as unknown as AbstractCheqdSDKModule],
110
- rpcUrl: localnet.rpcUrl,
111
- wallet: await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic)
112
- } as ICheqdSDKOptions
113
- const cheqdSDK = await createCheqdSDK(options)
114
-
115
- const didRegistryTypes = DIDModule.registryTypes
116
- const resourceRegistryTypes = ResourceModule.registryTypes
117
- const cheqdRegistry = createDefaultCheqdRegistry([...didRegistryTypes, ...resourceRegistryTypes])
118
-
119
- expect(cheqdSDK.signer.registry).toStrictEqual(cheqdRegistry)
120
- })
121
-
122
- it('should instantiate querier extension from passed modules', async () => {
123
- const options = {
124
- modules: [DIDModule as unknown as AbstractCheqdSDKModule],
125
- rpcUrl: localnet.rpcUrl,
126
- wallet: await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic)
127
- } as ICheqdSDKOptions
128
- const cheqdSDK = await createCheqdSDK(options)
129
-
130
- const querier = await CheqdQuerier.connectWithExtension(options.rpcUrl, setupDidExtension) as CheqdQuerier & DidExtension
131
-
132
- // we need to stringify the querier extension because it's a proxy object
133
- // and the equality check will fail
134
- expect(JSON.stringify(cheqdSDK.querier[defaultDidExtensionKey])).toStrictEqual(JSON.stringify(querier[defaultDidExtensionKey]))
135
- })
136
-
137
- it('should instantiate querier extension from multiple passed modules', async () => {
138
- const options = {
139
- modules: [DIDModule as unknown as AbstractCheqdSDKModule, ResourceModule as unknown as AbstractCheqdSDKModule],
140
- rpcUrl: localnet.rpcUrl,
141
- wallet: await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic)
142
- } as ICheqdSDKOptions
143
- const cheqdSDK = await createCheqdSDK(options)
144
-
145
- const didQuerier = await CheqdQuerier.connectWithExtension(options.rpcUrl, setupDidExtension) as CheqdQuerier & DidExtension
146
- const resourceQuerier = await CheqdQuerier.connectWithExtension(options.rpcUrl, setupResourceExtension) as CheqdQuerier & ResourceExtension
147
-
148
- // we need to stringify the querier extension because it's a proxy object
149
- // and the equality check will fail
150
- expect(JSON.stringify(cheqdSDK.querier[defaultDidExtensionKey])).toStrictEqual(JSON.stringify(didQuerier[defaultDidExtensionKey]))
151
- expect(JSON.stringify(cheqdSDK.querier[defaultResourceExtensionKey])).toStrictEqual(JSON.stringify(resourceQuerier[defaultResourceExtensionKey]))
152
- })
153
- })
154
- }
155
- )