@dfns/lib-tron 0.1.2-rc.5 → 0.1.3-rc.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.
Files changed (3) hide show
  1. package/index.d.ts +2 -5
  2. package/index.js +49 -77
  3. package/package.json +4 -5
package/index.d.ts CHANGED
@@ -3,16 +3,13 @@ import { SignedTransaction, Transaction } from '@tronweb3/tronwallet-abstract-ad
3
3
  export type DfnsWalletOptions = {
4
4
  walletId: string;
5
5
  dfnsClient: DfnsApiClient;
6
- maxRetries?: number;
7
- retryInterval?: number;
8
6
  };
9
7
  export declare class DfnsWallet {
10
- private options;
11
- private tronAddress;
8
+ private metadata;
9
+ private readonly dfnsClient;
12
10
  private constructor();
13
11
  static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
14
12
  get address(): string;
15
- waitForSignature(signatureId: string): Promise<string>;
16
13
  signTransaction(transaction: Transaction): Promise<SignedTransaction>;
17
14
  signMessage(message: string): Promise<string>;
18
15
  }
package/index.js CHANGED
@@ -1,100 +1,72 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  Object.defineProperty(exports, "__esModule", { value: true });
26
3
  exports.DfnsWallet = void 0;
27
- const Wallets_1 = require("@dfns/sdk/codegen/datamodel/Wallets");
28
- const elliptic = __importStar(require("elliptic"));
4
+ const sdk_1 = require("@dfns/sdk");
29
5
  const TronWeb = require('tronweb');
30
- const sleep = (interval = 0) => new Promise((resolve) => setTimeout(resolve, interval));
6
+ const bytes = TronWeb.utils.bytes;
7
+ const ethersUtils = TronWeb.utils.ethersUtils;
8
+ const txUtils = TronWeb.utils.transaction;
9
+ const bufferToHex = (buffer) => {
10
+ return `0x${bytes.byteArray2hexStr(buffer).toLowerCase()}`;
11
+ };
12
+ const assertSigned = (res) => {
13
+ if (res.status === 'Failed') {
14
+ throw new sdk_1.DfnsError(-1, 'signing failed', res);
15
+ }
16
+ else if (res.status !== 'Signed') {
17
+ throw new sdk_1.DfnsError(-1, 'cannot complete signing synchronously because this wallet action requires policy approval', res);
18
+ }
19
+ };
31
20
  class DfnsWallet {
32
- constructor(options, tronAddress) {
33
- this.options = {
34
- ...options,
35
- maxRetries: options.maxRetries ?? 3,
36
- retryInterval: options.retryInterval ?? 1000,
37
- };
38
- this.tronAddress = tronAddress;
21
+ constructor(metadata, options) {
22
+ this.metadata = metadata;
23
+ this.dfnsClient = options.dfnsClient;
39
24
  }
40
25
  static async init(options) {
41
26
  const { walletId, dfnsClient } = options;
42
27
  const res = await dfnsClient.wallets.getWallet({ walletId });
43
- if (!res.signingKey || res.signingKey.scheme !== Wallets_1.KeyScheme.ECDSA || res.signingKey.curve !== Wallets_1.KeyCurve.secp256k1) {
44
- throw new Error(`wallet ${walletId} has incompatible scheme (${res.signingKey?.scheme}) or curve (${res.signingKey?.curve})`);
28
+ if (res.status !== 'Active') {
29
+ throw new sdk_1.DfnsError(-1, 'wallet not active', { walletId, status: res.status });
30
+ }
31
+ if (res.network !== 'Tron' && res.network !== 'TronNile') {
32
+ throw new sdk_1.DfnsError(-1, 'wallet is not bound to Tron or TronNile', { walletId, network: res.network });
45
33
  }
46
- // Decompressed the key to derive the address
47
- const ec = new elliptic.ec('secp256k1');
48
- const keyAsArray = ec.keyFromPublic(res.signingKey.publicKey, 'hex').getPublic(false, 'array');
49
- const tronAddress = TronWeb.utils.crypto.getBase58CheckAddress(TronWeb.utils.crypto.computeAddress(keyAsArray));
50
- return new DfnsWallet(options, tronAddress);
34
+ return new DfnsWallet(res, options);
51
35
  }
52
36
  get address() {
53
- return this.tronAddress;
54
- }
55
- async waitForSignature(signatureId) {
56
- const { walletId, dfnsClient, retryInterval } = this.options;
57
- let maxRetries = this.options.maxRetries;
58
- while (maxRetries > 0) {
59
- await sleep(retryInterval);
60
- const res = await dfnsClient.wallets.getSignature({ walletId, signatureId });
61
- if (res.status === Wallets_1.SignatureStatus.Signed) {
62
- if (!res.signature)
63
- break;
64
- const r = res.signature.r.substring(2);
65
- const s = res.signature.s.substring(2);
66
- const v = (res.signature.recid ? 0x1c : 0x1b).toString(16);
67
- return r + s + v;
68
- }
69
- else if (res.status === Wallets_1.SignatureStatus.Failed) {
70
- console.log("signature failed: %s", res);
71
- break;
72
- }
73
- maxRetries -= 1;
74
- }
75
- throw new Error(`signature ${signatureId} not available`);
37
+ // Tron-bound wallets will have a tron address
38
+ return this.metadata.address;
76
39
  }
77
40
  async signTransaction(transaction) {
78
- const { walletId, dfnsClient } = this.options;
79
- const res = await dfnsClient.wallets.generateSignature({
80
- walletId,
81
- body: { kind: Wallets_1.SignatureKind.Hash, hash: transaction.txID },
41
+ const res = await this.dfnsClient.wallets.generateSignature({
42
+ walletId: this.metadata.id,
43
+ body: {
44
+ kind: 'Transaction',
45
+ transaction: bufferToHex(txUtils.txJsonToPb(transaction).serializeBinary()),
46
+ },
82
47
  });
83
- const signature = await this.waitForSignature(res.id);
84
- const signedTransaction = {
48
+ assertSigned(res);
49
+ if (!res.signature?.encoded) {
50
+ throw new sdk_1.DfnsError(-1, 'encoded signature missing', res);
51
+ }
52
+ return {
85
53
  ...transaction,
86
- signature: [signature],
54
+ signature: [res.signature.encoded.replace(/^0x/, '')],
87
55
  };
88
- return signedTransaction;
89
56
  }
90
57
  async signMessage(message) {
91
- const { walletId, dfnsClient } = this.options;
92
- let messageHash = TronWeb.utils.message.hashMessage(message);
93
- const res = await dfnsClient.wallets.generateSignature({
94
- walletId,
95
- body: { kind: Wallets_1.SignatureKind.Hash, hash: messageHash },
58
+ const res = await this.dfnsClient.wallets.generateSignature({
59
+ walletId: this.metadata.id,
60
+ body: {
61
+ kind: 'Message',
62
+ message: bufferToHex(ethersUtils.toUtf8Bytes(message)),
63
+ },
96
64
  });
97
- return this.waitForSignature(res.id);
65
+ assertSigned(res);
66
+ if (!res.signature?.encoded) {
67
+ throw new sdk_1.DfnsError(-1, 'encoded signature missing', res);
68
+ }
69
+ return res.signature.encoded.replace(/^0x/, '');
98
70
  }
99
71
  }
100
72
  exports.DfnsWallet = DfnsWallet;
package/package.json CHANGED
@@ -1,16 +1,15 @@
1
1
  {
2
2
  "name": "@dfns/lib-tron",
3
- "version": "0.1.2-rc.5",
3
+ "version": "0.1.3-rc.1",
4
4
  "dependencies": {
5
- "@tronweb3/tronwallet-abstract-adapter": "^1.1.5",
5
+ "@tronweb3/tronwallet-abstract-adapter": "1.1.6",
6
6
  "buffer": "6.0.3",
7
7
  "cross-fetch": "3.1.6",
8
- "elliptic": "6.5.4",
9
- "tronweb": "^5.3.0",
8
+ "tronweb": "5.3.1",
10
9
  "uuid": "9.0.0"
11
10
  },
12
11
  "peerDependencies": {
13
- "@dfns/sdk": "0.1.2-rc.5"
12
+ "@dfns/sdk": "0.1.3-rc.1"
14
13
  },
15
14
  "main": "./index.js",
16
15
  "type": "commonjs"