@cheqd/sdk 1.5.0-develop.3 → 2.0.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/CHANGELOG.md +18 -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 +172 -14
- package/build/modules/did.js.map +1 -1
- package/build/modules/resource.d.ts +24 -1
- package/build/modules/resource.d.ts.map +1 -1
- package/build/modules/resource.js +63 -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 +4 -2
- package/src/index.ts +5 -2
- package/src/modules/did.ts +204 -19
- package/src/modules/resource.ts +75 -4
- 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
package/build/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createDidPayload = exports.createDidVerificationMethod = exports.createVerificationKeys = exports.createKeyPairHex = exports.createKeyPairBase64 = exports.createKeyPairRaw = exports.createSignInputsFromImportableEd25519Key = exports.isEqualKeyValuePair = void 0;
|
|
3
|
+
exports.validateSpecCompliantPayload = exports.createDidPayload = exports.createDidVerificationMethod = exports.createVerificationKeys = exports.createKeyPairHex = exports.createKeyPairBase64 = exports.createKeyPairRaw = exports.createSignInputsFromImportableEd25519Key = exports.isEqualKeyValuePair = void 0;
|
|
4
4
|
const types_1 = require("./types");
|
|
5
5
|
const uint8arrays_1 = require("uint8arrays");
|
|
6
6
|
const basics_1 = require("multiformats/basics");
|
|
@@ -8,8 +8,8 @@ const did_jwt_1 = require("did-jwt");
|
|
|
8
8
|
const ed25519_1 = require("@stablelib/ed25519");
|
|
9
9
|
const uuid_1 = require("uuid");
|
|
10
10
|
const crypto_1 = require("crypto");
|
|
11
|
-
|
|
12
|
-
const
|
|
11
|
+
const v2_1 = require("@cheqd/ts-proto/cheqd/did/v2");
|
|
12
|
+
const MULTICODEC_ED25519_HEADER = new Uint8Array([0xed, 0x01]);
|
|
13
13
|
function isEqualKeyValuePair(kv1, kv2) {
|
|
14
14
|
return kv1.every((item, index) => item.key === kv2[index].key && item.value === kv2[index].value);
|
|
15
15
|
}
|
|
@@ -21,7 +21,7 @@ function createSignInputsFromImportableEd25519Key(key, verificationMethod) {
|
|
|
21
21
|
for (const method of verificationMethod) {
|
|
22
22
|
switch (method === null || method === void 0 ? void 0 : method.type) {
|
|
23
23
|
case types_1.VerificationMethods.Ed255192020:
|
|
24
|
-
const publicKeyMultibase =
|
|
24
|
+
const publicKeyMultibase = toMultibaseRaw(publicKey);
|
|
25
25
|
if (method.publicKeyMultibase === publicKeyMultibase) {
|
|
26
26
|
return {
|
|
27
27
|
verificationMethodId: method.id,
|
|
@@ -37,20 +37,17 @@ function createSignInputsFromImportableEd25519Key(key, verificationMethod) {
|
|
|
37
37
|
};
|
|
38
38
|
}
|
|
39
39
|
case types_1.VerificationMethods.JWK:
|
|
40
|
-
const
|
|
40
|
+
const publicKeyJwk = {
|
|
41
41
|
crv: 'Ed25519',
|
|
42
42
|
kty: 'OKP',
|
|
43
43
|
x: (0, uint8arrays_1.toString)(publicKey, 'base64url')
|
|
44
44
|
};
|
|
45
|
-
if (JSON.stringify(method.
|
|
45
|
+
if (JSON.stringify(method.publicKeyJwk) === JSON.stringify(publicKeyJwk)) {
|
|
46
46
|
return {
|
|
47
47
|
verificationMethodId: method.id,
|
|
48
48
|
privateKeyHex: key.privateKeyHex
|
|
49
49
|
};
|
|
50
50
|
}
|
|
51
|
-
else {
|
|
52
|
-
throw new Error(`JWK not matching ${method.publicKeyJWK}, ${publicKeyJWK}`);
|
|
53
|
-
}
|
|
54
51
|
}
|
|
55
52
|
}
|
|
56
53
|
throw new Error('No verification method type provided');
|
|
@@ -111,7 +108,7 @@ function createDidVerificationMethod(verificationMethodTypes, verificationKeys)
|
|
|
111
108
|
id: verificationKeys[_].keyId,
|
|
112
109
|
type,
|
|
113
110
|
controller: verificationKeys[_].didUrl,
|
|
114
|
-
publicKeyMultibase:
|
|
111
|
+
publicKeyMultibase: toMultibaseRaw((0, did_jwt_1.base64ToBytes)(verificationKeys[_].publicKey))
|
|
115
112
|
};
|
|
116
113
|
case types_1.VerificationMethods.Ed255192018:
|
|
117
114
|
return {
|
|
@@ -125,7 +122,7 @@ function createDidVerificationMethod(verificationMethodTypes, verificationKeys)
|
|
|
125
122
|
id: verificationKeys[_].keyId,
|
|
126
123
|
type,
|
|
127
124
|
controller: verificationKeys[_].didUrl,
|
|
128
|
-
|
|
125
|
+
publicKeyJwk: {
|
|
129
126
|
crv: 'Ed25519',
|
|
130
127
|
kty: 'OKP',
|
|
131
128
|
x: (0, uint8arrays_1.toString)((0, uint8arrays_1.fromString)(verificationKeys[_].publicKey, 'base64pad'), 'base64url')
|
|
@@ -141,23 +138,76 @@ function createDidPayload(verificationMethods, verificationKeys) {
|
|
|
141
138
|
if (!verificationKeys || verificationKeys.length === 0)
|
|
142
139
|
throw new Error('No verification keys provided');
|
|
143
140
|
const did = verificationKeys[0].didUrl;
|
|
144
|
-
return
|
|
141
|
+
return {
|
|
145
142
|
id: did,
|
|
146
143
|
controller: verificationKeys.map(key => key.didUrl),
|
|
147
144
|
verificationMethod: verificationMethods,
|
|
148
145
|
authentication: verificationKeys.map(key => key.keyId),
|
|
149
|
-
|
|
150
|
-
});
|
|
146
|
+
};
|
|
151
147
|
}
|
|
152
148
|
exports.createDidPayload = createDidPayload;
|
|
149
|
+
function validateSpecCompliantPayload(didDocument) {
|
|
150
|
+
var _a;
|
|
151
|
+
// id is required, validated on both compile and runtime
|
|
152
|
+
if (!(didDocument === null || didDocument === void 0 ? void 0 : didDocument.id))
|
|
153
|
+
return { valid: false, error: 'id is required' };
|
|
154
|
+
// verificationMethod is required
|
|
155
|
+
if (!(didDocument === null || didDocument === void 0 ? void 0 : didDocument.verificationMethod))
|
|
156
|
+
return { valid: false, error: 'verificationMethod is required' };
|
|
157
|
+
// verificationMethod must be an array
|
|
158
|
+
if (!Array.isArray(didDocument === null || didDocument === void 0 ? void 0 : didDocument.verificationMethod))
|
|
159
|
+
return { valid: false, error: 'verificationMethod must be an array' };
|
|
160
|
+
// verificationMethod types must be supported
|
|
161
|
+
const protoVerificationMethod = didDocument.verificationMethod.map((vm) => {
|
|
162
|
+
switch (vm === null || vm === void 0 ? void 0 : vm.type) {
|
|
163
|
+
case types_1.VerificationMethods.Ed255192020:
|
|
164
|
+
if (!vm.publicKeyMultibase)
|
|
165
|
+
throw new Error('publicKeyMultibase is required');
|
|
166
|
+
return v2_1.VerificationMethod.fromPartial({
|
|
167
|
+
id: vm.id,
|
|
168
|
+
controller: vm.controller,
|
|
169
|
+
verificationMethodType: types_1.VerificationMethods.Ed255192020,
|
|
170
|
+
verificationMaterial: vm.publicKeyMultibase,
|
|
171
|
+
});
|
|
172
|
+
case types_1.VerificationMethods.JWK:
|
|
173
|
+
if (!vm.publicKeyJwk)
|
|
174
|
+
throw new Error('publicKeyJwk is required');
|
|
175
|
+
return v2_1.VerificationMethod.fromPartial({
|
|
176
|
+
id: vm.id,
|
|
177
|
+
controller: vm.controller,
|
|
178
|
+
verificationMethodType: types_1.VerificationMethods.JWK,
|
|
179
|
+
verificationMaterial: JSON.stringify(vm.publicKeyJwk),
|
|
180
|
+
});
|
|
181
|
+
case types_1.VerificationMethods.Ed255192018:
|
|
182
|
+
if (!vm.publicKeyBase58)
|
|
183
|
+
throw new Error('publicKeyBase58 is required');
|
|
184
|
+
return v2_1.VerificationMethod.fromPartial({
|
|
185
|
+
id: vm.id,
|
|
186
|
+
controller: vm.controller,
|
|
187
|
+
verificationMethodType: types_1.VerificationMethods.Ed255192018,
|
|
188
|
+
verificationMaterial: vm.publicKeyBase58,
|
|
189
|
+
});
|
|
190
|
+
default:
|
|
191
|
+
throw new Error(`Unsupported verificationMethod type: ${vm === null || vm === void 0 ? void 0 : vm.type}`);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
const protoService = (_a = didDocument === null || didDocument === void 0 ? void 0 : didDocument.service) === null || _a === void 0 ? void 0 : _a.map((s) => {
|
|
195
|
+
return v2_1.Service.fromPartial({
|
|
196
|
+
id: s === null || s === void 0 ? void 0 : s.id,
|
|
197
|
+
serviceType: s === null || s === void 0 ? void 0 : s.type,
|
|
198
|
+
serviceEndpoint: s === null || s === void 0 ? void 0 : s.serviceEndpoint,
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
return { valid: true, protobufVerificationMethod: protoVerificationMethod, protobufService: protoService };
|
|
202
|
+
}
|
|
203
|
+
exports.validateSpecCompliantPayload = validateSpecCompliantPayload;
|
|
153
204
|
function sha256(message) {
|
|
154
205
|
return (0, crypto_1.createHash)('sha256').update(message).digest('hex');
|
|
155
206
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
return basics_1.bases['base58btc'].encode(mbKey);
|
|
207
|
+
function toMultibaseRaw(key) {
|
|
208
|
+
const multibase = new Uint8Array(MULTICODEC_ED25519_HEADER.length + key.length);
|
|
209
|
+
multibase.set(MULTICODEC_ED25519_HEADER);
|
|
210
|
+
multibase.set(key, MULTICODEC_ED25519_HEADER.length);
|
|
211
|
+
return basics_1.bases['base58btc'].encode(multibase);
|
|
162
212
|
}
|
|
163
213
|
//# sourceMappingURL=utils.js.map
|
package/build/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAAA,mCAegB;AAChB,6CAAkD;AAClD,gDAA2C;AAC3C,qCAAuC;AACvC,gDAAsF;AACtF,+BAAyB;AACzB,mCAAmC;AACnC,qDAGqC;AASrC,MAAM,yBAAyB,GAAG,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AAE/D,SAAgB,mBAAmB,CAAC,GAAoB,EAAE,GAAoB;IAC1E,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,KAAK,GAAG,CAAC,KAAK,CAAC,CAAC,GAAG,IAAI,IAAI,CAAC,KAAK,KAAK,GAAG,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAA;AACrG,CAAC;AAFD,kDAEC;AAED,SAAgB,wCAAwC,CAAC,GAA0B,EAAE,kBAAwC;IACzH,IAAI,CAAA,kBAAkB,aAAlB,kBAAkB,uBAAlB,kBAAkB,CAAE,MAAM,MAAK,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAA;IAEzF,MAAM,SAAS,GAAG,IAAA,wBAAU,EAAC,GAAG,CAAC,YAAY,EAAE,KAAK,CAAC,CAAA;IAErD,KAAI,MAAM,MAAM,IAAI,kBAAkB,EAAE;QACpC,QAAQ,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,IAAI,EAAE;YAClB,KAAK,2BAAmB,CAAC,WAAW;gBAChC,MAAM,kBAAkB,GAAG,cAAc,CAAC,SAAS,CAAC,CAAA;gBACpD,IAAI,MAA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AC;AAED,SAAS,cAAc,CAAC,GAAe;IACnC,MAAM,SAAS,GAAG,IAAI,UAAU,CAAC,yBAAyB,CAAC,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC;IAEhF,SAAS,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAC;IACzC,SAAS,CAAC,GAAG,CAAC,GAAG,EAAE,yBAAyB,CAAC,MAAM,CAAC,CAAC;IAErD,OAAO,cAAK,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;AAChD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cheqd/sdk",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-develop.2",
|
|
4
4
|
"description": "A TypeScript SDK built with CosmJS to interact with cheqd network ledger",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Cheqd Foundation Limited (https://github.com/cheqd)",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://github.com/cheqd/sdk#readme",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@cheqd/ts-proto": "^
|
|
29
|
+
"@cheqd/ts-proto": "^2.0.0-develop.1",
|
|
30
30
|
"@cosmjs/amino": "^0.29.4",
|
|
31
31
|
"@cosmjs/encoding": "^0.29.4",
|
|
32
32
|
"@cosmjs/math": "^0.29.4",
|
|
@@ -37,6 +37,8 @@
|
|
|
37
37
|
"@stablelib/ed25519": "^1.0.3",
|
|
38
38
|
"cosmjs-types": "^0.5.2",
|
|
39
39
|
"did-jwt": "^6.9.0",
|
|
40
|
+
"did-resolver": "^4.0.1",
|
|
41
|
+
"file-type": "^16.5.4",
|
|
40
42
|
"multiformats": "^9.9.0",
|
|
41
43
|
"uuid": "^9.0.0"
|
|
42
44
|
},
|
package/src/index.ts
CHANGED
|
@@ -6,12 +6,14 @@ import { createDefaultCheqdRegistry } from './registry'
|
|
|
6
6
|
import { CheqdSigningStargateClient } from './signer'
|
|
7
7
|
import { CheqdNetwork, IContext, IModuleMethodMap } from './types'
|
|
8
8
|
import { createSignInputsFromImportableEd25519Key } from './utils'
|
|
9
|
+
import { GasPrice } from '@cosmjs/stargate'
|
|
9
10
|
|
|
10
11
|
export interface ICheqdSDKOptions {
|
|
11
12
|
modules: AbstractCheqdSDKModule[]
|
|
12
|
-
authorizedMethods?: string[]
|
|
13
|
-
network?: CheqdNetwork
|
|
14
13
|
rpcUrl: string
|
|
14
|
+
network?: CheqdNetwork
|
|
15
|
+
gasPrice?: GasPrice
|
|
16
|
+
authorizedMethods?: string[]
|
|
15
17
|
readonly wallet: OfflineSigner
|
|
16
18
|
}
|
|
17
19
|
|
|
@@ -78,6 +80,7 @@ export class CheqdSDK {
|
|
|
78
80
|
this.options.wallet,
|
|
79
81
|
{
|
|
80
82
|
registry,
|
|
83
|
+
gasPrice: this.options?.gasPrice,
|
|
81
84
|
}
|
|
82
85
|
)
|
|
83
86
|
|
package/src/modules/did.ts
CHANGED
|
@@ -2,26 +2,41 @@ import { createProtobufRpcClient, DeliverTxResponse, QueryClient } from "@cosmjs
|
|
|
2
2
|
/* import { QueryClientImpl } from '@cheqd/ts-proto/cheqd/did/v1/query' */
|
|
3
3
|
import { CheqdExtension, AbstractCheqdSDKModule, MinimalImportableCheqdSDKModule } from "./_"
|
|
4
4
|
import { CheqdSigningStargateClient } from "../signer"
|
|
5
|
-
import { DidStdFee, IContext, ISignInputs,
|
|
5
|
+
import { DIDDocument, DidStdFee, IContext, ISignInputs, SpecValidationResult, VerificationMethods } from '../types';
|
|
6
6
|
import {
|
|
7
7
|
MsgCreateDidDoc,
|
|
8
|
+
MsgCreateDidDocPayload,
|
|
8
9
|
MsgCreateDidDocResponse,
|
|
9
10
|
MsgDeactivateDidDoc,
|
|
10
11
|
MsgDeactivateDidDocPayload,
|
|
11
12
|
MsgDeactivateDidDocResponse,
|
|
12
13
|
MsgUpdateDidDoc,
|
|
14
|
+
MsgUpdateDidDocPayload,
|
|
13
15
|
MsgUpdateDidDocResponse,
|
|
14
16
|
protobufPackage,
|
|
15
|
-
|
|
17
|
+
Service,
|
|
18
|
+
SignInfo,
|
|
19
|
+
VerificationMethod
|
|
16
20
|
} from "@cheqd/ts-proto/cheqd/did/v2"
|
|
17
21
|
import { EncodeObject, GeneratedType } from "@cosmjs/proto-signing"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
export const
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
import { v4 } from "uuid"
|
|
23
|
+
import { Coin } from "cosmjs-types/cosmos/base/v1beta1/coin";
|
|
24
|
+
import { Int53 } from "@cosmjs/math";
|
|
25
|
+
|
|
26
|
+
export const protobufLiterals = {
|
|
27
|
+
MsgCreateDidDoc: "MsgCreateDidDoc",
|
|
28
|
+
MsgCreateDidDocResponse: "MsgCreateDidDocResponse",
|
|
29
|
+
MsgUpdateDidDoc: "MsgUpdateDidDoc",
|
|
30
|
+
MsgUpdateDidDocResponse: "MsgUpdateDidDocResponse",
|
|
31
|
+
MsgDeactivateDidDoc: "MsgDeactivateDidDoc",
|
|
32
|
+
MsgDeactivateDidDocResponse: "MsgDeactivateDidDocResponse",
|
|
33
|
+
} as const
|
|
34
|
+
export const typeUrlMsgCreateDidDoc = `/${protobufPackage}.${protobufLiterals.MsgCreateDidDoc}`
|
|
35
|
+
export const typeUrlMsgCreateDidDocResponse = `/${protobufPackage}.${protobufLiterals.MsgCreateDidDocResponse}`
|
|
36
|
+
export const typeUrlMsgUpdateDidDoc = `/${protobufPackage}.${protobufLiterals.MsgUpdateDidDoc}`
|
|
37
|
+
export const typeUrlMsgUpdateDidDocResponse = `/${protobufPackage}.${protobufLiterals.MsgUpdateDidDocResponse}`
|
|
38
|
+
export const typeUrlMsgDeactivateDidDoc = `/${protobufPackage}.${protobufLiterals.MsgDeactivateDidDoc}`
|
|
39
|
+
export const typeUrlMsgDeactivateDidDocResponse = `/${protobufPackage}.${protobufLiterals.MsgDeactivateDidDocResponse}`
|
|
25
40
|
|
|
26
41
|
export interface MsgCreateDidDocEncodeObject extends EncodeObject {
|
|
27
42
|
readonly typeUrl: typeof typeUrlMsgCreateDidDoc,
|
|
@@ -87,6 +102,14 @@ export class DIDModule extends AbstractCheqdSDKModule {
|
|
|
87
102
|
[typeUrlMsgDeactivateDidDocResponse, MsgDeactivateDidDocResponse],
|
|
88
103
|
]
|
|
89
104
|
|
|
105
|
+
static readonly baseMinimalDenom = 'ncheq' as const
|
|
106
|
+
|
|
107
|
+
static readonly fees = {
|
|
108
|
+
DefaultCreateDidFee: { amount: '50000000000', denom: DIDModule.baseMinimalDenom } as const,
|
|
109
|
+
DefaultUpdateDidFee: { amount: '25000000000', denom: DIDModule.baseMinimalDenom } as const,
|
|
110
|
+
DefaultDeactivateDidFee: { amount: '10000000000', denom: DIDModule.baseMinimalDenom } as const,
|
|
111
|
+
} as const
|
|
112
|
+
|
|
90
113
|
constructor(signer: CheqdSigningStargateClient) {
|
|
91
114
|
super(signer)
|
|
92
115
|
this.methods = {
|
|
@@ -100,12 +123,36 @@ export class DIDModule extends AbstractCheqdSDKModule {
|
|
|
100
123
|
return DIDModule.registryTypes
|
|
101
124
|
}
|
|
102
125
|
|
|
103
|
-
async createDidTx(signInputs: ISignInputs[] | SignInfo[], didPayload:
|
|
126
|
+
async createDidTx(signInputs: ISignInputs[] | SignInfo[], didPayload: DIDDocument, address: string, fee?: DidStdFee | 'auto' | number, memo?: string, versionId?: string, context?: IContext): Promise<DeliverTxResponse> {
|
|
104
127
|
if (!this._signer) {
|
|
105
128
|
this._signer = context!.sdk!.signer
|
|
106
129
|
}
|
|
107
130
|
|
|
108
|
-
|
|
131
|
+
if (!versionId || versionId === '') {
|
|
132
|
+
versionId = v4()
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const { valid, error, protobufVerificationMethod, protobufService } = await DIDModule.validateSpecCompliantPayload(didPayload)
|
|
136
|
+
|
|
137
|
+
if (!valid) {
|
|
138
|
+
throw new Error(`DID payload is not spec compliant: ${error}`)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const payload = MsgCreateDidDocPayload.fromPartial({
|
|
142
|
+
context: <string[]>didPayload?.['@context'],
|
|
143
|
+
id: didPayload.id,
|
|
144
|
+
controller: <string[]>didPayload.controller,
|
|
145
|
+
verificationMethod: protobufVerificationMethod,
|
|
146
|
+
authentication: <string[]>didPayload.authentication,
|
|
147
|
+
assertionMethod: <string[]>didPayload.assertionMethod,
|
|
148
|
+
capabilityInvocation: <string[]>didPayload.capabilityInvocation,
|
|
149
|
+
capabilityDelegation: <string[]>didPayload.capabilityDelegation,
|
|
150
|
+
keyAgreement: <string[]>didPayload.keyAgreement,
|
|
151
|
+
service: protobufService,
|
|
152
|
+
alsoKnownAs: <string[]>didPayload.alsoKnownAs,
|
|
153
|
+
versionId: versionId
|
|
154
|
+
})
|
|
155
|
+
|
|
109
156
|
let signatures: SignInfo[]
|
|
110
157
|
if(ISignInputs.isSignInput(signInputs)) {
|
|
111
158
|
signatures = await this._signer.signCreateDidTx(signInputs, payload)
|
|
@@ -123,20 +170,47 @@ export class DIDModule extends AbstractCheqdSDKModule {
|
|
|
123
170
|
value
|
|
124
171
|
}
|
|
125
172
|
|
|
173
|
+
if (!fee) {
|
|
174
|
+
fee = await DIDModule.generateCreateDidDocFees(address)
|
|
175
|
+
}
|
|
176
|
+
|
|
126
177
|
return this._signer.signAndBroadcast(
|
|
127
178
|
address,
|
|
128
179
|
[createDidMsg],
|
|
129
|
-
fee
|
|
180
|
+
fee!,
|
|
130
181
|
memo
|
|
131
182
|
)
|
|
132
183
|
}
|
|
133
184
|
|
|
134
|
-
async updateDidTx(signInputs: ISignInputs[] | SignInfo[], didPayload:
|
|
185
|
+
async updateDidTx(signInputs: ISignInputs[] | SignInfo[], didPayload: DIDDocument, address: string, fee?: DidStdFee | 'auto' | number, memo?: string, versionId?: string, context?: IContext): Promise<DeliverTxResponse> {
|
|
135
186
|
if (!this._signer) {
|
|
136
187
|
this._signer = context!.sdk!.signer
|
|
137
188
|
}
|
|
138
189
|
|
|
139
|
-
|
|
190
|
+
if (!versionId || versionId === '') {
|
|
191
|
+
versionId = v4()
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const { valid, error, protobufVerificationMethod, protobufService } = await DIDModule.validateSpecCompliantPayload(didPayload)
|
|
195
|
+
|
|
196
|
+
if (!valid) {
|
|
197
|
+
throw new Error(`DID payload is not spec compliant: ${error}`)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const payload = MsgUpdateDidDocPayload.fromPartial({
|
|
201
|
+
context: <string[]>didPayload?.['@context'],
|
|
202
|
+
id: didPayload.id,
|
|
203
|
+
controller: <string[]>didPayload.controller,
|
|
204
|
+
verificationMethod: protobufVerificationMethod,
|
|
205
|
+
authentication: <string[]>didPayload.authentication,
|
|
206
|
+
assertionMethod: <string[]>didPayload.assertionMethod,
|
|
207
|
+
capabilityInvocation: <string[]>didPayload.capabilityInvocation,
|
|
208
|
+
capabilityDelegation: <string[]>didPayload.capabilityDelegation,
|
|
209
|
+
keyAgreement: <string[]>didPayload.keyAgreement,
|
|
210
|
+
service: protobufService,
|
|
211
|
+
alsoKnownAs: <string[]>didPayload.alsoKnownAs,
|
|
212
|
+
versionId: versionId
|
|
213
|
+
})
|
|
140
214
|
let signatures: SignInfo[]
|
|
141
215
|
if(ISignInputs.isSignInput(signInputs)) {
|
|
142
216
|
signatures = await this._signer.signUpdateDidTx(signInputs, payload)
|
|
@@ -154,23 +228,41 @@ export class DIDModule extends AbstractCheqdSDKModule {
|
|
|
154
228
|
value
|
|
155
229
|
}
|
|
156
230
|
|
|
231
|
+
if (!fee) {
|
|
232
|
+
fee = await DIDModule.generateUpdateDidDocFees(address)
|
|
233
|
+
}
|
|
234
|
+
|
|
157
235
|
return this._signer.signAndBroadcast(
|
|
158
236
|
address,
|
|
159
237
|
[updateDidMsg],
|
|
160
|
-
fee
|
|
238
|
+
fee!,
|
|
161
239
|
memo
|
|
162
240
|
)
|
|
163
241
|
}
|
|
164
242
|
|
|
165
|
-
async deactivateDidTx(signInputs: ISignInputs[] | SignInfo[], didPayload:
|
|
243
|
+
async deactivateDidTx(signInputs: ISignInputs[] | SignInfo[], didPayload: DIDDocument, address: string, fee?: DidStdFee | 'auto' | number, memo?: string, versionId?: string, context?: IContext): Promise<DeliverTxResponse> {
|
|
166
244
|
if (!this._signer) {
|
|
167
245
|
this._signer = context!.sdk!.signer
|
|
168
246
|
}
|
|
169
247
|
|
|
170
|
-
|
|
248
|
+
if (!versionId || versionId === '') {
|
|
249
|
+
versionId = v4()
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const { valid, error, protobufVerificationMethod } = await DIDModule.validateSpecCompliantPayload(didPayload)
|
|
253
|
+
|
|
254
|
+
if (!valid) {
|
|
255
|
+
throw new Error(`DID payload is not spec compliant: ${error}`)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const payload = MsgDeactivateDidDocPayload.fromPartial({
|
|
259
|
+
id: didPayload.id,
|
|
260
|
+
versionId: versionId
|
|
261
|
+
})
|
|
262
|
+
|
|
171
263
|
let signatures: SignInfo[]
|
|
172
264
|
if(ISignInputs.isSignInput(signInputs)) {
|
|
173
|
-
signatures = await this._signer.signDeactivateDidTx(signInputs, payload,
|
|
265
|
+
signatures = await this._signer.signDeactivateDidTx(signInputs, payload, protobufVerificationMethod!)
|
|
174
266
|
} else {
|
|
175
267
|
signatures = signInputs
|
|
176
268
|
}
|
|
@@ -185,13 +277,106 @@ export class DIDModule extends AbstractCheqdSDKModule {
|
|
|
185
277
|
value
|
|
186
278
|
}
|
|
187
279
|
|
|
280
|
+
if (!fee) {
|
|
281
|
+
fee = await DIDModule.generateDeactivateDidDocFees(address)
|
|
282
|
+
}
|
|
283
|
+
|
|
188
284
|
return this._signer.signAndBroadcast(
|
|
189
285
|
address,
|
|
190
286
|
[deactivateDidMsg],
|
|
191
|
-
fee
|
|
287
|
+
fee!,
|
|
192
288
|
memo
|
|
193
289
|
)
|
|
194
290
|
}
|
|
291
|
+
|
|
292
|
+
static async validateSpecCompliantPayload(didDocument: DIDDocument): Promise<SpecValidationResult> {
|
|
293
|
+
// id is required, validated on both compile and runtime
|
|
294
|
+
if (!didDocument?.id) return { valid: false, error: 'id is required' }
|
|
295
|
+
|
|
296
|
+
// verificationMethod is required
|
|
297
|
+
if (!didDocument?.verificationMethod) return { valid: false, error: 'verificationMethod is required' }
|
|
298
|
+
|
|
299
|
+
// verificationMethod must be an array
|
|
300
|
+
if (!Array.isArray(didDocument?.verificationMethod)) return { valid: false, error: 'verificationMethod must be an array' }
|
|
301
|
+
|
|
302
|
+
// verificationMethod types must be supported
|
|
303
|
+
const protoVerificationMethod = didDocument.verificationMethod.map((vm) => {
|
|
304
|
+
switch (vm?.type) {
|
|
305
|
+
case VerificationMethods.Ed255192020:
|
|
306
|
+
if (!vm?.publicKeyMultibase) throw new Error('publicKeyMultibase is required')
|
|
307
|
+
|
|
308
|
+
return VerificationMethod.fromPartial({
|
|
309
|
+
id: vm.id,
|
|
310
|
+
controller: vm.controller,
|
|
311
|
+
verificationMethodType: VerificationMethods.Ed255192020,
|
|
312
|
+
verificationMaterial: vm.publicKeyMultibase,
|
|
313
|
+
})
|
|
314
|
+
case VerificationMethods.JWK:
|
|
315
|
+
if (!vm?.publicKeyJwk) throw new Error('publicKeyJwk is required')
|
|
316
|
+
|
|
317
|
+
return VerificationMethod.fromPartial({
|
|
318
|
+
id: vm.id,
|
|
319
|
+
controller: vm.controller,
|
|
320
|
+
verificationMethodType: VerificationMethods.JWK,
|
|
321
|
+
verificationMaterial: JSON.stringify(vm.publicKeyJwk),
|
|
322
|
+
})
|
|
323
|
+
case VerificationMethods.Ed255192018:
|
|
324
|
+
if (!vm?.publicKeyBase58) throw new Error('publicKeyBase58 is required')
|
|
325
|
+
|
|
326
|
+
return VerificationMethod.fromPartial({
|
|
327
|
+
id: vm.id,
|
|
328
|
+
controller: vm.controller,
|
|
329
|
+
verificationMethodType: VerificationMethods.Ed255192018,
|
|
330
|
+
verificationMaterial: vm.publicKeyBase58,
|
|
331
|
+
})
|
|
332
|
+
default:
|
|
333
|
+
throw new Error('Unsupported verificationMethod type')
|
|
334
|
+
}
|
|
335
|
+
})
|
|
336
|
+
|
|
337
|
+
const protoService = didDocument?.service?.map((s) => {
|
|
338
|
+
return Service.fromPartial({
|
|
339
|
+
id: s?.id,
|
|
340
|
+
serviceType: s?.type,
|
|
341
|
+
serviceEndpoint: <string[]>s?.serviceEndpoint,
|
|
342
|
+
})
|
|
343
|
+
})
|
|
344
|
+
|
|
345
|
+
return { valid: true, protobufVerificationMethod: protoVerificationMethod, protobufService: protoService } as SpecValidationResult
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
static async generateCreateDidDocFees(feePayer: string, granter?: string): Promise<DidStdFee> {
|
|
349
|
+
return {
|
|
350
|
+
amount: [
|
|
351
|
+
DIDModule.fees.DefaultCreateDidFee
|
|
352
|
+
],
|
|
353
|
+
gas: '360000',
|
|
354
|
+
payer: feePayer,
|
|
355
|
+
granter: granter
|
|
356
|
+
} as DidStdFee
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
static async generateUpdateDidDocFees(feePayer: string, granter?: string): Promise<DidStdFee> {
|
|
360
|
+
return {
|
|
361
|
+
amount: [
|
|
362
|
+
DIDModule.fees.DefaultUpdateDidFee
|
|
363
|
+
],
|
|
364
|
+
gas: '360000',
|
|
365
|
+
payer: feePayer,
|
|
366
|
+
granter: granter
|
|
367
|
+
} as DidStdFee
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
static async generateDeactivateDidDocFees(feePayer: string, granter?: string): Promise<DidStdFee> {
|
|
371
|
+
return {
|
|
372
|
+
amount: [
|
|
373
|
+
DIDModule.fees.DefaultDeactivateDidFee
|
|
374
|
+
],
|
|
375
|
+
gas: '360000',
|
|
376
|
+
payer: feePayer,
|
|
377
|
+
granter: granter
|
|
378
|
+
} as DidStdFee
|
|
379
|
+
}
|
|
195
380
|
}
|
|
196
381
|
|
|
197
382
|
export type MinimalImportableDIDModule = MinimalImportableCheqdSDKModule<DIDModule>
|
package/src/modules/resource.ts
CHANGED
|
@@ -5,9 +5,15 @@ import { DidStdFee, IContext, ISignInputs } from '../types';
|
|
|
5
5
|
import { MsgCreateResource, MsgCreateResourcePayload, MsgCreateResourceResponse, protobufPackage } from "@cheqd/ts-proto/cheqd/resource/v2"
|
|
6
6
|
import { DeliverTxResponse } from "@cosmjs/stargate"
|
|
7
7
|
import { SignInfo } from "@cheqd/ts-proto/cheqd/did/v2";
|
|
8
|
+
import { fromBuffer } from "file-type";
|
|
8
9
|
|
|
9
|
-
export const
|
|
10
|
-
|
|
10
|
+
export const protobufLiterals = {
|
|
11
|
+
MsgCreateResource: 'MsgCreateResource',
|
|
12
|
+
MsgCreateResourceResponse: 'MsgCreateResourceResponse'
|
|
13
|
+
} as const
|
|
14
|
+
|
|
15
|
+
export const typeUrlMsgCreateResource = `/${protobufPackage}.${protobufLiterals.MsgCreateResource}`
|
|
16
|
+
export const typeUrlMsgCreateResourceResponse = `/${protobufPackage}.${protobufLiterals.MsgCreateResourceResponse}`
|
|
11
17
|
|
|
12
18
|
export interface MsgCreateResourceEncodeObject extends EncodeObject {
|
|
13
19
|
readonly typeUrl: typeof typeUrlMsgCreateResource,
|
|
@@ -24,6 +30,14 @@ export class ResourceModule extends AbstractCheqdSDKModule {
|
|
|
24
30
|
[typeUrlMsgCreateResourceResponse, MsgCreateResourceResponse]
|
|
25
31
|
]
|
|
26
32
|
|
|
33
|
+
static readonly baseMinimalDenom = 'ncheq' as const
|
|
34
|
+
|
|
35
|
+
static readonly fees = {
|
|
36
|
+
DefaultCreateResourceImageFee: { amount: '10000000000', denom: ResourceModule.baseMinimalDenom } as const,
|
|
37
|
+
DefaultCreateResourceJsonFee: { amount: '2500000000', denom: ResourceModule.baseMinimalDenom } as const,
|
|
38
|
+
DefaultCreateResourceDefaultFee: { amount: '5000000000', denom: ResourceModule.baseMinimalDenom } as const,
|
|
39
|
+
} as const
|
|
40
|
+
|
|
27
41
|
constructor(signer: CheqdSigningStargateClient) {
|
|
28
42
|
super(signer)
|
|
29
43
|
this.methods = {
|
|
@@ -50,7 +64,7 @@ export class ResourceModule extends AbstractCheqdSDKModule {
|
|
|
50
64
|
}
|
|
51
65
|
}
|
|
52
66
|
|
|
53
|
-
async createResourceTx(signInputs: ISignInputs[] | SignInfo[], resourcePayload: Partial<MsgCreateResourcePayload>, address: string, fee
|
|
67
|
+
async createResourceTx(signInputs: ISignInputs[] | SignInfo[], resourcePayload: Partial<MsgCreateResourcePayload>, address: string, fee?: DidStdFee | 'auto' | number, memo?: string, context?: IContext): Promise<DeliverTxResponse> {
|
|
54
68
|
if (!this._signer) {
|
|
55
69
|
this._signer = context!.sdk!.signer
|
|
56
70
|
}
|
|
@@ -64,13 +78,70 @@ export class ResourceModule extends AbstractCheqdSDKModule {
|
|
|
64
78
|
value: msg
|
|
65
79
|
}
|
|
66
80
|
|
|
81
|
+
if (!fee) {
|
|
82
|
+
if (payload.data.length === 0) {
|
|
83
|
+
throw new Error('Linked resource data is empty')
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
fee = await async function() {
|
|
87
|
+
const mimeType = await ResourceModule.readMimeType(payload.data)
|
|
88
|
+
|
|
89
|
+
if (mimeType.startsWith('image/')) {
|
|
90
|
+
return await ResourceModule.generateCreateResourceImageFees(address)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (mimeType.startsWith('application/json')) {
|
|
94
|
+
return await ResourceModule.generateCreateResourceJsonFees(address)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return await ResourceModule.generateCreateResourceDefaultFees(address)
|
|
98
|
+
}()
|
|
99
|
+
}
|
|
100
|
+
|
|
67
101
|
return this._signer.signAndBroadcast(
|
|
68
102
|
address,
|
|
69
103
|
[encObj],
|
|
70
|
-
fee
|
|
104
|
+
fee!,
|
|
71
105
|
memo
|
|
72
106
|
)
|
|
73
107
|
}
|
|
108
|
+
|
|
109
|
+
static async readMimeType(content: Uint8Array): Promise<string> {
|
|
110
|
+
return (await fromBuffer(content))?.mime ?? 'application/octet-stream'
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
static async generateCreateResourceImageFees(feePayer: string, granter?: string): Promise<DidStdFee> {
|
|
114
|
+
return {
|
|
115
|
+
amount: [
|
|
116
|
+
ResourceModule.fees.DefaultCreateResourceImageFee
|
|
117
|
+
],
|
|
118
|
+
gas: '360000',
|
|
119
|
+
payer: feePayer,
|
|
120
|
+
granter: granter
|
|
121
|
+
} as DidStdFee
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
static async generateCreateResourceJsonFees(feePayer: string, granter?: string): Promise<DidStdFee> {
|
|
125
|
+
return {
|
|
126
|
+
amount: [
|
|
127
|
+
ResourceModule.fees.DefaultCreateResourceJsonFee
|
|
128
|
+
],
|
|
129
|
+
gas: '360000',
|
|
130
|
+
payer: feePayer,
|
|
131
|
+
granter: granter
|
|
132
|
+
} as DidStdFee
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
static async generateCreateResourceDefaultFees(feePayer: string, granter?: string): Promise<DidStdFee> {
|
|
136
|
+
return {
|
|
137
|
+
amount: [
|
|
138
|
+
ResourceModule.fees.DefaultCreateResourceDefaultFee
|
|
139
|
+
],
|
|
140
|
+
gas: '360000',
|
|
141
|
+
payer: feePayer,
|
|
142
|
+
granter: granter
|
|
143
|
+
} as DidStdFee
|
|
144
|
+
}
|
|
74
145
|
}
|
|
75
146
|
|
|
76
147
|
export type MinimalImportableResourcesModule = MinimalImportableCheqdSDKModule<ResourceModule>
|