@cheqd/did-provider-cheqd 3.2.1 → 3.3.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/build/cjs/agent/ICheqd.d.ts +513 -14
- package/build/cjs/agent/ICheqd.d.ts.map +1 -1
- package/build/cjs/agent/ICheqd.js +1446 -5
- package/build/cjs/agent/ICheqd.js.map +1 -1
- package/build/cjs/did-manager/cheqd-did-provider.d.ts +26 -7
- package/build/cjs/did-manager/cheqd-did-provider.d.ts.map +1 -1
- package/build/cjs/did-manager/cheqd-did-provider.js +30 -8
- package/build/cjs/did-manager/cheqd-did-provider.js.map +1 -1
- package/build/cjs/did-manager/cheqd-did-resolver.d.ts +5 -0
- package/build/cjs/did-manager/cheqd-did-resolver.d.ts.map +1 -1
- package/build/cjs/did-manager/cheqd-did-resolver.js +7 -2
- package/build/cjs/did-manager/cheqd-did-resolver.js.map +1 -1
- package/build/cjs/dkg-threshold/lit-protocol.d.ts +92 -0
- package/build/cjs/dkg-threshold/lit-protocol.d.ts.map +1 -0
- package/build/cjs/dkg-threshold/lit-protocol.js +170 -0
- package/build/cjs/dkg-threshold/lit-protocol.js.map +1 -0
- package/build/cjs/global.d..d.ts +2 -0
- package/build/cjs/global.d..d.ts.map +1 -0
- package/build/cjs/global.d..js +2 -0
- package/build/cjs/global.d..js.map +1 -0
- package/build/cjs/index.js +1 -2
- package/build/cjs/index.js.map +1 -1
- package/build/cjs/utils/env.d.ts +7 -0
- package/build/cjs/utils/env.d.ts.map +1 -0
- package/build/cjs/utils/env.js +10 -0
- package/build/cjs/utils/env.js.map +1 -0
- package/build/cjs/utils/helpers.d.ts +12 -0
- package/build/cjs/utils/helpers.d.ts.map +1 -0
- package/build/cjs/utils/helpers.js +63 -0
- package/build/cjs/utils/helpers.js.map +1 -0
- package/build/esm/agent/ICheqd.d.ts +513 -14
- package/build/esm/agent/ICheqd.d.ts.map +1 -1
- package/build/esm/agent/ICheqd.js +1446 -5
- package/build/esm/agent/ICheqd.js.map +1 -1
- package/build/esm/did-manager/cheqd-did-provider.d.ts +26 -7
- package/build/esm/did-manager/cheqd-did-provider.d.ts.map +1 -1
- package/build/esm/did-manager/cheqd-did-provider.js +29 -7
- package/build/esm/did-manager/cheqd-did-provider.js.map +1 -1
- package/build/esm/did-manager/cheqd-did-resolver.d.ts +5 -0
- package/build/esm/did-manager/cheqd-did-resolver.d.ts.map +1 -1
- package/build/esm/did-manager/cheqd-did-resolver.js +6 -1
- package/build/esm/did-manager/cheqd-did-resolver.js.map +1 -1
- package/build/esm/dkg-threshold/lit-protocol.d.ts +92 -0
- package/build/esm/dkg-threshold/lit-protocol.d.ts.map +1 -0
- package/build/esm/dkg-threshold/lit-protocol.js +166 -0
- package/build/esm/dkg-threshold/lit-protocol.js.map +1 -0
- package/build/esm/global.d..d.ts +2 -0
- package/build/esm/global.d..d.ts.map +1 -0
- package/build/esm/global.d..js +2 -0
- package/build/esm/global.d..js.map +1 -0
- package/build/esm/index.js +1 -1
- package/build/esm/index.js.map +1 -1
- package/build/esm/utils/env.d.ts +7 -0
- package/build/esm/utils/env.d.ts.map +1 -0
- package/build/esm/utils/env.js +7 -0
- package/build/esm/utils/env.js.map +1 -0
- package/build/esm/utils/helpers.d.ts +12 -0
- package/build/esm/utils/helpers.d.ts.map +1 -0
- package/build/esm/utils/helpers.js +51 -0
- package/build/esm/utils/helpers.js.map +1 -0
- package/build/tsconfig.cjs.tsbuildinfo +1 -1
- package/build/tsconfig.esm.tsbuildinfo +1 -1
- package/build/tsconfig.types.tsbuildinfo +1 -1
- package/build/types/agent/ICheqd.d.ts +513 -14
- package/build/types/agent/ICheqd.d.ts.map +1 -1
- package/build/types/did-manager/cheqd-did-provider.d.ts +26 -7
- package/build/types/did-manager/cheqd-did-provider.d.ts.map +1 -1
- package/build/types/did-manager/cheqd-did-resolver.d.ts +5 -0
- package/build/types/did-manager/cheqd-did-resolver.d.ts.map +1 -1
- package/build/types/dkg-threshold/lit-protocol.d.ts +92 -0
- package/build/types/dkg-threshold/lit-protocol.d.ts.map +1 -0
- package/build/types/global.d..d.ts +2 -0
- package/build/types/global.d..d.ts.map +1 -0
- package/build/types/utils/env.d.ts +7 -0
- package/build/types/utils/env.d.ts.map +1 -0
- package/build/types/utils/helpers.d.ts +12 -0
- package/build/types/utils/helpers.d.ts.map +1 -0
- package/package.json +7 -1
- package/src/agent/ICheqd.ts +2209 -232
- package/src/did-manager/cheqd-did-provider.ts +55 -8
- package/src/did-manager/cheqd-did-resolver.ts +7 -1
- package/src/dkg-threshold/lit-protocol.ts +238 -0
- package/src/global.d..ts +1 -0
- package/src/utils/env.ts +6 -0
- package/src/utils/helpers.ts +66 -0
- package/tsconfig.json +1 -0
|
@@ -18,9 +18,11 @@ import {
|
|
|
18
18
|
} from '@cheqd/sdk'
|
|
19
19
|
import { MsgCreateResourcePayload } from '@cheqd/ts-proto/cheqd/resource/v2/index.js'
|
|
20
20
|
import {
|
|
21
|
+
Coin,
|
|
21
22
|
DirectSecp256k1HdWallet,
|
|
22
23
|
DirectSecp256k1Wallet
|
|
23
24
|
} from '@cosmjs/proto-signing'
|
|
25
|
+
import { GasPrice, DeliverTxResponse } from '@cosmjs/stargate'
|
|
24
26
|
import { assert } from '@cosmjs/utils'
|
|
25
27
|
import { DIDDocument } from 'did-resolver'
|
|
26
28
|
import {
|
|
@@ -49,17 +51,28 @@ import { v4 } from 'uuid'
|
|
|
49
51
|
|
|
50
52
|
const debug = Debug('veramo:did-provider-cheqd')
|
|
51
53
|
|
|
52
|
-
|
|
54
|
+
export const DefaultRPCUrls = {
|
|
55
|
+
[CheqdNetwork.Mainnet]: 'https://rpc.cheqd.net',
|
|
56
|
+
[CheqdNetwork.Testnet]: 'https://rpc.cheqd.network'
|
|
57
|
+
} as const
|
|
53
58
|
|
|
54
|
-
export
|
|
55
|
-
Mainnet
|
|
56
|
-
Testnet
|
|
57
|
-
}
|
|
59
|
+
export const DefaultRESTUrls = {
|
|
60
|
+
[CheqdNetwork.Mainnet]: 'https://api.cheqd.net',
|
|
61
|
+
[CheqdNetwork.Testnet]: 'https://api.cheqd.network'
|
|
62
|
+
} as const
|
|
63
|
+
|
|
64
|
+
export type IContext = IAgentContext<IKeyManager>
|
|
65
|
+
|
|
66
|
+
export type DefaultRPCUrl = typeof DefaultRPCUrls[CheqdNetwork.Mainnet] | typeof DefaultRPCUrls[CheqdNetwork.Testnet]
|
|
67
|
+
|
|
68
|
+
export type DefaultRESTUrl = typeof DefaultRESTUrls[CheqdNetwork.Mainnet] | typeof DefaultRESTUrls[CheqdNetwork.Testnet]
|
|
58
69
|
|
|
59
70
|
export type LinkedResource = Omit<MsgCreateResourcePayload, 'data'> & { data?: string }
|
|
60
71
|
|
|
61
72
|
export type ResourcePayload = Partial<MsgCreateResourcePayload>
|
|
62
73
|
|
|
74
|
+
export type StatusList2021ResourcePayload = ResourcePayload & { resourceType: 'StatusList2021' }
|
|
75
|
+
|
|
63
76
|
export type TImportableEd25519Key = Required<Pick<IKey, 'publicKeyHex' | 'privateKeyHex'>> & { kid: TImportableEd25519Key['publicKeyHex'], type: 'Ed25519' }
|
|
64
77
|
|
|
65
78
|
declare const TImportableEd25519Key: {
|
|
@@ -79,16 +92,18 @@ export class EnglishMnemonic extends _ {
|
|
|
79
92
|
export class CheqdDIDProvider extends AbstractIdentifierProvider {
|
|
80
93
|
private defaultKms: string
|
|
81
94
|
public readonly network: CheqdNetwork
|
|
82
|
-
|
|
95
|
+
public readonly rpcUrl: string
|
|
83
96
|
private readonly cosmosPayerWallet: Promise<DirectSecp256k1HdWallet | DirectSecp256k1Wallet>
|
|
84
97
|
private sdk?: CheqdSDK
|
|
85
98
|
private fee?: DidStdFee
|
|
86
99
|
|
|
100
|
+
static readonly defaultGasPrice = GasPrice.fromString('50ncheq')
|
|
101
|
+
|
|
87
102
|
constructor(options: { defaultKms: string, cosmosPayerSeed: string, networkType?: CheqdNetwork, rpcUrl?: string }) {
|
|
88
103
|
super()
|
|
89
104
|
this.defaultKms = options.defaultKms
|
|
90
105
|
this.network = options.networkType ? options.networkType : CheqdNetwork.Testnet
|
|
91
|
-
this.rpcUrl = options.rpcUrl ? options.rpcUrl :
|
|
106
|
+
this.rpcUrl = options.rpcUrl ? options.rpcUrl : DefaultRPCUrls[this.network]
|
|
92
107
|
|
|
93
108
|
if (!options?.cosmosPayerSeed || options.cosmosPayerSeed === '') {
|
|
94
109
|
this.cosmosPayerWallet = DirectSecp256k1HdWallet.generate()
|
|
@@ -105,7 +120,7 @@ export class CheqdDIDProvider extends AbstractIdentifierProvider {
|
|
|
105
120
|
)
|
|
106
121
|
}
|
|
107
122
|
|
|
108
|
-
private async getCheqdSDK(fee?: DidStdFee): Promise<CheqdSDK> {
|
|
123
|
+
private async getCheqdSDK(fee?: DidStdFee, gasPrice?: GasPrice): Promise<CheqdSDK> {
|
|
109
124
|
if (!this.sdk) {
|
|
110
125
|
const wallet = await this.cosmosPayerWallet.catch(() => {
|
|
111
126
|
throw new Error(`[did-provider-cheqd]: network: ${this.network} valid cosmosPayerSeed is required`)
|
|
@@ -114,6 +129,7 @@ export class CheqdDIDProvider extends AbstractIdentifierProvider {
|
|
|
114
129
|
modules: [DIDModule as unknown as AbstractCheqdSDKModule, ResourceModule as unknown as AbstractCheqdSDKModule],
|
|
115
130
|
rpcUrl: this.rpcUrl,
|
|
116
131
|
wallet: wallet,
|
|
132
|
+
gasPrice
|
|
117
133
|
}
|
|
118
134
|
|
|
119
135
|
this.sdk = await createCheqdSDK(sdkOptions)
|
|
@@ -415,6 +431,37 @@ export class CheqdDIDProvider extends AbstractIdentifierProvider {
|
|
|
415
431
|
throw Error('CheqdDIDProvider removeService is not supported.')
|
|
416
432
|
}
|
|
417
433
|
|
|
434
|
+
async transactSendTokens(args: { recipientAddress: string, amount: Coin, memoNonce: string, txBytes?: Uint8Array, timeoutMs?: number, pollIntervalMs?: number }): Promise<DeliverTxResponse> {
|
|
435
|
+
const sdk = await this.getCheqdSDK(undefined, CheqdDIDProvider.defaultGasPrice)
|
|
436
|
+
|
|
437
|
+
if (args?.txBytes) {
|
|
438
|
+
// broadcast txBytes
|
|
439
|
+
const tx = await sdk.signer.broadcastTx(args.txBytes, args?.timeoutMs, args?.pollIntervalMs)
|
|
440
|
+
|
|
441
|
+
// assert tx code is 0, in other words, tx succeeded
|
|
442
|
+
assert(tx.code === 0, `cosmos_transaction: Failed to send tokens. Reason: ${tx.rawLog}`)
|
|
443
|
+
|
|
444
|
+
// keep log
|
|
445
|
+
debug('Sent tokens', 'txBytes', toString(args.txBytes, 'hex'))
|
|
446
|
+
|
|
447
|
+
return tx
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
const tx = await sdk.signer.sendTokens(
|
|
451
|
+
(await this.cosmosPayerWallet).getAccounts()[0],
|
|
452
|
+
args.recipientAddress,
|
|
453
|
+
[args.amount],
|
|
454
|
+
'auto',
|
|
455
|
+
args.memoNonce,
|
|
456
|
+
)
|
|
457
|
+
|
|
458
|
+
assert(tx.code === 0, `cosmos_transaction: Failed to send tokens. Reason: ${tx.rawLog}`)
|
|
459
|
+
|
|
460
|
+
debug('Sent tokens', args.amount.amount, args.amount.denom, 'to', args.recipientAddress)
|
|
461
|
+
|
|
462
|
+
return tx
|
|
463
|
+
}
|
|
464
|
+
|
|
418
465
|
private async signPayload(context: IAgentContext<IKeyManager>, data: Uint8Array, verificationMethod: VerificationMethod[] = []): Promise<SignInfo[]> {
|
|
419
466
|
return Promise.all(
|
|
420
467
|
verificationMethod.map(async (method) => {
|
|
@@ -10,6 +10,12 @@ interface Options {
|
|
|
10
10
|
url: string
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Default resolver url.
|
|
15
|
+
* @public
|
|
16
|
+
*/
|
|
17
|
+
export const resolverUrl = 'https://resolver.cheqd.net/1.0/identifiers/'
|
|
18
|
+
|
|
13
19
|
/**
|
|
14
20
|
* Creates a CheqdDIDResolver instance that can be used with `did-resolver`.
|
|
15
21
|
* @public
|
|
@@ -25,7 +31,7 @@ export function getResolver(options?: Options): Record<string, DIDResolver> {
|
|
|
25
31
|
* @public
|
|
26
32
|
*/
|
|
27
33
|
export class CheqdDidResolver {
|
|
28
|
-
private resolverUrl =
|
|
34
|
+
private resolverUrl = resolverUrl
|
|
29
35
|
|
|
30
36
|
constructor(options?: Options) {
|
|
31
37
|
if (options?.url) this.resolverUrl = options.url
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import {
|
|
2
|
+
OfflineAminoSigner,
|
|
3
|
+
Secp256k1HdWallet,
|
|
4
|
+
StdSignDoc
|
|
5
|
+
} from '@cosmjs/amino'
|
|
6
|
+
import { toString } from 'uint8arrays/to-string'
|
|
7
|
+
import { sha256 } from '@cosmjs/crypto'
|
|
8
|
+
import {
|
|
9
|
+
LitNodeClientNodeJs,
|
|
10
|
+
LitNodeClient,
|
|
11
|
+
decryptString,
|
|
12
|
+
encryptString,
|
|
13
|
+
} from '@lit-protocol/lit-node-client'
|
|
14
|
+
import { JsonSaveEncryptionKeyRequest } from '@lit-protocol/types'
|
|
15
|
+
import { randomBytes } from '../utils/helpers.js'
|
|
16
|
+
import { isBrowser, isNode } from '../utils/env.js'
|
|
17
|
+
import { v4 } from 'uuid'
|
|
18
|
+
|
|
19
|
+
export type EncryptionResult = {
|
|
20
|
+
encryptedString: Blob
|
|
21
|
+
encryptedSymmetricKey: string
|
|
22
|
+
symmetricKey?: Uint8Array
|
|
23
|
+
}
|
|
24
|
+
export type AuthSignature = {
|
|
25
|
+
sig: string
|
|
26
|
+
derivedVia: 'cosmos.signArbitrary',
|
|
27
|
+
signedMessage: string
|
|
28
|
+
address: string
|
|
29
|
+
}
|
|
30
|
+
export type CosmosAuthSignature = {
|
|
31
|
+
cosmos: AuthSignature
|
|
32
|
+
}
|
|
33
|
+
export type CosmosReturnValueTest = {
|
|
34
|
+
key: string
|
|
35
|
+
comparator: string
|
|
36
|
+
value: string
|
|
37
|
+
}
|
|
38
|
+
export type CosmosAccessControlCondition = {
|
|
39
|
+
conditionType: 'cosmos'
|
|
40
|
+
path: string
|
|
41
|
+
chain: string
|
|
42
|
+
returnValueTest: CosmosReturnValueTest
|
|
43
|
+
}
|
|
44
|
+
export type SaveEncryptionKeyArgs = {
|
|
45
|
+
unifiedAccessControlConditions: CosmosAccessControlCondition[]
|
|
46
|
+
symmetricKey: CryptoKey
|
|
47
|
+
authSig: CosmosAuthSignature
|
|
48
|
+
chain: string
|
|
49
|
+
}
|
|
50
|
+
export type GetEncryptionKeyArgs = {
|
|
51
|
+
unifiedAccessControlConditions: CosmosAccessControlCondition[]
|
|
52
|
+
toDecrypt: string
|
|
53
|
+
authSig: CosmosAuthSignature
|
|
54
|
+
chain: string
|
|
55
|
+
}
|
|
56
|
+
export type EncryptStringMethodResult = { encryptedString: Blob, symmetricKey: Uint8Array }
|
|
57
|
+
export type DecryptStringMethodResult = string
|
|
58
|
+
export type EncryptStringMethod = (str: string) => Promise<EncryptStringMethodResult>
|
|
59
|
+
export type DecryptStringMethod = (encryptedString: Blob, symmetricKey: Uint8Array) => Promise<DecryptStringMethodResult>
|
|
60
|
+
export type LitNetwork = typeof LitNetworks[keyof typeof LitNetworks]
|
|
61
|
+
export type LitCompatibleCosmosChain = typeof LitCompatibleCosmosChains[keyof typeof LitCompatibleCosmosChains]
|
|
62
|
+
export type LitProtocolOptions = { cosmosAuthWallet: Secp256k1HdWallet, litNetwork?: LitNetwork, chain?: LitCompatibleCosmosChain }
|
|
63
|
+
export type TxNonceFormat = typeof TxNonceFormats[keyof typeof TxNonceFormats]
|
|
64
|
+
|
|
65
|
+
export const LitNetworks = { jalapeno: 'jalapeno', serrano: 'serrano', localhost: 'localhost', custom: 'custom' } as const
|
|
66
|
+
export const LitCompatibleCosmosChains = { cosmos: 'cosmos', cheqdMainnet: 'cheqdMainnet', cheqdTestnet: 'cheqdTestnet' } as const
|
|
67
|
+
export const TxNonceFormats = { entropy: 'entropy', uuid: 'uuid', timestamp: 'timestamp' } as const
|
|
68
|
+
|
|
69
|
+
export class LitProtocol {
|
|
70
|
+
client: LitNodeClientNodeJs | LitNodeClient
|
|
71
|
+
litNetwork: LitNetwork = LitNetworks.serrano
|
|
72
|
+
chain: LitCompatibleCosmosChain = LitCompatibleCosmosChains.cheqdTestnet
|
|
73
|
+
private readonly cosmosAuthWallet: Secp256k1HdWallet
|
|
74
|
+
|
|
75
|
+
private constructor(options: LitProtocolOptions) {
|
|
76
|
+
// validate options
|
|
77
|
+
if (options.litNetwork && !Object.values(LitNetworks).includes(options.litNetwork)) throw new Error(`[did-provider-cheqd]: lit-protocol: Invalid LitNetwork: ${options.litNetwork}`)
|
|
78
|
+
if (options.chain && !Object.values(LitCompatibleCosmosChains).includes(options.chain)) throw new Error(`[did-provider-cheqd]: lit-protocol: Invalid LitCompatibleCosmosChain: ${options.chain}`)
|
|
79
|
+
|
|
80
|
+
// set options
|
|
81
|
+
if (options.litNetwork) this.litNetwork = options.litNetwork
|
|
82
|
+
if (options.chain) this.chain = options.chain
|
|
83
|
+
this.cosmosAuthWallet = options.cosmosAuthWallet
|
|
84
|
+
|
|
85
|
+
// set client as per environment
|
|
86
|
+
this.client = function(that: LitProtocol) {
|
|
87
|
+
if (isNode) return new LitNodeClientNodeJs({ litNetwork: that.litNetwork })
|
|
88
|
+
if (isBrowser) return new LitNodeClient({ litNetwork: that.litNetwork })
|
|
89
|
+
throw new Error('[did-provider-cheqd]: lit-protocol: Unsupported runtime environment')
|
|
90
|
+
}(this)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async connect(): Promise<void> {
|
|
94
|
+
return await this.client.connect()
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async encrypt(secret: string, unifiedAccessControlConditions: NonNullable<JsonSaveEncryptionKeyRequest['unifiedAccessControlConditions']>, returnSymmetricKey = false): Promise<EncryptionResult> {
|
|
98
|
+
const authSig = await LitProtocol.generateAuthSignature(this.cosmosAuthWallet)
|
|
99
|
+
const { encryptedString, symmetricKey } = await encryptString(secret as string) as EncryptStringMethodResult
|
|
100
|
+
const encryptedSymmetricKey = await this.client.saveEncryptionKey(
|
|
101
|
+
{
|
|
102
|
+
unifiedAccessControlConditions,
|
|
103
|
+
symmetricKey,
|
|
104
|
+
authSig: authSig,
|
|
105
|
+
chain: this.chain
|
|
106
|
+
}
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
encryptedString,
|
|
111
|
+
encryptedSymmetricKey: toString(encryptedSymmetricKey, 'hex'),
|
|
112
|
+
symmetricKey: returnSymmetricKey ? symmetricKey : undefined
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async decrypt(encryptedString: Blob, encryptedSymmetricKey: string, unifiedAccessControlConditions: NonNullable<JsonSaveEncryptionKeyRequest['unifiedAccessControlConditions']>): Promise<string> {
|
|
117
|
+
const authSig = await LitProtocol.generateAuthSignature(this.cosmosAuthWallet)
|
|
118
|
+
const symmetricKey = await this.client.getEncryptionKey(
|
|
119
|
+
{
|
|
120
|
+
unifiedAccessControlConditions,
|
|
121
|
+
toDecrypt: encryptedSymmetricKey,
|
|
122
|
+
authSig: authSig,
|
|
123
|
+
chain: this.chain
|
|
124
|
+
}
|
|
125
|
+
)
|
|
126
|
+
return await decryptString(encryptedString, symmetricKey) as DecryptStringMethodResult
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
static async encryptDirect(secret: string): Promise<EncryptStringMethodResult> {
|
|
130
|
+
const { encryptedString, symmetricKey } = await encryptString(secret as string) as EncryptStringMethodResult
|
|
131
|
+
return {
|
|
132
|
+
encryptedString,
|
|
133
|
+
symmetricKey
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
static async decryptDirect(encryptedString: Blob, symmetricKey: Uint8Array): Promise<DecryptStringMethodResult> {
|
|
138
|
+
return await decryptString(encryptedString, symmetricKey) as DecryptStringMethodResult
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
static async create(options: Partial<LitProtocolOptions>): Promise<LitProtocol> {
|
|
142
|
+
// instantiate underlying cosmos auth wallet
|
|
143
|
+
if (!options.cosmosAuthWallet) options.cosmosAuthWallet = await Secp256k1HdWallet.generate(24, { prefix: await LitProtocol.getCosmosWalletPrefix(options?.chain) })
|
|
144
|
+
|
|
145
|
+
// validate top-level options chain
|
|
146
|
+
if (!options?.chain) options.chain = LitCompatibleCosmosChains.cheqdTestnet
|
|
147
|
+
|
|
148
|
+
// validate top-level options litNetwork
|
|
149
|
+
if (!options?.litNetwork) options.litNetwork = LitNetworks.serrano
|
|
150
|
+
|
|
151
|
+
const litProtocol = new LitProtocol(options as LitProtocolOptions)
|
|
152
|
+
await litProtocol.connect()
|
|
153
|
+
return litProtocol
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
static async getCosmosWalletPrefix(chain?: LitCompatibleCosmosChain): Promise<string> {
|
|
157
|
+
switch (chain) {
|
|
158
|
+
case LitCompatibleCosmosChains.cosmos:
|
|
159
|
+
return 'cosmos'
|
|
160
|
+
case LitCompatibleCosmosChains.cheqdMainnet:
|
|
161
|
+
return 'cheqd'
|
|
162
|
+
case LitCompatibleCosmosChains.cheqdTestnet:
|
|
163
|
+
return 'cheqd'
|
|
164
|
+
default:
|
|
165
|
+
return 'cheqd'
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
static async generateAuthSignature(wallet: OfflineAminoSigner): Promise<AuthSignature> {
|
|
170
|
+
const signerAddress = (await wallet.getAccounts())[0].address
|
|
171
|
+
const signData = await LitProtocol.generateSignData()
|
|
172
|
+
const signDoc = await LitProtocol.generateSignDoc(signerAddress, signData)
|
|
173
|
+
const result = await wallet.signAmino(signerAddress, signDoc)
|
|
174
|
+
return {
|
|
175
|
+
address: signerAddress,
|
|
176
|
+
derivedVia: 'cosmos.signArbitrary',
|
|
177
|
+
sig: result.signature.signature,
|
|
178
|
+
signedMessage: toString(sha256(new TextEncoder().encode(JSON.stringify(signDoc))), 'hex'), // <-- hex encoded sha256 hash of the json stringified signDoc
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
static async generateSignDoc(address: string, data: Uint8Array): Promise<StdSignDoc> {
|
|
183
|
+
return {
|
|
184
|
+
account_number: '0',
|
|
185
|
+
chain_id: '',
|
|
186
|
+
fee: {
|
|
187
|
+
amount: [],
|
|
188
|
+
gas: '0'
|
|
189
|
+
},
|
|
190
|
+
memo: '',
|
|
191
|
+
msgs: [
|
|
192
|
+
{
|
|
193
|
+
type: 'sign/MsgSignData',
|
|
194
|
+
value: {
|
|
195
|
+
data: toString(data, 'base64'),
|
|
196
|
+
signer: address,
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
],
|
|
200
|
+
sequence: '0',
|
|
201
|
+
} // <-- should be sorted alphabetically
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
static async generateSignData(): Promise<Uint8Array> {
|
|
205
|
+
return new TextEncoder().encode(`I am creating an account to use Lit Protocol at 2023-02-21T16:40:15.305Z`) // <-- lit nodes search for this string in the signData
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
static async generateTxNonce(format?: TxNonceFormat, entropyLength?: number): Promise<string> {
|
|
209
|
+
switch (format) {
|
|
210
|
+
case TxNonceFormats.entropy:
|
|
211
|
+
return toString(await randomBytes(entropyLength || 64), 'hex')
|
|
212
|
+
case TxNonceFormats.uuid:
|
|
213
|
+
return v4()
|
|
214
|
+
case TxNonceFormats.timestamp:
|
|
215
|
+
return new Date().toISOString()
|
|
216
|
+
default:
|
|
217
|
+
return v4()
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
static async generateCosmosAccessControlConditionBalance(returnValueTest: CosmosReturnValueTest, chain: LitCompatibleCosmosChain = LitCompatibleCosmosChains.cheqdTestnet, address = ':userAddress'): Promise<CosmosAccessControlCondition> {
|
|
222
|
+
return {
|
|
223
|
+
conditionType: 'cosmos',
|
|
224
|
+
path: `/cosmos/bank/v1beta1/balances/${address}`,
|
|
225
|
+
chain,
|
|
226
|
+
returnValueTest
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
static async generateCosmosAccessControlConditionTransactionMemo(returnValueTest: CosmosReturnValueTest, amount: string, sender: string, recipient = ':userAddress', chain: LitCompatibleCosmosChain = LitCompatibleCosmosChains.cheqdTestnet): Promise<CosmosAccessControlCondition> {
|
|
231
|
+
return {
|
|
232
|
+
conditionType: 'cosmos',
|
|
233
|
+
path: `/cosmos/tx/v1beta1/txs?events=transfer.recipient='${recipient}'&events=transfer.sender='${sender}'&events=transfer.amount='${amount}'&order_by=2`,
|
|
234
|
+
chain,
|
|
235
|
+
returnValueTest
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
package/src/global.d..ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare module '@digitalbazaar/vc-status-list'
|
package/src/utils/env.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined'
|
|
2
|
+
export const isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null
|
|
3
|
+
export const isWebWorker = typeof self === 'object' && self.constructor && self.constructor.name === 'DedicatedWorkerGlobalScope'
|
|
4
|
+
export const isJsDom = (typeof window !== 'undefined' && window.name === 'nodejs') || (typeof navigator !== 'undefined' && (navigator.userAgent.includes('jsdom') || navigator.userAgent.includes('Node.js')))
|
|
5
|
+
export const isReactNative = typeof navigator !== 'undefined' && navigator.product === 'ReactNative' // use wisely: limited as of react-native v0.67 + navigator.product has been deprecated
|
|
6
|
+
export const isElectron = (typeof window !== 'undefined' && typeof window.process === 'object' && (window.process as NodeJS.Process & { type: 'browser' | 'renderer' | 'worker' | 'utility' }).type === 'renderer') || (typeof navigator === 'object' && typeof navigator.userAgent === 'string' && navigator.userAgent.indexOf('Electron') >= 0) || (typeof process === 'object' && typeof process.versions === 'object' && !!process.versions.electron)
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { generate as generateSecret, type GenerateOptions } from 'generate-password'
|
|
2
|
+
import { toString } from 'uint8arrays/to-string'
|
|
3
|
+
|
|
4
|
+
export async function randomFromRange(min: number, max: number, notIn: number[]): Promise<number> {
|
|
5
|
+
const random = Math.floor(Math.random() * (max - min + 1) + min)
|
|
6
|
+
if (notIn.includes(random)) {
|
|
7
|
+
return await randomFromRange(min, max, notIn)
|
|
8
|
+
}
|
|
9
|
+
return random
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function randomUniqueSubsetInRange(min: number, max: number, count: number): Promise<Array<number>> {
|
|
13
|
+
const subset: number[] = []
|
|
14
|
+
for (let i = 0; i < count; i++) {
|
|
15
|
+
subset.push(await randomFromRange(min, max, subset))
|
|
16
|
+
}
|
|
17
|
+
return subset
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export async function randomBytes(length: number): Promise<Buffer> {
|
|
21
|
+
return Buffer.from(
|
|
22
|
+
Array.from(
|
|
23
|
+
{ length },
|
|
24
|
+
() => Math.floor(Math.random() * 256),
|
|
25
|
+
),
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function randomUniqueSecret(options?: GenerateOptions): Promise<string> {
|
|
30
|
+
return generateSecret({
|
|
31
|
+
length: 64,
|
|
32
|
+
numbers: true,
|
|
33
|
+
symbols: true,
|
|
34
|
+
uppercase: true,
|
|
35
|
+
...options,
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function initialiseIndexArray(length: number): Promise<Array<boolean>> {
|
|
40
|
+
return Array(length).fill(true)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function shuffleArray<T>(array: Array<T>): Promise<Array<T>> {
|
|
44
|
+
const shuffled = array.sort(() => Math.random() - 0.5)
|
|
45
|
+
return shuffled
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function toBlob(data: Uint8Array): Promise<Blob> {
|
|
49
|
+
return new Blob([data])
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function blobToHexString(blob: Blob): Promise<string> {
|
|
53
|
+
// buffer from blob
|
|
54
|
+
const buffer = await blob.arrayBuffer()
|
|
55
|
+
|
|
56
|
+
// convert buffer to uint8Array
|
|
57
|
+
const uint8Array = new Uint8Array(buffer)
|
|
58
|
+
|
|
59
|
+
return toString(uint8Array, 'hex')
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function unescapeUnicode(str: string): string {
|
|
63
|
+
return str.replace(/\\u([a-fA-F0-9]{4})/g, (m, cc) => {
|
|
64
|
+
return String.fromCharCode(parseInt(cc, 16))
|
|
65
|
+
})
|
|
66
|
+
}
|