@dfns/lib-cosmjs 0.5.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.d.ts +18 -0
  2. package/index.js +73 -0
  3. package/package.json +18 -0
package/index.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { AccountData, DirectSignResponse, OfflineDirectSigner } from '@cosmjs/proto-signing';
2
+ import { DfnsApiClient } from '@dfns/sdk';
3
+ import { SignDoc } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
4
+ export type DfnsWalletOptions = {
5
+ walletId: string;
6
+ dfnsClient: DfnsApiClient;
7
+ prefix: string;
8
+ };
9
+ export declare class DfnsWallet implements OfflineDirectSigner {
10
+ private metadata;
11
+ private readonly prefix;
12
+ private readonly pubkey;
13
+ private readonly dfnsClient;
14
+ private constructor();
15
+ static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
16
+ getAccounts(): Promise<AccountData[]>;
17
+ signDirect(_signerAddress: string, signDoc: SignDoc): Promise<DirectSignResponse>;
18
+ }
package/index.js ADDED
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DfnsWallet = void 0;
4
+ const crypto_1 = require("@cosmjs/crypto");
5
+ const encoding_1 = require("@cosmjs/encoding");
6
+ const proto_signing_1 = require("@cosmjs/proto-signing");
7
+ const sdk_1 = require("@dfns/sdk");
8
+ const stripHexPrefix = (hex) => {
9
+ return hex.replace(/^0x/, '');
10
+ };
11
+ const assertSigned = (res) => {
12
+ if (res.status === 'Failed') {
13
+ throw new sdk_1.DfnsError(-1, 'signing failed', res);
14
+ }
15
+ else if (res.status !== 'Signed') {
16
+ throw new sdk_1.DfnsError(-1, 'cannot complete signing synchronously because this wallet action requires policy approval', res);
17
+ }
18
+ };
19
+ class DfnsWallet {
20
+ constructor(metadata, options) {
21
+ this.metadata = metadata;
22
+ this.prefix = options.prefix;
23
+ this.pubkey = Buffer.from(metadata.signingKey.publicKey, 'hex');
24
+ this.dfnsClient = options.dfnsClient;
25
+ }
26
+ static async init(options) {
27
+ const { walletId, dfnsClient } = options;
28
+ const res = await dfnsClient.wallets.getWallet({ walletId });
29
+ if (res.status !== 'Active') {
30
+ throw new sdk_1.DfnsError(-1, 'wallet not active', { walletId, status: res.status });
31
+ }
32
+ if (res.network !== 'SeiPacific1' && res.network !== 'SeiAtlantic2') {
33
+ throw new sdk_1.DfnsError(-1, 'wallet is not bound to Sei', {
34
+ walletId,
35
+ network: res.network,
36
+ });
37
+ }
38
+ return new DfnsWallet(res, options);
39
+ }
40
+ async getAccounts() {
41
+ return [
42
+ {
43
+ address: (0, encoding_1.toBech32)(this.prefix, (0, crypto_1.ripemd160)((0, crypto_1.sha256)(this.pubkey))),
44
+ algo: 'secp256k1',
45
+ pubkey: this.pubkey,
46
+ },
47
+ ];
48
+ }
49
+ async signDirect(_signerAddress, signDoc) {
50
+ const res = await this.dfnsClient.wallets.generateSignature({
51
+ walletId: this.metadata.id,
52
+ body: {
53
+ kind: 'SignDocDirect',
54
+ signDoc: `0x${Buffer.from((0, proto_signing_1.makeSignBytes)(signDoc)).toString('hex')}`,
55
+ },
56
+ });
57
+ assertSigned(res);
58
+ if (!res.signature?.encoded) {
59
+ throw new sdk_1.DfnsError(-1, 'signature missing', res);
60
+ }
61
+ return {
62
+ signed: signDoc,
63
+ signature: {
64
+ pub_key: {
65
+ type: 'tendermint/PubKeySecp256k1',
66
+ value: this.pubkey.toString('base64'),
67
+ },
68
+ signature: Buffer.from(stripHexPrefix(res.signature.encoded), 'hex').toString('base64'),
69
+ },
70
+ };
71
+ }
72
+ }
73
+ exports.DfnsWallet = DfnsWallet;
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@dfns/lib-cosmjs",
3
+ "version": "0.5.8",
4
+ "peerDependencies": {
5
+ "@cosmjs/crypto": "^0.32.0",
6
+ "@cosmjs/encoding": "^0.32.0",
7
+ "@cosmjs/proto-signing": "^0.32.0",
8
+ "cosmjs-types": "^0.9.0",
9
+ "@dfns/sdk": "0.5.8"
10
+ },
11
+ "dependencies": {
12
+ "buffer": "6.0.3",
13
+ "cross-fetch": "3.1.6",
14
+ "uuid": "9.0.0"
15
+ },
16
+ "main": "./index.js",
17
+ "type": "commonjs"
18
+ }