@dfns/lib-tron 0.1.0-beta.6
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/index.d.ts +18 -0
- package/index.js +102 -0
- package/package.json +17 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { DfnsApiClient } from '@dfns/sdk';
|
|
2
|
+
import { SignedTransaction, Transaction } from '@tronweb3/tronwallet-abstract-adapter';
|
|
3
|
+
export type DfnsWalletOptions = {
|
|
4
|
+
walletId: string;
|
|
5
|
+
dfnsClient: DfnsApiClient;
|
|
6
|
+
maxRetries?: number;
|
|
7
|
+
retryInterval?: number;
|
|
8
|
+
};
|
|
9
|
+
export declare class DfnsWallet {
|
|
10
|
+
private options;
|
|
11
|
+
private tronAddress;
|
|
12
|
+
private constructor();
|
|
13
|
+
static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
|
|
14
|
+
get address(): string;
|
|
15
|
+
waitForSignature(signatureId: string): Promise<string>;
|
|
16
|
+
signTransaction(transaction: Transaction): Promise<SignedTransaction>;
|
|
17
|
+
signMessage(message: string): Promise<string>;
|
|
18
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.DfnsWallet = void 0;
|
|
27
|
+
const Wallets_1 = require("@dfns/sdk/codegen/datamodel/Wallets");
|
|
28
|
+
const elliptic = __importStar(require("elliptic"));
|
|
29
|
+
const TronWeb = require('tronweb');
|
|
30
|
+
const sleep = (interval = 0) => new Promise((resolve) => setTimeout(resolve, interval));
|
|
31
|
+
class DfnsWallet {
|
|
32
|
+
constructor(options, tronAddress) {
|
|
33
|
+
var _a, _b;
|
|
34
|
+
this.options = {
|
|
35
|
+
...options,
|
|
36
|
+
maxRetries: (_a = options.maxRetries) !== null && _a !== void 0 ? _a : 3,
|
|
37
|
+
retryInterval: (_b = options.retryInterval) !== null && _b !== void 0 ? _b : 1000,
|
|
38
|
+
};
|
|
39
|
+
this.tronAddress = tronAddress;
|
|
40
|
+
}
|
|
41
|
+
static async init(options) {
|
|
42
|
+
var _a, _b;
|
|
43
|
+
const { walletId, dfnsClient } = options;
|
|
44
|
+
const res = await dfnsClient.wallets.getWallet({ walletId });
|
|
45
|
+
if (!res.signingKey || res.signingKey.scheme !== Wallets_1.KeyScheme.ECDSA || res.signingKey.curve !== Wallets_1.KeyCurve.secp256k1) {
|
|
46
|
+
throw new Error(`wallet ${walletId} has incompatible scheme (${(_a = res.signingKey) === null || _a === void 0 ? void 0 : _a.scheme}) or curve (${(_b = res.signingKey) === null || _b === void 0 ? void 0 : _b.curve})`);
|
|
47
|
+
}
|
|
48
|
+
// Decompressed the key to derive the address
|
|
49
|
+
const ec = new elliptic.ec('secp256k1');
|
|
50
|
+
const keyAsArray = ec.keyFromPublic(res.signingKey.publicKey, 'hex').getPublic(false, 'array');
|
|
51
|
+
const tronAddress = TronWeb.utils.crypto.getBase58CheckAddress(TronWeb.utils.crypto.computeAddress(keyAsArray));
|
|
52
|
+
return new DfnsWallet(options, tronAddress);
|
|
53
|
+
}
|
|
54
|
+
get address() {
|
|
55
|
+
return this.tronAddress;
|
|
56
|
+
}
|
|
57
|
+
async waitForSignature(signatureId) {
|
|
58
|
+
const { walletId, dfnsClient, retryInterval } = this.options;
|
|
59
|
+
let maxRetries = this.options.maxRetries;
|
|
60
|
+
while (maxRetries > 0) {
|
|
61
|
+
await sleep(retryInterval);
|
|
62
|
+
const res = await dfnsClient.wallets.getSignature({ walletId, signatureId });
|
|
63
|
+
if (res.status === Wallets_1.SignatureStatus.Signed) {
|
|
64
|
+
if (!res.signature)
|
|
65
|
+
break;
|
|
66
|
+
const r = res.signature.r.substring(2);
|
|
67
|
+
const s = res.signature.s.substring(2);
|
|
68
|
+
const v = (res.signature.recid ? 0x1c : 0x1b).toString(16);
|
|
69
|
+
return r + s + v;
|
|
70
|
+
}
|
|
71
|
+
else if (res.status === Wallets_1.SignatureStatus.Failed) {
|
|
72
|
+
console.log("signature failed: %s", res);
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
maxRetries -= 1;
|
|
76
|
+
}
|
|
77
|
+
throw new Error(`signature ${signatureId} not available`);
|
|
78
|
+
}
|
|
79
|
+
async signTransaction(transaction) {
|
|
80
|
+
const { walletId, dfnsClient } = this.options;
|
|
81
|
+
const res = await dfnsClient.wallets.generateSignature({
|
|
82
|
+
walletId,
|
|
83
|
+
body: { kind: Wallets_1.SignatureKind.Hash, hash: transaction.txID },
|
|
84
|
+
});
|
|
85
|
+
const signature = await this.waitForSignature(res.id);
|
|
86
|
+
const signedTransaction = {
|
|
87
|
+
...transaction,
|
|
88
|
+
signature: [signature],
|
|
89
|
+
};
|
|
90
|
+
return signedTransaction;
|
|
91
|
+
}
|
|
92
|
+
async signMessage(message) {
|
|
93
|
+
const { walletId, dfnsClient } = this.options;
|
|
94
|
+
let messageHash = TronWeb.utils.message.hashMessage(message);
|
|
95
|
+
const res = await dfnsClient.wallets.generateSignature({
|
|
96
|
+
walletId,
|
|
97
|
+
body: { kind: Wallets_1.SignatureKind.Hash, hash: messageHash },
|
|
98
|
+
});
|
|
99
|
+
return this.waitForSignature(res.id);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
exports.DfnsWallet = DfnsWallet;
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dfns/lib-tron",
|
|
3
|
+
"version": "0.1.0-beta.6",
|
|
4
|
+
"dependencies": {
|
|
5
|
+
"@tronweb3/tronwallet-abstract-adapter": "^1.1.5",
|
|
6
|
+
"buffer": "6.0.3",
|
|
7
|
+
"cross-fetch": "3.1.6",
|
|
8
|
+
"elliptic": "6.5.4",
|
|
9
|
+
"tronweb": "^5.3.0",
|
|
10
|
+
"uuid": "9.0.0"
|
|
11
|
+
},
|
|
12
|
+
"peerDependencies": {
|
|
13
|
+
"@dfns/sdk": "0.1.0-beta.6"
|
|
14
|
+
},
|
|
15
|
+
"main": "./index.js",
|
|
16
|
+
"types": "./index.d.ts"
|
|
17
|
+
}
|