@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.
- package/package.json +9 -3
- package/.github/ISSUE_TEMPLATE/bug-report.yml +0 -74
- package/.github/ISSUE_TEMPLATE/config.yml +0 -14
- package/.github/ISSUE_TEMPLATE/feature-request.yaml +0 -27
- package/.github/dependabot.yml +0 -43
- package/.github/linters/.commitlint.rules.cjs +0 -37
- package/.github/linters/.eslintrc.json +0 -18
- package/.github/linters/.markdown-lint.yml +0 -139
- package/.github/linters/mlc_config.json +0 -13
- package/.github/workflows/build.yml +0 -26
- package/.github/workflows/cleanup-actions.yml +0 -45
- package/.github/workflows/cleanup-cache-automatic.yml +0 -24
- package/.github/workflows/cleanup-cache-manual.yml +0 -21
- package/.github/workflows/codeql.yml +0 -40
- package/.github/workflows/dispatch.yml +0 -30
- package/.github/workflows/lint.yml +0 -51
- package/.github/workflows/pull-request.yml +0 -48
- package/.github/workflows/release.yml +0 -42
- package/.github/workflows/test.yml +0 -66
- package/.releaserc.json +0 -61
- package/CHANGELOG.md +0 -320
- package/CODE_OF_CONDUCT.md +0 -81
- package/NOTICE.md +0 -10
- package/SECURITY.md +0 -12
- package/docker/Dockerfile +0 -55
- package/docker/entrypoint.sh +0 -58
- package/docker/localnet/build-latest.env +0 -7
- package/docker/localnet/container-env/observer-0.env +0 -13
- package/docker/localnet/container-env/seed-0.env +0 -17
- package/docker/localnet/container-env/validator-0.env +0 -13
- package/docker/localnet/container-env/validator-1.env +0 -13
- package/docker/localnet/container-env/validator-2.env +0 -13
- package/docker/localnet/container-env/validator-3.env +0 -13
- package/docker/localnet/docker-compose.yml +0 -281
- package/docker/localnet/gen-network-config.sh +0 -259
- package/docker/localnet/import-keys.sh +0 -31
- package/jest.config.cjs +0 -20
- package/src/index.ts +0 -192
- package/src/modules/_.ts +0 -61
- package/src/modules/did.ts +0 -601
- package/src/modules/resource.ts +0 -247
- package/src/querier.ts +0 -36
- package/src/registry.ts +0 -13
- package/src/signer.ts +0 -297
- package/src/types.ts +0 -110
- package/src/utils.ts +0 -246
- package/tests/index.test.ts +0 -155
- package/tests/modules/did.test.ts +0 -985
- package/tests/modules/resource.test.ts +0 -991
- package/tests/signer.test.ts +0 -202
- package/tests/testutils.test.ts +0 -29
- package/tests/utils.test.ts +0 -77
- package/tsconfig.cjs.json +0 -8
- package/tsconfig.esm.json +0 -8
- package/tsconfig.json +0 -84
- package/tsconfig.types.json +0 -10
package/src/modules/resource.ts
DELETED
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AbstractCheqdSDKModule,
|
|
3
|
-
MinimalImportableCheqdSDKModule
|
|
4
|
-
} from './_.js';
|
|
5
|
-
import { CheqdSigningStargateClient } from "../signer.js"
|
|
6
|
-
import {
|
|
7
|
-
EncodeObject,
|
|
8
|
-
GeneratedType
|
|
9
|
-
} from "@cosmjs/proto-signing"
|
|
10
|
-
import {
|
|
11
|
-
DidStdFee,
|
|
12
|
-
IContext,
|
|
13
|
-
ISignInputs,
|
|
14
|
-
QueryExtensionSetup
|
|
15
|
-
} from '../types.js';
|
|
16
|
-
import {
|
|
17
|
-
Metadata,
|
|
18
|
-
MsgCreateResource,
|
|
19
|
-
MsgCreateResourcePayload,
|
|
20
|
-
MsgCreateResourceResponse,
|
|
21
|
-
QueryClientImpl,
|
|
22
|
-
QueryCollectionResourcesResponse,
|
|
23
|
-
ResourceWithMetadata,
|
|
24
|
-
protobufPackage
|
|
25
|
-
} from "@cheqd/ts-proto/cheqd/resource/v2/index.js"
|
|
26
|
-
import {
|
|
27
|
-
DeliverTxResponse,
|
|
28
|
-
QueryClient,
|
|
29
|
-
createPagination,
|
|
30
|
-
createProtobufRpcClient
|
|
31
|
-
} from "@cosmjs/stargate"
|
|
32
|
-
import { SignInfo } from "@cheqd/ts-proto/cheqd/did/v2/index.js";
|
|
33
|
-
import { fileTypeFromBuffer } from "file-type";
|
|
34
|
-
import { assert } from '@cosmjs/utils';
|
|
35
|
-
import { PageRequest } from '@cheqd/ts-proto/cosmos/base/query/v1beta1/pagination.js';
|
|
36
|
-
import { CheqdQuerier } from '../querier.js';
|
|
37
|
-
|
|
38
|
-
export const defaultResourceExtensionKey = 'resource' as const
|
|
39
|
-
|
|
40
|
-
export const protobufLiterals = {
|
|
41
|
-
MsgCreateResource: 'MsgCreateResource',
|
|
42
|
-
MsgCreateResourceResponse: 'MsgCreateResourceResponse'
|
|
43
|
-
} as const
|
|
44
|
-
|
|
45
|
-
export const typeUrlMsgCreateResource = `/${protobufPackage}.${protobufLiterals.MsgCreateResource}`
|
|
46
|
-
export const typeUrlMsgCreateResourceResponse = `/${protobufPackage}.${protobufLiterals.MsgCreateResourceResponse}`
|
|
47
|
-
|
|
48
|
-
export interface MsgCreateResourceEncodeObject extends EncodeObject {
|
|
49
|
-
readonly typeUrl: typeof typeUrlMsgCreateResource,
|
|
50
|
-
readonly value: Partial<MsgCreateResource>
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function isMsgCreateResourceEncodeObject(obj: EncodeObject): obj is MsgCreateResourceEncodeObject {
|
|
54
|
-
return obj.typeUrl === typeUrlMsgCreateResource
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export type MinimalImportableResourceModule = MinimalImportableCheqdSDKModule<ResourceModule>
|
|
58
|
-
|
|
59
|
-
export type ResourceExtension = {
|
|
60
|
-
readonly [defaultResourceExtensionKey]: {
|
|
61
|
-
readonly resource: (collectionId: string, resourceId: string) => Promise<ResourceWithMetadata>
|
|
62
|
-
readonly resourceMetadata: (collectionId: string, resourceId: string) => Promise<Metadata>
|
|
63
|
-
readonly collectionResources: (collectionId: string, paginationKey?: Uint8Array) => Promise<QueryCollectionResourcesResponse>
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export const setupResourceExtension = (base: QueryClient): ResourceExtension => {
|
|
68
|
-
const rpc = createProtobufRpcClient(base)
|
|
69
|
-
|
|
70
|
-
const queryService = new QueryClientImpl(rpc)
|
|
71
|
-
|
|
72
|
-
return {
|
|
73
|
-
[defaultResourceExtensionKey]: {
|
|
74
|
-
resource: async (collectionId: string, resourceId: string) => {
|
|
75
|
-
const { resource } = await queryService.Resource({ collectionId, id: resourceId })
|
|
76
|
-
assert(resource)
|
|
77
|
-
return resource
|
|
78
|
-
},
|
|
79
|
-
resourceMetadata: async (collectionId: string, resourceId: string) => {
|
|
80
|
-
const { resource } = await queryService.ResourceMetadata({ collectionId, id: resourceId })
|
|
81
|
-
assert(resource)
|
|
82
|
-
return resource
|
|
83
|
-
},
|
|
84
|
-
collectionResources: async (collectionId: string, paginationKey?: Uint8Array) => {
|
|
85
|
-
const response = await queryService.CollectionResources({ collectionId, pagination: createPagination(paginationKey) as PageRequest | undefined })
|
|
86
|
-
return response
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
} as ResourceExtension
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export class ResourceModule extends AbstractCheqdSDKModule {
|
|
93
|
-
static readonly registryTypes: Iterable<[string, GeneratedType]> = [
|
|
94
|
-
[typeUrlMsgCreateResource, MsgCreateResource],
|
|
95
|
-
[typeUrlMsgCreateResourceResponse, MsgCreateResourceResponse]
|
|
96
|
-
]
|
|
97
|
-
|
|
98
|
-
static readonly baseMinimalDenom = 'ncheq' as const
|
|
99
|
-
|
|
100
|
-
static readonly fees = {
|
|
101
|
-
DefaultCreateResourceImageFee: { amount: '10000000000', denom: ResourceModule.baseMinimalDenom } as const,
|
|
102
|
-
DefaultCreateResourceJsonFee: { amount: '2500000000', denom: ResourceModule.baseMinimalDenom } as const,
|
|
103
|
-
DefaultCreateResourceDefaultFee: { amount: '5000000000', denom: ResourceModule.baseMinimalDenom } as const,
|
|
104
|
-
} as const
|
|
105
|
-
|
|
106
|
-
static readonly querierExtensionSetup: QueryExtensionSetup<ResourceExtension> = setupResourceExtension
|
|
107
|
-
|
|
108
|
-
querier: CheqdQuerier & ResourceExtension
|
|
109
|
-
|
|
110
|
-
constructor(signer: CheqdSigningStargateClient, querier: CheqdQuerier & ResourceExtension) {
|
|
111
|
-
super(signer, querier)
|
|
112
|
-
this.querier = querier
|
|
113
|
-
this.methods = {
|
|
114
|
-
createLinkedResourceTx: this.createLinkedResourceTx.bind(this),
|
|
115
|
-
queryLinkedResource: this.queryLinkedResource.bind(this),
|
|
116
|
-
queryLinkedResourceMetadata: this.queryLinkedResourceMetadata.bind(this),
|
|
117
|
-
queryLinkedResources: this.queryLinkedResources.bind(this),
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
public getRegistryTypes(): Iterable<[string, GeneratedType]> {
|
|
122
|
-
return ResourceModule.registryTypes
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
public getQuerierExtensionSetup(): QueryExtensionSetup<ResourceExtension> {
|
|
126
|
-
return ResourceModule.querierExtensionSetup
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
static async signPayload(payload: MsgCreateResourcePayload, signInputs: ISignInputs[] | SignInfo[]): Promise<MsgCreateResource> {
|
|
130
|
-
const signBytes = MsgCreateResourcePayload.encode(payload).finish()
|
|
131
|
-
let signatures: SignInfo[]
|
|
132
|
-
if(ISignInputs.isSignInput(signInputs)) {
|
|
133
|
-
signatures = await CheqdSigningStargateClient.signIdentityTx(signBytes, signInputs)
|
|
134
|
-
} else {
|
|
135
|
-
signatures = signInputs
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
return {
|
|
139
|
-
payload,
|
|
140
|
-
signatures
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
async createLinkedResourceTx(signInputs: ISignInputs[] | SignInfo[], resourcePayload: Partial<MsgCreateResourcePayload>, address: string, fee?: DidStdFee | 'auto' | number, memo?: string, context?: IContext): Promise<DeliverTxResponse> {
|
|
145
|
-
if (!this._signer) {
|
|
146
|
-
this._signer = context!.sdk!.signer
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
const payload = MsgCreateResourcePayload.fromPartial(resourcePayload)
|
|
150
|
-
|
|
151
|
-
const msg = await ResourceModule.signPayload(payload, signInputs)
|
|
152
|
-
|
|
153
|
-
const encObj: MsgCreateResourceEncodeObject = {
|
|
154
|
-
typeUrl: typeUrlMsgCreateResource,
|
|
155
|
-
value: msg
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
if (address === '') {
|
|
159
|
-
address = (await context!.sdk!.options.wallet.getAccounts())[0].address
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (!fee) {
|
|
163
|
-
if (payload.data.length === 0) {
|
|
164
|
-
throw new Error('Linked resource data is empty')
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
fee = await async function() {
|
|
168
|
-
const mimeType = await ResourceModule.readMimeType(payload.data)
|
|
169
|
-
|
|
170
|
-
if (mimeType.startsWith('image/')) {
|
|
171
|
-
return await ResourceModule.generateCreateResourceImageFees(address)
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
if (mimeType.startsWith('application/json')) {
|
|
175
|
-
return await ResourceModule.generateCreateResourceJsonFees(address)
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
return await ResourceModule.generateCreateResourceDefaultFees(address)
|
|
179
|
-
}()
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return this._signer.signAndBroadcast(
|
|
183
|
-
address,
|
|
184
|
-
[encObj],
|
|
185
|
-
fee!,
|
|
186
|
-
memo
|
|
187
|
-
)
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
async queryLinkedResource(collectionId: string, resourceId: string, context?: IContext): Promise<ResourceWithMetadata> {
|
|
191
|
-
if (!this.querier) {
|
|
192
|
-
this.querier = context!.sdk!.querier
|
|
193
|
-
}
|
|
194
|
-
return await this.querier[defaultResourceExtensionKey].resource(collectionId, resourceId)
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
async queryLinkedResourceMetadata(collectionId: string, resourceId: string, context?: IContext): Promise<Metadata> {
|
|
198
|
-
if (!this.querier) {
|
|
199
|
-
this.querier = context!.sdk!.querier
|
|
200
|
-
}
|
|
201
|
-
return await this.querier[defaultResourceExtensionKey].resourceMetadata(collectionId, resourceId)
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
async queryLinkedResources(collectionId: string, context?: IContext): Promise<QueryCollectionResourcesResponse> {
|
|
205
|
-
if (!this.querier) {
|
|
206
|
-
this.querier = context!.sdk!.querier
|
|
207
|
-
}
|
|
208
|
-
return await this.querier[defaultResourceExtensionKey].collectionResources(collectionId)
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
static async readMimeType(content: Uint8Array): Promise<string> {
|
|
212
|
-
return (await fileTypeFromBuffer(content))?.mime ?? 'application/octet-stream'
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
static async generateCreateResourceImageFees(feePayer: string, granter?: string): Promise<DidStdFee> {
|
|
216
|
-
return {
|
|
217
|
-
amount: [
|
|
218
|
-
ResourceModule.fees.DefaultCreateResourceImageFee
|
|
219
|
-
],
|
|
220
|
-
gas: '360000',
|
|
221
|
-
payer: feePayer,
|
|
222
|
-
granter: granter
|
|
223
|
-
} as DidStdFee
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
static async generateCreateResourceJsonFees(feePayer: string, granter?: string): Promise<DidStdFee> {
|
|
227
|
-
return {
|
|
228
|
-
amount: [
|
|
229
|
-
ResourceModule.fees.DefaultCreateResourceJsonFee
|
|
230
|
-
],
|
|
231
|
-
gas: '360000',
|
|
232
|
-
payer: feePayer,
|
|
233
|
-
granter: granter
|
|
234
|
-
} as DidStdFee
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
static async generateCreateResourceDefaultFees(feePayer: string, granter?: string): Promise<DidStdFee> {
|
|
238
|
-
return {
|
|
239
|
-
amount: [
|
|
240
|
-
ResourceModule.fees.DefaultCreateResourceDefaultFee
|
|
241
|
-
],
|
|
242
|
-
gas: '360000',
|
|
243
|
-
payer: feePayer,
|
|
244
|
-
granter: granter
|
|
245
|
-
} as DidStdFee
|
|
246
|
-
}
|
|
247
|
-
}
|
package/src/querier.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { QueryClient } from "@cosmjs/stargate";
|
|
2
|
-
import { Tendermint34Client } from "@cosmjs/tendermint-rpc";
|
|
3
|
-
import {
|
|
4
|
-
QueryExtensionSetup,
|
|
5
|
-
CheqdExtensions
|
|
6
|
-
} from "./types.js";
|
|
7
|
-
|
|
8
|
-
export class CheqdQuerier extends QueryClient {
|
|
9
|
-
constructor(tmClient: Tendermint34Client) {
|
|
10
|
-
super(tmClient)
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
static async connect(url: string): Promise<CheqdQuerier> {
|
|
14
|
-
const tmClient = await Tendermint34Client.connect(url);
|
|
15
|
-
return new CheqdQuerier(tmClient);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
static async fromClient(client: Tendermint34Client): Promise<CheqdQuerier> {
|
|
19
|
-
return new CheqdQuerier(client);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
static async connectWithExtension(url: string, extension: QueryExtensionSetup<CheqdExtensions>): Promise<CheqdQuerier & CheqdExtensions> {
|
|
23
|
-
const tmClient = await Tendermint34Client.connect(url);
|
|
24
|
-
return CheqdQuerier.withExtensions(tmClient, extension);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
static async connectWithExtensions(url: string, ...extensions: QueryExtensionSetup<CheqdExtensions>[]): Promise<CheqdQuerier & CheqdExtensions> {
|
|
28
|
-
if (extensions.length === 1) {
|
|
29
|
-
return CheqdQuerier.connectWithExtension(url, extensions[0]);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const tmClient = await Tendermint34Client.connect(url);
|
|
33
|
-
const tupleLike = extensions as [QueryExtensionSetup<CheqdExtensions>, QueryExtensionSetup<CheqdExtensions>];
|
|
34
|
-
return CheqdQuerier.withExtensions(tmClient, ...tupleLike);
|
|
35
|
-
}
|
|
36
|
-
}
|
package/src/registry.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Registry,
|
|
3
|
-
GeneratedType,
|
|
4
|
-
} from '@cosmjs/proto-signing'
|
|
5
|
-
|
|
6
|
-
import { defaultRegistryTypes } from '@cosmjs/stargate'
|
|
7
|
-
|
|
8
|
-
export function createDefaultCheqdRegistry(customTypes?: Iterable<[string, GeneratedType]>): Registry {
|
|
9
|
-
if (!customTypes) customTypes = [];
|
|
10
|
-
return new Registry([...defaultRegistryTypes, ...customTypes])
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const CheqdRegistry = new Registry(defaultRegistryTypes)
|
package/src/signer.ts
DELETED
|
@@ -1,297 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
EncodeObject,
|
|
3
|
-
isOfflineDirectSigner,
|
|
4
|
-
OfflineSigner,
|
|
5
|
-
encodePubkey,
|
|
6
|
-
TxBodyEncodeObject,
|
|
7
|
-
makeSignDoc
|
|
8
|
-
} from "@cosmjs/proto-signing"
|
|
9
|
-
import {
|
|
10
|
-
DeliverTxResponse,
|
|
11
|
-
GasPrice,
|
|
12
|
-
HttpEndpoint,
|
|
13
|
-
SigningStargateClient,
|
|
14
|
-
SigningStargateClientOptions,
|
|
15
|
-
calculateFee,
|
|
16
|
-
SignerData } from "@cosmjs/stargate"
|
|
17
|
-
import { Tendermint34Client } from "@cosmjs/tendermint-rpc"
|
|
18
|
-
import { createDefaultCheqdRegistry } from "./registry.js"
|
|
19
|
-
import {
|
|
20
|
-
MsgCreateDidDocPayload,
|
|
21
|
-
SignInfo,
|
|
22
|
-
MsgUpdateDidDocPayload,
|
|
23
|
-
MsgDeactivateDidDocPayload,
|
|
24
|
-
VerificationMethod
|
|
25
|
-
} from '@cheqd/ts-proto/cheqd/did/v2/index.js';
|
|
26
|
-
import {
|
|
27
|
-
DidStdFee,
|
|
28
|
-
ISignInputs,
|
|
29
|
-
TSignerAlgo,
|
|
30
|
-
VerificationMethods
|
|
31
|
-
} from './types.js';
|
|
32
|
-
import {
|
|
33
|
-
base64ToBytes,
|
|
34
|
-
EdDSASigner,
|
|
35
|
-
hexToBytes,
|
|
36
|
-
Signer,
|
|
37
|
-
ES256Signer,
|
|
38
|
-
ES256KSigner
|
|
39
|
-
} from 'did-jwt';
|
|
40
|
-
import {
|
|
41
|
-
assert,
|
|
42
|
-
assertDefined
|
|
43
|
-
} from '@cosmjs/utils'
|
|
44
|
-
import { encodeSecp256k1Pubkey } from '@cosmjs/amino'
|
|
45
|
-
import { Int53 } from '@cosmjs/math'
|
|
46
|
-
import { fromBase64 } from '@cosmjs/encoding'
|
|
47
|
-
import {
|
|
48
|
-
AuthInfo,
|
|
49
|
-
SignerInfo,
|
|
50
|
-
TxRaw
|
|
51
|
-
} from 'cosmjs-types/cosmos/tx/v1beta1/tx.js'
|
|
52
|
-
import { SignMode } from 'cosmjs-types/cosmos/tx/signing/v1beta1/signing.js'
|
|
53
|
-
import { Any } from 'cosmjs-types/google/protobuf/any.js'
|
|
54
|
-
import { Coin } from 'cosmjs-types/cosmos/base/v1beta1/coin.js'
|
|
55
|
-
import Long from 'long'
|
|
56
|
-
|
|
57
|
-
export function calculateDidFee(gasLimit: number, gasPrice: string | GasPrice): DidStdFee {
|
|
58
|
-
return calculateFee(gasLimit, gasPrice)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function makeSignerInfos(
|
|
62
|
-
signers: ReadonlyArray<{ readonly pubkey: Any; readonly sequence: number }>,
|
|
63
|
-
signMode: SignMode,
|
|
64
|
-
): SignerInfo[] {
|
|
65
|
-
return signers.map(
|
|
66
|
-
({ pubkey, sequence }): SignerInfo => ({
|
|
67
|
-
publicKey: pubkey,
|
|
68
|
-
modeInfo: {
|
|
69
|
-
single: { mode: signMode },
|
|
70
|
-
},
|
|
71
|
-
sequence: Long.fromNumber(sequence),
|
|
72
|
-
}),
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function makeDidAuthInfoBytes(
|
|
77
|
-
signers: ReadonlyArray<{ readonly pubkey: Any; readonly sequence: number }>,
|
|
78
|
-
feeAmount: readonly Coin[],
|
|
79
|
-
gasLimit: number,
|
|
80
|
-
feePayer: string,
|
|
81
|
-
signMode = SignMode.SIGN_MODE_DIRECT,
|
|
82
|
-
): Uint8Array {
|
|
83
|
-
const authInfo = {
|
|
84
|
-
signerInfos: makeSignerInfos(signers, signMode),
|
|
85
|
-
fee: {
|
|
86
|
-
amount: [...feeAmount],
|
|
87
|
-
gasLimit: Long.fromNumber(gasLimit),
|
|
88
|
-
payer: feePayer
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return AuthInfo.encode(AuthInfo.fromPartial(authInfo)).finish()
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export class CheqdSigningStargateClient extends SigningStargateClient {
|
|
95
|
-
private didSigners: TSignerAlgo = {}
|
|
96
|
-
private readonly _gasPrice: GasPrice | undefined
|
|
97
|
-
private readonly _signer: OfflineSigner
|
|
98
|
-
|
|
99
|
-
public static async connectWithSigner(endpoint: string | HttpEndpoint, signer: OfflineSigner, options?: SigningStargateClientOptions | undefined): Promise<CheqdSigningStargateClient> {
|
|
100
|
-
const tmClient = await Tendermint34Client.connect(endpoint)
|
|
101
|
-
return new CheqdSigningStargateClient(tmClient, signer, {
|
|
102
|
-
registry: options?.registry ? options.registry : createDefaultCheqdRegistry(),
|
|
103
|
-
...options
|
|
104
|
-
})
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
constructor(
|
|
108
|
-
tmClient: Tendermint34Client | undefined,
|
|
109
|
-
signer: OfflineSigner,
|
|
110
|
-
options: SigningStargateClientOptions = {}
|
|
111
|
-
) {
|
|
112
|
-
super(tmClient, signer, options)
|
|
113
|
-
this._signer = signer
|
|
114
|
-
if (options.gasPrice) this._gasPrice = options.gasPrice
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
async signAndBroadcast(
|
|
118
|
-
signerAddress: string,
|
|
119
|
-
messages: readonly EncodeObject[],
|
|
120
|
-
fee: DidStdFee | "auto" | number,
|
|
121
|
-
memo = "",
|
|
122
|
-
): Promise<DeliverTxResponse> {
|
|
123
|
-
let usedFee: DidStdFee
|
|
124
|
-
if (fee == "auto" || typeof fee === "number") {
|
|
125
|
-
assertDefined(this._gasPrice, "Gas price must be set in the client options when auto gas is used.")
|
|
126
|
-
const gasEstimation = await this.simulate(signerAddress, messages, memo)
|
|
127
|
-
const multiplier = typeof fee === "number" ? fee : 1.3
|
|
128
|
-
usedFee = calculateDidFee(Math.round(gasEstimation * multiplier), this._gasPrice)
|
|
129
|
-
usedFee.payer = signerAddress
|
|
130
|
-
} else {
|
|
131
|
-
usedFee = fee
|
|
132
|
-
assertDefined(usedFee.payer, "Payer address must be set when fee is not auto.")
|
|
133
|
-
signerAddress = usedFee.payer!
|
|
134
|
-
}
|
|
135
|
-
const txRaw = await this.sign(signerAddress, messages, usedFee, memo)
|
|
136
|
-
const txBytes = TxRaw.encode(txRaw).finish()
|
|
137
|
-
return this.broadcastTx(txBytes, this.broadcastTimeoutMs, this.broadcastPollIntervalMs)
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
public async sign(
|
|
141
|
-
signerAddress: string,
|
|
142
|
-
messages: readonly EncodeObject[],
|
|
143
|
-
fee: DidStdFee,
|
|
144
|
-
memo: string,
|
|
145
|
-
explicitSignerData?: SignerData,
|
|
146
|
-
): Promise<TxRaw> {
|
|
147
|
-
let signerData: SignerData
|
|
148
|
-
if (explicitSignerData) {
|
|
149
|
-
signerData = explicitSignerData
|
|
150
|
-
} else {
|
|
151
|
-
const { accountNumber, sequence } = await this.getSequence(signerAddress)
|
|
152
|
-
const chainId = await this.getChainId()
|
|
153
|
-
signerData = {
|
|
154
|
-
accountNumber: accountNumber,
|
|
155
|
-
sequence: sequence,
|
|
156
|
-
chainId: chainId,
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
return this._signDirect(signerAddress, messages, fee, memo, signerData)
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
private async _signDirect(
|
|
164
|
-
signerAddress: string,
|
|
165
|
-
messages: readonly EncodeObject[],
|
|
166
|
-
fee: DidStdFee,
|
|
167
|
-
memo: string,
|
|
168
|
-
{ accountNumber, sequence, chainId }: SignerData,
|
|
169
|
-
): Promise<TxRaw> {
|
|
170
|
-
assert(isOfflineDirectSigner(this._signer))
|
|
171
|
-
const accountFromSigner = (await this._signer.getAccounts()).find(
|
|
172
|
-
(account) => account.address === signerAddress,
|
|
173
|
-
)
|
|
174
|
-
if (!accountFromSigner) {
|
|
175
|
-
throw new Error("Failed to retrieve account from signer")
|
|
176
|
-
}
|
|
177
|
-
const pubkey = encodePubkey(encodeSecp256k1Pubkey(accountFromSigner.pubkey))
|
|
178
|
-
const txBodyEncodeObject: TxBodyEncodeObject = {
|
|
179
|
-
typeUrl: "/cosmos.tx.v1beta1.TxBody",
|
|
180
|
-
value: {
|
|
181
|
-
messages: messages,
|
|
182
|
-
memo: memo,
|
|
183
|
-
},
|
|
184
|
-
}
|
|
185
|
-
const txBodyBytes = this.registry.encode(txBodyEncodeObject)
|
|
186
|
-
const gasLimit = Int53.fromString(fee.gas).toNumber()
|
|
187
|
-
const authInfoBytes = makeDidAuthInfoBytes([{ pubkey, sequence }], fee.amount, gasLimit, fee.payer!)
|
|
188
|
-
const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber)
|
|
189
|
-
const { signature, signed } = await this._signer.signDirect(signerAddress, signDoc)
|
|
190
|
-
return TxRaw.fromPartial({
|
|
191
|
-
bodyBytes: signed.bodyBytes,
|
|
192
|
-
authInfoBytes: signed.authInfoBytes,
|
|
193
|
-
signatures: [fromBase64(signature.signature)],
|
|
194
|
-
})
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
async checkDidSigners(verificationMethods: Partial<VerificationMethod>[] = []): Promise<TSignerAlgo> {
|
|
198
|
-
if (verificationMethods.length === 0) {
|
|
199
|
-
throw new Error('No verification methods provided')
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
verificationMethods.forEach((verificationMethod) => {
|
|
203
|
-
if (!(Object.values(VerificationMethods) as string[]).includes(verificationMethod.verificationMethodType ?? '')) {
|
|
204
|
-
throw new Error(`Unsupported verification method type: ${verificationMethod.verificationMethodType}`)
|
|
205
|
-
}
|
|
206
|
-
if (!this.didSigners[verificationMethod.verificationMethodType ?? '']) {
|
|
207
|
-
this.didSigners[verificationMethod.verificationMethodType ?? ''] = EdDSASigner
|
|
208
|
-
}
|
|
209
|
-
})
|
|
210
|
-
|
|
211
|
-
return this.didSigners
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
async getDidSigner(verificationMethodId: string, verificationMethods: Partial<VerificationMethod>[]): Promise<(secretKey: Uint8Array) => Signer> {
|
|
215
|
-
await this.checkDidSigners(verificationMethods)
|
|
216
|
-
const verificationMethod = verificationMethods.find(method => method.id === verificationMethodId)?.verificationMethodType
|
|
217
|
-
if (!verificationMethod) {
|
|
218
|
-
throw new Error(`Verification method for ${verificationMethodId} not found`)
|
|
219
|
-
}
|
|
220
|
-
return this.didSigners[verificationMethod]!
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
async signcreateDidDocTx(signInputs: ISignInputs[], payload: MsgCreateDidDocPayload): Promise<SignInfo[]> {
|
|
224
|
-
await this.checkDidSigners(payload?.verificationMethod)
|
|
225
|
-
|
|
226
|
-
const signBytes = MsgCreateDidDocPayload.encode(payload).finish()
|
|
227
|
-
const signInfos: SignInfo[] = await Promise.all(signInputs.map(async (signInput) => {
|
|
228
|
-
return {
|
|
229
|
-
verificationMethodId: signInput.verificationMethodId,
|
|
230
|
-
signature: base64ToBytes((await (await this.getDidSigner(signInput.verificationMethodId, payload.verificationMethod))(hexToBytes(signInput.privateKeyHex))(signBytes)) as string)
|
|
231
|
-
}
|
|
232
|
-
}))
|
|
233
|
-
|
|
234
|
-
return signInfos
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
async signupdateDidDocTx(signInputs: ISignInputs[], payload: MsgUpdateDidDocPayload): Promise<SignInfo[]> {
|
|
238
|
-
await this.checkDidSigners(payload?.verificationMethod)
|
|
239
|
-
|
|
240
|
-
const signBytes = MsgUpdateDidDocPayload.encode(payload).finish()
|
|
241
|
-
const signInfos: SignInfo[] = await Promise.all(signInputs.map(async (signInput) => {
|
|
242
|
-
return {
|
|
243
|
-
verificationMethodId: signInput.verificationMethodId,
|
|
244
|
-
signature: base64ToBytes((await (await this.getDidSigner(signInput.verificationMethodId, payload.verificationMethod))(hexToBytes(signInput.privateKeyHex))(signBytes)) as string)
|
|
245
|
-
}
|
|
246
|
-
}))
|
|
247
|
-
|
|
248
|
-
return signInfos
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
async signdeactivateDidDocTx(signInputs: ISignInputs[], payload: MsgDeactivateDidDocPayload, verificationMethod: VerificationMethod[]): Promise<SignInfo[]> {
|
|
252
|
-
await this.checkDidSigners(verificationMethod)
|
|
253
|
-
|
|
254
|
-
const signBytes = MsgDeactivateDidDocPayload.encode(payload).finish()
|
|
255
|
-
const signInfos: SignInfo[] = await Promise.all(signInputs.map(async (signInput) => {
|
|
256
|
-
return {
|
|
257
|
-
verificationMethodId: signInput.verificationMethodId,
|
|
258
|
-
signature: base64ToBytes((await (await this.getDidSigner(signInput.verificationMethodId, verificationMethod))(hexToBytes(signInput.privateKeyHex))(signBytes)) as string)
|
|
259
|
-
}
|
|
260
|
-
}))
|
|
261
|
-
|
|
262
|
-
return signInfos
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
static async signIdentityTx(signBytes: Uint8Array, signInputs: ISignInputs[]): Promise<SignInfo[]> {
|
|
266
|
-
let signInfos: SignInfo[] = [];
|
|
267
|
-
|
|
268
|
-
for (let signInput of signInputs) {
|
|
269
|
-
if (typeof(signInput.keyType) === undefined) {
|
|
270
|
-
throw new Error('Key type is not defined')
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
let signature: string;
|
|
274
|
-
|
|
275
|
-
switch (signInput.keyType) {
|
|
276
|
-
case 'Ed25519':
|
|
277
|
-
signature = (await EdDSASigner(hexToBytes(signInput.privateKeyHex))(signBytes)) as string;
|
|
278
|
-
break;
|
|
279
|
-
case 'Secp256k1':
|
|
280
|
-
signature = (await ES256KSigner(hexToBytes(signInput.privateKeyHex))(signBytes)) as string;
|
|
281
|
-
break;
|
|
282
|
-
case 'P256':
|
|
283
|
-
signature = (await ES256Signer(hexToBytes(signInput.privateKeyHex))(signBytes)) as string;
|
|
284
|
-
break;
|
|
285
|
-
default:
|
|
286
|
-
throw new Error(`Unsupported signature type: ${signInput.keyType}`);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
signInfos.push({
|
|
290
|
-
verificationMethodId: signInput.verificationMethodId,
|
|
291
|
-
signature: base64ToBytes(signature)
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
return signInfos
|
|
296
|
-
}
|
|
297
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Service as ProtobufService,
|
|
3
|
-
VerificationMethod as ProtobufVerificationMethod
|
|
4
|
-
} from "@cheqd/ts-proto/cheqd/did/v2/index.js"
|
|
5
|
-
import { CheqdSDK } from "./index.js"
|
|
6
|
-
import { Coin } from "@cosmjs/proto-signing"
|
|
7
|
-
import { Signer } from "did-jwt"
|
|
8
|
-
import { QueryClient } from "@cosmjs/stargate"
|
|
9
|
-
import { DIDResolutionResult } from "did-resolver"
|
|
10
|
-
import { DidExtension } from "./modules/did.js"
|
|
11
|
-
import { ResourceExtension } from './modules/resource.js';
|
|
12
|
-
export {
|
|
13
|
-
DIDDocument,
|
|
14
|
-
VerificationMethod,
|
|
15
|
-
Service,
|
|
16
|
-
ServiceEndpoint,
|
|
17
|
-
JsonWebKey
|
|
18
|
-
} from "did-resolver"
|
|
19
|
-
|
|
20
|
-
export enum CheqdNetwork {
|
|
21
|
-
Mainnet = 'mainnet',
|
|
22
|
-
Testnet = 'testnet',
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export type QueryExtensionSetup<T> = (base: QueryClient) => T
|
|
26
|
-
|
|
27
|
-
export type CheqdExtension<K extends string, V = any> = {
|
|
28
|
-
[P in K]: (Record<P, V> & Partial<Record<Exclude<K, P>, never>>) extends infer O
|
|
29
|
-
? { [Q in keyof O]: O[Q] }
|
|
30
|
-
: never
|
|
31
|
-
}[K]
|
|
32
|
-
|
|
33
|
-
export type CheqdExtensions = DidExtension | ResourceExtension
|
|
34
|
-
|
|
35
|
-
export interface IModuleMethod {
|
|
36
|
-
(...args: any[]): Promise<any>
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export interface IModuleMethodMap extends Record<string, IModuleMethod> {}
|
|
40
|
-
|
|
41
|
-
export interface IContext {
|
|
42
|
-
sdk: CheqdSDK
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export type DIDDocumentWithMetadata = Pick<DIDResolutionResult, 'didDocument' | 'didDocumentMetadata'>
|
|
46
|
-
|
|
47
|
-
export type SpecValidationResult = {
|
|
48
|
-
valid: boolean
|
|
49
|
-
error?: string
|
|
50
|
-
protobufVerificationMethod?: ProtobufVerificationMethod[]
|
|
51
|
-
protobufService?: ProtobufService[]
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export enum VerificationMethods {
|
|
55
|
-
Ed255192020 = 'Ed25519VerificationKey2020',
|
|
56
|
-
Ed255192018 = 'Ed25519VerificationKey2018',
|
|
57
|
-
JWK = 'JsonWebKey2020',
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export enum MethodSpecificIdAlgo {
|
|
61
|
-
Base58 = 'base58btc',
|
|
62
|
-
Uuid = 'uuid',
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export type TSignerAlgo = {
|
|
66
|
-
[key in VerificationMethods as string]?: (secretKey: Uint8Array) => Signer
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export interface ISignInputs {
|
|
70
|
-
verificationMethodId: string
|
|
71
|
-
keyType?: 'Ed25519' | 'Secp256k1' | 'P256'
|
|
72
|
-
privateKeyHex: string
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export interface IKeyPair {
|
|
76
|
-
publicKey: string
|
|
77
|
-
privateKey: string
|
|
78
|
-
algo?: MethodSpecificIdAlgo
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export interface IKeyValuePair {
|
|
82
|
-
key: string
|
|
83
|
-
value: any
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export type TVerificationKeyPrefix = string
|
|
87
|
-
|
|
88
|
-
export type TVerificationKey<K extends TVerificationKeyPrefix, N extends number> = `${K}-${N}`
|
|
89
|
-
|
|
90
|
-
export interface IVerificationKeys {
|
|
91
|
-
readonly methodSpecificId: TMethodSpecificId
|
|
92
|
-
readonly didUrl: `did:cheqd:${CheqdNetwork}:${IVerificationKeys['methodSpecificId']}` extends string ? string : never
|
|
93
|
-
readonly keyId: `${IVerificationKeys['didUrl']}#${TVerificationKey<TVerificationKeyPrefix, number>}`
|
|
94
|
-
readonly publicKey: string
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export type TMethodSpecificId = string
|
|
98
|
-
|
|
99
|
-
export interface DidStdFee {
|
|
100
|
-
readonly amount: readonly Coin[]
|
|
101
|
-
readonly gas: string
|
|
102
|
-
payer?: string
|
|
103
|
-
granter?: string
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export const ISignInputs = {
|
|
107
|
-
isSignInput(object: Object[]): object is ISignInputs[] {
|
|
108
|
-
return object.some((x)=> 'privateKeyHex' in x)
|
|
109
|
-
}
|
|
110
|
-
}
|