@cheqd/sdk 1.4.0-develop.5 → 1.5.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/.github/workflows/build.yml +3 -2
- package/.github/workflows/dispatch.yml +7 -1
- package/.github/workflows/test.yml +62 -0
- package/CHANGELOG.md +23 -0
- package/build/modules/did.d.ts +41 -28
- package/build/modules/did.d.ts.map +1 -1
- package/build/modules/did.js +55 -28
- package/build/modules/did.js.map +1 -1
- package/build/modules/resource.d.ts +1 -2
- package/build/modules/resource.d.ts.map +1 -1
- package/build/modules/resource.js +7 -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 +22 -11
- package/build/signer.js.map +1 -1
- package/build/types.d.ts +4 -0
- package/build/types.d.ts.map +1 -1
- package/build/types.js.map +1 -1
- package/build/utils.d.ts +4 -8
- package/build/utils.d.ts.map +1 -1
- package/build/utils.js +36 -22
- 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/modules/did.ts +100 -42
- package/src/modules/resource.ts +2 -31
- package/src/signer.ts +23 -10
- package/src/types.ts +5 -0
- package/src/utils.ts +44 -23
- package/tests/modules/did.test.ts +10 -8
- package/tests/modules/resource.test.ts +5 -3
- package/tests/signer.test.ts +9 -10
- package/tests/testutils.test.ts +25 -19
package/src/modules/did.ts
CHANGED
|
@@ -1,65 +1,97 @@
|
|
|
1
1
|
import { createProtobufRpcClient, DeliverTxResponse, QueryClient } from "@cosmjs/stargate"
|
|
2
|
-
/* import { QueryClientImpl } from '@cheqd/ts-proto/cheqd/v1/query' */
|
|
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 } from "../types"
|
|
6
|
-
import {
|
|
5
|
+
import { DidStdFee, IContext, ISignInputs, MsgDeactivateDidPayload } from "../types"
|
|
6
|
+
import {
|
|
7
|
+
MsgCreateDidDoc,
|
|
8
|
+
MsgCreateDidDocPayload,
|
|
9
|
+
MsgCreateDidDocResponse,
|
|
10
|
+
MsgDeactivateDidDoc,
|
|
11
|
+
MsgDeactivateDidDocPayload,
|
|
12
|
+
MsgDeactivateDidDocResponse,
|
|
13
|
+
MsgUpdateDidDoc,
|
|
14
|
+
MsgUpdateDidDocPayload,
|
|
15
|
+
MsgUpdateDidDocResponse,
|
|
16
|
+
protobufPackage
|
|
17
|
+
} from "@cheqd/ts-proto/cheqd/did/v2"
|
|
7
18
|
import { EncodeObject, GeneratedType } from "@cosmjs/proto-signing"
|
|
8
19
|
|
|
9
|
-
export const
|
|
10
|
-
export const
|
|
11
|
-
export const
|
|
12
|
-
export const
|
|
20
|
+
export const typeUrlMsgCreateDidDoc = `/${protobufPackage}.MsgCreateDidDoc`
|
|
21
|
+
export const typeUrlMsgCreateDidDocResponse = `/${protobufPackage}.MsgCreateDidDocResponse`
|
|
22
|
+
export const typeUrlMsgUpdateDidDoc = `/${protobufPackage}.MsgUpdateDidDoc`
|
|
23
|
+
export const typeUrlMsgUpdateDidDocResponse = `/${protobufPackage}.MsgUpdateDidDocResponse`
|
|
24
|
+
export const typeUrlMsgDeactivateDidDoc = `/${protobufPackage}.MsgDeactivateDidDoc`
|
|
25
|
+
export const typeUrlMsgDeactivateDidDocResponse = `/${protobufPackage}.MsgDeactivateDidDocResponse`
|
|
13
26
|
|
|
14
|
-
export interface
|
|
15
|
-
readonly typeUrl: typeof
|
|
16
|
-
readonly value: Partial<
|
|
27
|
+
export interface MsgCreateDidDocEncodeObject extends EncodeObject {
|
|
28
|
+
readonly typeUrl: typeof typeUrlMsgCreateDidDoc,
|
|
29
|
+
readonly value: Partial<MsgCreateDidDoc>
|
|
17
30
|
}
|
|
18
31
|
|
|
19
|
-
export function
|
|
20
|
-
return obj.typeUrl ===
|
|
32
|
+
export function isMsgCreateDidDocEncodeObject(obj: EncodeObject): obj is MsgCreateDidDocEncodeObject {
|
|
33
|
+
return obj.typeUrl === typeUrlMsgCreateDidDoc
|
|
21
34
|
}
|
|
22
35
|
|
|
23
|
-
export interface
|
|
24
|
-
readonly typeUrl: typeof
|
|
25
|
-
readonly value: Partial<
|
|
36
|
+
export interface MsgCreateDidDocResponseEncodeObject extends EncodeObject {
|
|
37
|
+
readonly typeUrl: typeof typeUrlMsgCreateDidDocResponse,
|
|
38
|
+
readonly value: Partial<MsgCreateDidDocResponse>
|
|
26
39
|
}
|
|
27
40
|
|
|
28
|
-
export function
|
|
29
|
-
return obj.typeUrl ===
|
|
41
|
+
export function MsgCreateDidDocResponseEncodeObject(obj: EncodeObject): obj is MsgCreateDidDocResponseEncodeObject {
|
|
42
|
+
return obj.typeUrl === typeUrlMsgCreateDidDocResponse
|
|
30
43
|
}
|
|
31
44
|
|
|
32
|
-
export interface
|
|
33
|
-
readonly typeUrl: typeof
|
|
34
|
-
readonly value: Partial<
|
|
45
|
+
export interface MsgUpdateDidDocEncodeObject extends EncodeObject {
|
|
46
|
+
readonly typeUrl: typeof typeUrlMsgUpdateDidDoc,
|
|
47
|
+
readonly value: Partial<MsgUpdateDidDoc>
|
|
35
48
|
}
|
|
36
49
|
|
|
37
|
-
export function
|
|
38
|
-
return obj.typeUrl ===
|
|
50
|
+
export function MsgUpdateDidDocEncodeObject(obj: EncodeObject): obj is MsgUpdateDidDocEncodeObject {
|
|
51
|
+
return obj.typeUrl === typeUrlMsgUpdateDidDoc
|
|
39
52
|
}
|
|
40
53
|
|
|
41
|
-
export interface
|
|
42
|
-
readonly typeUrl: typeof
|
|
43
|
-
readonly value: Partial<
|
|
54
|
+
export interface MsgUpdateDidDocResponseEncodeObject extends EncodeObject {
|
|
55
|
+
readonly typeUrl: typeof typeUrlMsgUpdateDidDocResponse,
|
|
56
|
+
readonly value: Partial<MsgUpdateDidDocResponse>
|
|
44
57
|
}
|
|
45
58
|
|
|
46
|
-
export function
|
|
47
|
-
return obj.typeUrl ===
|
|
59
|
+
export function MsgUpdateDidDocResponseEncodeObject(obj: EncodeObject): obj is MsgUpdateDidDocResponseEncodeObject {
|
|
60
|
+
return obj.typeUrl === typeUrlMsgUpdateDidDocResponse
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface MsgDeactivateDidDocEncodeObject extends EncodeObject {
|
|
64
|
+
readonly typeUrl: typeof typeUrlMsgDeactivateDidDoc,
|
|
65
|
+
readonly value: Partial<MsgDeactivateDidDoc>
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function MsgDeactivateDidDocEncodeObject(obj: EncodeObject): obj is MsgUpdateDidDocEncodeObject {
|
|
69
|
+
return obj.typeUrl === typeUrlMsgDeactivateDidDoc
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface MsgDeactivateDidDocResponseEncodeObject extends EncodeObject {
|
|
73
|
+
readonly typeUrl: typeof typeUrlMsgDeactivateDidDocResponse,
|
|
74
|
+
readonly value: Partial<MsgDeactivateDidDocResponse>
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function MsgDeactiveDidDocResponseEncodeObject(obj: EncodeObject): obj is MsgDeactivateDidDocResponseEncodeObject {
|
|
78
|
+
return obj.typeUrl === typeUrlMsgUpdateDidDocResponse
|
|
48
79
|
}
|
|
49
80
|
|
|
50
81
|
export class DIDModule extends AbstractCheqdSDKModule {
|
|
51
82
|
static readonly registryTypes: Iterable<[string, GeneratedType]> = [
|
|
52
|
-
[
|
|
53
|
-
[
|
|
54
|
-
[
|
|
55
|
-
[
|
|
83
|
+
[typeUrlMsgCreateDidDoc, MsgCreateDidDoc],
|
|
84
|
+
[typeUrlMsgCreateDidDocResponse, MsgCreateDidDocResponse],
|
|
85
|
+
[typeUrlMsgUpdateDidDoc, MsgUpdateDidDoc],
|
|
86
|
+
[typeUrlMsgUpdateDidDocResponse, MsgUpdateDidDocResponse],
|
|
56
87
|
]
|
|
57
88
|
|
|
58
89
|
constructor(signer: CheqdSigningStargateClient) {
|
|
59
90
|
super(signer)
|
|
60
91
|
this.methods = {
|
|
61
92
|
createDidTx: this.createDidTx.bind(this),
|
|
62
|
-
updateDidTx: this.updateDidTx.bind(this)
|
|
93
|
+
updateDidTx: this.updateDidTx.bind(this),
|
|
94
|
+
deactivateDidTx: this.deactivateDidTx.bind(this),
|
|
63
95
|
}
|
|
64
96
|
}
|
|
65
97
|
|
|
@@ -67,21 +99,21 @@ export class DIDModule extends AbstractCheqdSDKModule {
|
|
|
67
99
|
return DIDModule.registryTypes
|
|
68
100
|
}
|
|
69
101
|
|
|
70
|
-
async createDidTx(signInputs: ISignInputs[], didPayload: Partial<
|
|
102
|
+
async createDidTx(signInputs: ISignInputs[], didPayload: Partial<MsgCreateDidDocPayload>, address: string, fee: DidStdFee | 'auto' | number, memo?: string, context?: IContext): Promise<DeliverTxResponse> {
|
|
71
103
|
if (!this._signer) {
|
|
72
104
|
this._signer = context!.sdk!.signer
|
|
73
105
|
}
|
|
74
106
|
|
|
75
|
-
const payload =
|
|
107
|
+
const payload = MsgCreateDidDocPayload.fromPartial(didPayload)
|
|
76
108
|
const signatures = await this._signer.signCreateDidTx(signInputs, payload)
|
|
77
109
|
|
|
78
|
-
const value:
|
|
110
|
+
const value: MsgCreateDidDoc = {
|
|
79
111
|
payload,
|
|
80
112
|
signatures
|
|
81
113
|
}
|
|
82
114
|
|
|
83
|
-
const createDidMsg:
|
|
84
|
-
typeUrl:
|
|
115
|
+
const createDidMsg: MsgCreateDidDocEncodeObject = {
|
|
116
|
+
typeUrl: typeUrlMsgCreateDidDoc,
|
|
85
117
|
value
|
|
86
118
|
}
|
|
87
119
|
|
|
@@ -93,21 +125,21 @@ export class DIDModule extends AbstractCheqdSDKModule {
|
|
|
93
125
|
)
|
|
94
126
|
}
|
|
95
127
|
|
|
96
|
-
async updateDidTx(signInputs: ISignInputs[], didPayload: Partial<
|
|
128
|
+
async updateDidTx(signInputs: ISignInputs[], didPayload: Partial<MsgUpdateDidDocPayload>, address: string, fee: DidStdFee | 'auto' | number, memo?: string, context?: IContext): Promise<DeliverTxResponse> {
|
|
97
129
|
if (!this._signer) {
|
|
98
130
|
this._signer = context!.sdk!.signer
|
|
99
131
|
}
|
|
100
132
|
|
|
101
|
-
const payload =
|
|
133
|
+
const payload = MsgUpdateDidDocPayload.fromPartial(didPayload)
|
|
102
134
|
const signatures = await this._signer.signUpdateDidTx(signInputs, payload)
|
|
103
135
|
|
|
104
|
-
const value:
|
|
136
|
+
const value: MsgUpdateDidDoc = {
|
|
105
137
|
payload,
|
|
106
138
|
signatures
|
|
107
139
|
}
|
|
108
140
|
|
|
109
|
-
const updateDidMsg:
|
|
110
|
-
typeUrl:
|
|
141
|
+
const updateDidMsg: MsgUpdateDidDocEncodeObject = {
|
|
142
|
+
typeUrl: typeUrlMsgUpdateDidDoc,
|
|
111
143
|
value
|
|
112
144
|
}
|
|
113
145
|
|
|
@@ -118,6 +150,32 @@ export class DIDModule extends AbstractCheqdSDKModule {
|
|
|
118
150
|
memo
|
|
119
151
|
)
|
|
120
152
|
}
|
|
153
|
+
|
|
154
|
+
async deactivateDidTx(signInputs: ISignInputs[], didPayload: MsgDeactivateDidPayload, address: string, fee: DidStdFee | 'auto' | number, memo?: string, context?: IContext): Promise<DeliverTxResponse> {
|
|
155
|
+
if (!this._signer) {
|
|
156
|
+
this._signer = context!.sdk!.signer
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const payload = MsgDeactivateDidDocPayload.fromPartial({id: didPayload.id, versionId: didPayload.versionId})
|
|
160
|
+
const signatures = await this._signer.signDeactivateDidTx(signInputs, payload, didPayload.verificationMethod)
|
|
161
|
+
|
|
162
|
+
const value: MsgDeactivateDidDoc = {
|
|
163
|
+
payload,
|
|
164
|
+
signatures
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const deactivateDidMsg: MsgDeactivateDidDocEncodeObject = {
|
|
168
|
+
typeUrl: typeUrlMsgDeactivateDidDoc,
|
|
169
|
+
value
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return this._signer.signAndBroadcast(
|
|
173
|
+
address,
|
|
174
|
+
[deactivateDidMsg],
|
|
175
|
+
fee,
|
|
176
|
+
memo
|
|
177
|
+
)
|
|
178
|
+
}
|
|
121
179
|
}
|
|
122
180
|
|
|
123
181
|
export type MinimalImportableDIDModule = MinimalImportableCheqdSDKModule<DIDModule>
|
package/src/modules/resource.ts
CHANGED
|
@@ -2,9 +2,8 @@ import { AbstractCheqdSDKModule, MinimalImportableCheqdSDKModule } from "./_"
|
|
|
2
2
|
import { CheqdSigningStargateClient } from "../signer"
|
|
3
3
|
import { EncodeObject, GeneratedType } from "@cosmjs/proto-signing"
|
|
4
4
|
import { DidStdFee, IContext, ISignInputs } from '../types';
|
|
5
|
-
import { MsgCreateResource, MsgCreateResourcePayload, MsgCreateResourceResponse, protobufPackage } from "@cheqd/ts-proto/resource/
|
|
5
|
+
import { MsgCreateResource, MsgCreateResourcePayload, MsgCreateResourceResponse, protobufPackage } from "@cheqd/ts-proto/cheqd/resource/v2"
|
|
6
6
|
import { DeliverTxResponse } from "@cosmjs/stargate"
|
|
7
|
-
import { Writer } from "protobufjs"
|
|
8
7
|
|
|
9
8
|
export const typeUrlMsgCreateResource = `/${protobufPackage}.MsgCreateResource`
|
|
10
9
|
export const typeUrlMsgCreateResourceResponse = `/${protobufPackage}.MsgCreateResourceResponse`
|
|
@@ -35,36 +34,8 @@ export class ResourceModule extends AbstractCheqdSDKModule {
|
|
|
35
34
|
return []
|
|
36
35
|
}
|
|
37
36
|
|
|
38
|
-
// We need this workagound because amino encoding is used in cheqd-node to derive sign bytes for identity messages.
|
|
39
|
-
// In most cases it works the same way as protobuf encoding, but in the MsgCreateResourcePayload
|
|
40
|
-
// we use non-default property indexes so we need this separate encoding function.
|
|
41
|
-
// TODO: Remove this workaround when cheqd-node will use protobuf encoding.
|
|
42
|
-
static getMsgCreateResourcePayloadAminoSignBytes(message: MsgCreateResourcePayload): Uint8Array {
|
|
43
|
-
const writer = new Writer();
|
|
44
|
-
|
|
45
|
-
if (message.collectionId !== "") {
|
|
46
|
-
writer.uint32(10).string(message.collectionId);
|
|
47
|
-
}
|
|
48
|
-
if (message.id !== "") {
|
|
49
|
-
writer.uint32(18).string(message.id);
|
|
50
|
-
}
|
|
51
|
-
if (message.name !== "") {
|
|
52
|
-
writer.uint32(26).string(message.name);
|
|
53
|
-
}
|
|
54
|
-
if (message.resourceType !== "") {
|
|
55
|
-
writer.uint32(34).string(message.resourceType);
|
|
56
|
-
}
|
|
57
|
-
if (message.data.length !== 0) {
|
|
58
|
-
// Animo coded assigns index 5 to this property. In proto definitions it's 6.
|
|
59
|
-
// Since we use amino on node + non default property indexing, we need to encode it manually.
|
|
60
|
-
writer.uint32(42).bytes(message.data);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return writer.finish();
|
|
64
|
-
}
|
|
65
|
-
|
|
66
37
|
static async signPayload(payload: MsgCreateResourcePayload, signInputs: ISignInputs[]): Promise<MsgCreateResource> {
|
|
67
|
-
const signBytes =
|
|
38
|
+
const signBytes = MsgCreateResourcePayload.encode(payload).finish()
|
|
68
39
|
const signatures = await CheqdSigningStargateClient.signIdentityTx(signBytes, signInputs)
|
|
69
40
|
|
|
70
41
|
return {
|
package/src/signer.ts
CHANGED
|
@@ -3,11 +3,9 @@ import { EncodeObject, isOfflineDirectSigner, OfflineSigner, encodePubkey, TxBod
|
|
|
3
3
|
import { DeliverTxResponse, GasPrice, HttpEndpoint, QueryClient, SigningStargateClient, SigningStargateClientOptions, calculateFee, SignerData } from "@cosmjs/stargate"
|
|
4
4
|
import { Tendermint34Client } from "@cosmjs/tendermint-rpc"
|
|
5
5
|
import { createDefaultCheqdRegistry } from "./registry"
|
|
6
|
-
import {
|
|
6
|
+
import { MsgCreateDidDocPayload, SignInfo, MsgUpdateDidDocPayload, MsgDeactivateDidDocPayload, VerificationMethod } from '@cheqd/ts-proto/cheqd/did/v2';
|
|
7
7
|
import { DidStdFee, ISignInputs, TSignerAlgo, VerificationMethods } from './types';
|
|
8
|
-
import { VerificationMethod } from '@cheqd/ts-proto/cheqd/v1/did'
|
|
9
8
|
import { base64ToBytes, EdDSASigner, hexToBytes, Signer, ES256Signer, ES256KSigner } from 'did-jwt';
|
|
10
|
-
import { toString } from 'uint8arrays'
|
|
11
9
|
import { assert, assertDefined } from '@cosmjs/utils'
|
|
12
10
|
import { encodeSecp256k1Pubkey } from '@cosmjs/amino'
|
|
13
11
|
import { Int53 } from '@cosmjs/math'
|
|
@@ -198,30 +196,45 @@ export class CheqdSigningStargateClient extends SigningStargateClient {
|
|
|
198
196
|
return this.didSigners[verificationMethod]!
|
|
199
197
|
}
|
|
200
198
|
|
|
201
|
-
async signCreateDidTx(signInputs: ISignInputs[], payload:
|
|
199
|
+
async signCreateDidTx(signInputs: ISignInputs[], payload: MsgCreateDidDocPayload): Promise<SignInfo[]> {
|
|
202
200
|
await this.checkDidSigners(payload?.verificationMethod)
|
|
203
201
|
|
|
204
|
-
const signBytes =
|
|
202
|
+
const signBytes = MsgCreateDidDocPayload.encode(payload).finish()
|
|
205
203
|
const signInfos: SignInfo[] = await Promise.all(signInputs.map(async (signInput) => {
|
|
206
204
|
return {
|
|
207
205
|
verificationMethodId: signInput.verificationMethodId,
|
|
208
206
|
// TODO: We can't rely on `payload.verificationMethod` here because `CreateResourceTx` doesn't have it
|
|
209
|
-
signature:
|
|
207
|
+
signature: base64ToBytes((await (await this.getDidSigner(signInput.verificationMethodId, payload.verificationMethod))(hexToBytes(signInput.privateKeyHex))(signBytes)) as string)
|
|
210
208
|
}
|
|
211
209
|
}))
|
|
212
210
|
|
|
213
211
|
return signInfos
|
|
214
212
|
}
|
|
215
213
|
|
|
216
|
-
async signUpdateDidTx(signInputs: ISignInputs[], payload:
|
|
214
|
+
async signUpdateDidTx(signInputs: ISignInputs[], payload: MsgUpdateDidDocPayload): Promise<SignInfo[]> {
|
|
217
215
|
await this.checkDidSigners(payload?.verificationMethod)
|
|
218
216
|
|
|
219
|
-
const signBytes =
|
|
217
|
+
const signBytes = MsgUpdateDidDocPayload.encode(payload).finish()
|
|
220
218
|
const signInfos: SignInfo[] = await Promise.all(signInputs.map(async (signInput) => {
|
|
221
219
|
return {
|
|
222
220
|
verificationMethodId: signInput.verificationMethodId,
|
|
223
221
|
// TODO: We can't rely on `payload.verificationMethod` here because `CreateResourceTx` doesn't have it
|
|
224
|
-
signature:
|
|
222
|
+
signature: base64ToBytes((await (await this.getDidSigner(signInput.verificationMethodId, payload.verificationMethod))(hexToBytes(signInput.privateKeyHex))(signBytes)) as string)
|
|
223
|
+
}
|
|
224
|
+
}))
|
|
225
|
+
|
|
226
|
+
return signInfos
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async signDeactivateDidTx(signInputs: ISignInputs[], payload: MsgDeactivateDidDocPayload, verificationMethod: VerificationMethod[]): Promise<SignInfo[]> {
|
|
230
|
+
await this.checkDidSigners(verificationMethod)
|
|
231
|
+
|
|
232
|
+
const signBytes = MsgDeactivateDidDocPayload.encode(payload).finish()
|
|
233
|
+
const signInfos: SignInfo[] = await Promise.all(signInputs.map(async (signInput) => {
|
|
234
|
+
return {
|
|
235
|
+
verificationMethodId: signInput.verificationMethodId,
|
|
236
|
+
// TODO: We can't rely on `payload.verificationMethod` here because `CreateResourceTx` doesn't have it
|
|
237
|
+
signature: base64ToBytes((await (await this.getDidSigner(signInput.verificationMethodId, verificationMethod))(hexToBytes(signInput.privateKeyHex))(signBytes)) as string)
|
|
225
238
|
}
|
|
226
239
|
}))
|
|
227
240
|
|
|
@@ -254,7 +267,7 @@ export class CheqdSigningStargateClient extends SigningStargateClient {
|
|
|
254
267
|
|
|
255
268
|
signInfos.push({
|
|
256
269
|
verificationMethodId: signInput.verificationMethodId,
|
|
257
|
-
signature:
|
|
270
|
+
signature: base64ToBytes(signature)
|
|
258
271
|
});
|
|
259
272
|
}
|
|
260
273
|
|
package/src/types.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { CheqdSDK } from "."
|
|
2
2
|
import { Coin } from "@cosmjs/proto-signing"
|
|
3
3
|
import { Signer } from "did-jwt"
|
|
4
|
+
import { MsgDeactivateDidDocPayload, VerificationMethod } from "@cheqd/ts-proto/cheqd/did/v2"
|
|
4
5
|
|
|
5
6
|
export enum CheqdNetwork {
|
|
6
7
|
Mainnet = 'mainnet',
|
|
@@ -66,4 +67,8 @@ export interface DidStdFee {
|
|
|
66
67
|
readonly gas: string
|
|
67
68
|
payer?: string
|
|
68
69
|
granter?: string
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface MsgDeactivateDidPayload extends MsgDeactivateDidDocPayload {
|
|
73
|
+
verificationMethod: VerificationMethod[]
|
|
69
74
|
}
|
package/src/utils.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
MsgCreateDidDocPayload,
|
|
3
|
+
MsgUpdateDidDocPayload,
|
|
4
|
+
VerificationMethod
|
|
5
|
+
} from "@cheqd/ts-proto/cheqd/did/v2"
|
|
2
6
|
import {
|
|
3
7
|
IKeyPair,
|
|
4
8
|
IKeyValuePair,
|
|
@@ -16,7 +20,7 @@ import { bases } from "multiformats/basics"
|
|
|
16
20
|
import { base64ToBytes } from "did-jwt"
|
|
17
21
|
import { generateKeyPair, generateKeyPairFromSeed, KeyPair } from '@stablelib/ed25519'
|
|
18
22
|
import { v4 } from 'uuid'
|
|
19
|
-
import {
|
|
23
|
+
import { createHash } from 'crypto'
|
|
20
24
|
|
|
21
25
|
export type TImportableEd25519Key = {
|
|
22
26
|
publicKeyHex: string
|
|
@@ -25,11 +29,10 @@ export type TImportableEd25519Key = {
|
|
|
25
29
|
type: "Ed25519"
|
|
26
30
|
}
|
|
27
31
|
|
|
28
|
-
|
|
32
|
+
// multicodec ed25519-pub header as varint
|
|
33
|
+
const MULTICODEC_ED25519_PUB_HEADER = new Uint8Array([0xed, 0x01]);
|
|
29
34
|
|
|
30
|
-
export
|
|
31
|
-
return Object.entries(object).map(([key, value]) => ({ key, value }))
|
|
32
|
-
}
|
|
35
|
+
export type IdentifierPayload = Partial<MsgCreateDidDocPayload> | Partial<MsgUpdateDidDocPayload>
|
|
33
36
|
|
|
34
37
|
export function isEqualKeyValuePair(kv1: IKeyValuePair[], kv2: IKeyValuePair[]): boolean {
|
|
35
38
|
return kv1.every((item, index) => item.key === kv2[index].key && item.value === kv2[index].value)
|
|
@@ -44,7 +47,7 @@ export function createSignInputsFromImportableEd25519Key(key: TImportableEd25519
|
|
|
44
47
|
switch (method?.type) {
|
|
45
48
|
case VerificationMethods.Base58:
|
|
46
49
|
const publicKeyMultibase = bases['base58btc'].encode(publicKey)
|
|
47
|
-
if (method.publicKeyMultibase === publicKeyMultibase) {
|
|
50
|
+
if ((JSON.parse(method.verificationMaterial)).publicKeyMultibase === publicKeyMultibase) {
|
|
48
51
|
return {
|
|
49
52
|
verificationMethodId: method.id,
|
|
50
53
|
privateKeyHex: key.privateKeyHex
|
|
@@ -52,12 +55,12 @@ export function createSignInputsFromImportableEd25519Key(key: TImportableEd25519
|
|
|
52
55
|
}
|
|
53
56
|
|
|
54
57
|
case VerificationMethods.JWK:
|
|
55
|
-
const publicKeyJWK =
|
|
58
|
+
const publicKeyJWK = {
|
|
56
59
|
crv: 'Ed25519',
|
|
57
60
|
kty: 'OKP',
|
|
58
61
|
x: toString( publicKey, 'base64url' )
|
|
59
|
-
}
|
|
60
|
-
if (
|
|
62
|
+
}
|
|
63
|
+
if ((JSON.parse(method.verificationMaterial)).publicKeyJwk, publicKeyJWK) {
|
|
61
64
|
return {
|
|
62
65
|
verificationMethodId: method.id,
|
|
63
66
|
privateKeyHex: key.privateKeyHex
|
|
@@ -95,7 +98,7 @@ export function createVerificationKeys(keyPair: IKeyPair, algo: MethodSpecificId
|
|
|
95
98
|
switch (algo) {
|
|
96
99
|
case MethodSpecificIdAlgo.Base58:
|
|
97
100
|
methodSpecificId = bases['base58btc'].encode(base64ToBytes(keyPair.publicKey))
|
|
98
|
-
didUrl = `did:cheqd:${network}:${
|
|
101
|
+
didUrl = `did:cheqd:${network}:${(bases['base58btc'].encode((fromString(sha256(keyPair.publicKey))).slice(0,16))).slice(1)}`
|
|
99
102
|
return {
|
|
100
103
|
methodSpecificId,
|
|
101
104
|
didUrl,
|
|
@@ -122,8 +125,10 @@ export function createDidVerificationMethod(verificationMethodTypes: Verificatio
|
|
|
122
125
|
id: verificationKeys[_].keyId,
|
|
123
126
|
type: type,
|
|
124
127
|
controller: verificationKeys[_].didUrl,
|
|
125
|
-
|
|
126
|
-
|
|
128
|
+
verificationMaterial: JSON.stringify({
|
|
129
|
+
publicKeyMultibase: verificationKeys[_].methodSpecificId,
|
|
130
|
+
publicKeyJwk: []
|
|
131
|
+
})
|
|
127
132
|
}
|
|
128
133
|
|
|
129
134
|
case VerificationMethods.JWK:
|
|
@@ -131,32 +136,33 @@ export function createDidVerificationMethod(verificationMethodTypes: Verificatio
|
|
|
131
136
|
id: verificationKeys[_].keyId,
|
|
132
137
|
type: type,
|
|
133
138
|
controller: verificationKeys[_].didUrl,
|
|
134
|
-
|
|
135
|
-
{
|
|
139
|
+
verificationMaterial: JSON.stringify({
|
|
140
|
+
publicKeyJwk: {
|
|
136
141
|
crv: 'Ed25519',
|
|
137
142
|
kty: 'OKP',
|
|
138
143
|
x: toString( fromString( verificationKeys[_].publicKey, 'base64pad' ), 'base64url' )
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
|
|
144
|
+
},
|
|
145
|
+
publicKeyMultibase: ''
|
|
146
|
+
})
|
|
142
147
|
}
|
|
143
148
|
}
|
|
144
149
|
}) ?? []
|
|
145
150
|
}
|
|
146
151
|
|
|
147
|
-
export function createDidPayload(verificationMethods: VerificationMethod[], verificationKeys: IVerificationKeys[]):
|
|
152
|
+
export function createDidPayload(verificationMethods: VerificationMethod[], verificationKeys: IVerificationKeys[]): MsgCreateDidDocPayload {
|
|
148
153
|
if (!verificationMethods || verificationMethods.length === 0)
|
|
149
154
|
throw new Error('No verification methods provided')
|
|
150
155
|
if (!verificationKeys || verificationKeys.length === 0)
|
|
151
156
|
throw new Error('No verification keys provided')
|
|
152
157
|
|
|
153
158
|
const did = verificationKeys[0].didUrl
|
|
154
|
-
return
|
|
159
|
+
return MsgCreateDidDocPayload.fromPartial(
|
|
155
160
|
{
|
|
156
161
|
id: did,
|
|
157
162
|
controller: verificationKeys.map(key => key.didUrl),
|
|
158
163
|
verificationMethod: verificationMethods,
|
|
159
|
-
authentication: verificationKeys.map(key => key.keyId)
|
|
164
|
+
authentication: verificationKeys.map(key => key.keyId),
|
|
165
|
+
versionId: v4()
|
|
160
166
|
}
|
|
161
167
|
)
|
|
162
168
|
}
|
|
@@ -172,7 +178,7 @@ export function createDidPayloadWithSignInputs(seed?: string, keys?: IKeyPair[])
|
|
|
172
178
|
const verificationKeys = keys.map((key, i) => createVerificationKeys(key, key.algo || MethodSpecificIdAlgo.Base58, `key-${i}`))
|
|
173
179
|
const verificationMethod = createDidVerificationMethod(verificationMethodTypes, verificationKeys)
|
|
174
180
|
|
|
175
|
-
let payload : Partial<
|
|
181
|
+
let payload : Partial<MsgCreateDidDocPayload> = {
|
|
176
182
|
id: verificationKeys[0].didUrl,
|
|
177
183
|
controller: verificationKeys.map(key => key.didUrl),
|
|
178
184
|
verificationMethod: verificationMethod,
|
|
@@ -182,7 +188,7 @@ export function createDidPayloadWithSignInputs(seed?: string, keys?: IKeyPair[])
|
|
|
182
188
|
const keyHexs = keys.map((key)=>convertKeyPairtoTImportableEd25519Key(key))
|
|
183
189
|
const signInputs = keyHexs.map((key)=>createSignInputsFromImportableEd25519Key(key, verificationMethod))
|
|
184
190
|
|
|
185
|
-
return { didPayload:
|
|
191
|
+
return { didPayload: MsgCreateDidDocPayload.fromPartial(payload), keys, signInputs }
|
|
186
192
|
}
|
|
187
193
|
|
|
188
194
|
export function convertKeyPairtoTImportableEd25519Key(keyPair: IKeyPair) : TImportableEd25519Key {
|
|
@@ -199,3 +205,18 @@ export function createSignInputsFromKeyPair(didDocument: IdentifierPayload, keys
|
|
|
199
205
|
const signInputs = keyHexs.map((key)=>createSignInputsFromImportableEd25519Key(key, didDocument.verificationMethod!))
|
|
200
206
|
return signInputs
|
|
201
207
|
}
|
|
208
|
+
|
|
209
|
+
function sha256(message: string) {
|
|
210
|
+
return createHash('sha256').update(message).digest('hex')
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// encode a multibase base58-btc multicodec key
|
|
214
|
+
// function _encodeMbKey(header: any, key: any) {
|
|
215
|
+
// const mbKey = new Uint8Array(header.length + key.length);
|
|
216
|
+
|
|
217
|
+
// mbKey.set(header);
|
|
218
|
+
// mbKey.set(key, header.length);
|
|
219
|
+
|
|
220
|
+
// return bases['base58btc'].encode(mbKey);
|
|
221
|
+
// }
|
|
222
|
+
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MsgUpdateDidDocPayload } from "@cheqd/ts-proto/cheqd/did/v2"
|
|
2
2
|
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing"
|
|
3
3
|
import { DeliverTxResponse } from "@cosmjs/stargate"
|
|
4
4
|
import { fromString, toString } from 'uint8arrays'
|
|
5
|
+
import { v4 } from "uuid"
|
|
5
6
|
import { DIDModule } from "../../src"
|
|
6
7
|
import { createDefaultCheqdRegistry } from "../../src/registry"
|
|
7
8
|
import { CheqdSigningStargateClient } from "../../src/signer"
|
|
@@ -41,10 +42,10 @@ describe('DIDModule', () => {
|
|
|
41
42
|
amount: [
|
|
42
43
|
{
|
|
43
44
|
denom: 'ncheq',
|
|
44
|
-
amount: '
|
|
45
|
+
amount: '5000000000'
|
|
45
46
|
}
|
|
46
47
|
],
|
|
47
|
-
gas: '
|
|
48
|
+
gas: '200000',
|
|
48
49
|
payer: (await wallet.getAccounts())[0].address
|
|
49
50
|
}
|
|
50
51
|
const didTx: DeliverTxResponse = await didModule.createDidTx(
|
|
@@ -69,6 +70,7 @@ describe('DIDModule', () => {
|
|
|
69
70
|
const verificationKeys = createVerificationKeys(keyPair, MethodSpecificIdAlgo.Uuid, 'key-1', 16)
|
|
70
71
|
const verificationMethods = createDidVerificationMethod([VerificationMethods.Base58], [verificationKeys])
|
|
71
72
|
const didPayload = createDidPayload(verificationMethods, [verificationKeys])
|
|
73
|
+
didPayload.versionId = v4()
|
|
72
74
|
const signInputs: ISignInputs[] = [
|
|
73
75
|
{
|
|
74
76
|
verificationMethodId: didPayload.verificationMethod[0].id,
|
|
@@ -79,10 +81,10 @@ describe('DIDModule', () => {
|
|
|
79
81
|
amount: [
|
|
80
82
|
{
|
|
81
83
|
denom: 'ncheq',
|
|
82
|
-
amount: '
|
|
84
|
+
amount: '5000000000'
|
|
83
85
|
}
|
|
84
86
|
],
|
|
85
|
-
gas: '
|
|
87
|
+
gas: '200000',
|
|
86
88
|
payer: (await wallet.getAccounts())[0].address
|
|
87
89
|
}
|
|
88
90
|
const didTx: DeliverTxResponse = await didModule.createDidTx(
|
|
@@ -120,10 +122,10 @@ describe('DIDModule', () => {
|
|
|
120
122
|
amount: [
|
|
121
123
|
{
|
|
122
124
|
denom: 'ncheq',
|
|
123
|
-
amount: '
|
|
125
|
+
amount: '5000000000'
|
|
124
126
|
}
|
|
125
127
|
],
|
|
126
|
-
gas: '
|
|
128
|
+
gas: '200000',
|
|
127
129
|
payer: (await wallet.getAccounts())[0].address
|
|
128
130
|
}
|
|
129
131
|
const didTx: DeliverTxResponse = await didModule.createDidTx(
|
|
@@ -139,7 +141,7 @@ describe('DIDModule', () => {
|
|
|
139
141
|
expect(didTx.code).toBe(0)
|
|
140
142
|
|
|
141
143
|
// Update the DID
|
|
142
|
-
const updateDidPayload =
|
|
144
|
+
const updateDidPayload = MsgUpdateDidDocPayload.fromPartial({
|
|
143
145
|
context: didPayload.context,
|
|
144
146
|
id: didPayload.id,
|
|
145
147
|
controller: didPayload.controller,
|
|
@@ -6,7 +6,7 @@ import { createDefaultCheqdRegistry } from "../../src/registry"
|
|
|
6
6
|
import { CheqdSigningStargateClient } from "../../src/signer"
|
|
7
7
|
import { DidStdFee, ISignInputs, MethodSpecificIdAlgo, VerificationMethods } from '../../src/types';
|
|
8
8
|
import { createDidPayload, createDidVerificationMethod, createKeyPairBase64, createVerificationKeys, exampleCheqdNetwork, faucet } from "../testutils.test"
|
|
9
|
-
import { MsgCreateResourcePayload } from '@cheqd/ts-proto/resource/
|
|
9
|
+
import { MsgCreateResourcePayload } from '@cheqd/ts-proto/cheqd/resource/v2';
|
|
10
10
|
import { randomUUID } from "crypto"
|
|
11
11
|
|
|
12
12
|
const defaultAsyncTxTimeout = 30000
|
|
@@ -48,10 +48,10 @@ describe('ResourceModule', () => {
|
|
|
48
48
|
amount: [
|
|
49
49
|
{
|
|
50
50
|
denom: 'ncheq',
|
|
51
|
-
amount: '
|
|
51
|
+
amount: '2500000000'
|
|
52
52
|
}
|
|
53
53
|
],
|
|
54
|
-
gas: '
|
|
54
|
+
gas: '200000',
|
|
55
55
|
payer: (await wallet.getAccounts())[0].address
|
|
56
56
|
}
|
|
57
57
|
|
|
@@ -74,6 +74,8 @@ describe('ResourceModule', () => {
|
|
|
74
74
|
const resourcePayload: MsgCreateResourcePayload = {
|
|
75
75
|
collectionId: didPayload.id.split(":").reverse()[0],
|
|
76
76
|
id: randomUUID(),
|
|
77
|
+
version: "1.0",
|
|
78
|
+
alsoKnownAs: [],
|
|
77
79
|
name: 'Test Resource',
|
|
78
80
|
resourceType: 'test-resource-type',
|
|
79
81
|
data: new TextEncoder().encode("{ \"message\": \"hello world\"}")
|
package/tests/signer.test.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { VerificationMethod } from "@cheqd/ts-proto/cheqd/
|
|
2
|
-
import { MsgCreateDid, MsgCreateDidPayload, SignInfo } from "@cheqd/ts-proto/cheqd/v1/tx"
|
|
1
|
+
import { MsgCreateDidDoc, MsgCreateDidDocPayload, VerificationMethod } from "@cheqd/ts-proto/cheqd/did/v2"
|
|
3
2
|
import { DirectSecp256k1HdWallet, Registry } from "@cosmjs/proto-signing"
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import { EdDSASigner } from "did-jwt"
|
|
4
|
+
import { typeUrlMsgCreateDidDoc } from '../src/modules/did'
|
|
6
5
|
import { CheqdSigningStargateClient } from "../src/signer"
|
|
7
6
|
import { ISignInputs, MethodSpecificIdAlgo, VerificationMethods } from "../src/types"
|
|
8
7
|
import { fromString, toString } from 'uint8arrays'
|
|
@@ -42,9 +41,9 @@ describe('CheqdSigningStargateClient', () => {
|
|
|
42
41
|
it('can be constructed with cheqd custom registry', async () => {
|
|
43
42
|
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic)
|
|
44
43
|
const registry = new Registry()
|
|
45
|
-
registry.register(
|
|
44
|
+
registry.register(typeUrlMsgCreateDidDoc, MsgCreateDidDoc)
|
|
46
45
|
const signer = await CheqdSigningStargateClient.connectWithSigner(exampleCheqdNetwork.rpcUrl, wallet, { registry })
|
|
47
|
-
expect(signer.registry.lookupType(
|
|
46
|
+
expect(signer.registry.lookupType(typeUrlMsgCreateDidDoc)).toBe(MsgCreateDidDoc)
|
|
48
47
|
})
|
|
49
48
|
})
|
|
50
49
|
|
|
@@ -116,7 +115,7 @@ describe('CheqdSigningStargateClient', () => {
|
|
|
116
115
|
id: nonExistingKeyId,
|
|
117
116
|
type: nonExistingVerificationMethod,
|
|
118
117
|
controller: nonExistingDid,
|
|
119
|
-
publicKeyMultibase: nonExistingPublicKeyMultibase
|
|
118
|
+
verificationMaterial: JSON.stringify({publicKeyMultibase: nonExistingPublicKeyMultibase})
|
|
120
119
|
}
|
|
121
120
|
|
|
122
121
|
await expect(signer.checkDidSigners([VerificationMethod.fromPartial(verificationMethod)])).rejects.toThrow()
|
|
@@ -139,12 +138,12 @@ describe('CheqdSigningStargateClient', () => {
|
|
|
139
138
|
]
|
|
140
139
|
const signInfos = await signer.signCreateDidTx(signInputs, didPayload)
|
|
141
140
|
const publicKeyRaw = fromString(keyPair.publicKey, 'base64')
|
|
142
|
-
const messageRaw =
|
|
143
|
-
|
|
141
|
+
const messageRaw = MsgCreateDidDocPayload.encode(didPayload).finish()
|
|
142
|
+
|
|
144
143
|
const verified = verify(
|
|
145
144
|
publicKeyRaw,
|
|
146
145
|
messageRaw,
|
|
147
|
-
|
|
146
|
+
signInfos[0].signature
|
|
148
147
|
)
|
|
149
148
|
|
|
150
149
|
expect(verified).toBe(true)
|