@dfns/lib-vechain 0.1.0-beta.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/index.d.ts +18 -0
- package/index.js +67 -0
- package/package.json +17 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { DfnsApiClient } from '@dfns/sdk';
|
|
3
|
+
import { Wallet } from '@vechain/connex-driver';
|
|
4
|
+
export type DfnsWalletOptions = {
|
|
5
|
+
walletId: string;
|
|
6
|
+
dfnsClient: DfnsApiClient;
|
|
7
|
+
maxRetries?: number;
|
|
8
|
+
retryInterval?: number;
|
|
9
|
+
};
|
|
10
|
+
export declare class DfnsWallet implements Wallet {
|
|
11
|
+
readonly address: string;
|
|
12
|
+
private options;
|
|
13
|
+
private constructor();
|
|
14
|
+
static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
|
|
15
|
+
waitForSignature(signatureId: string): Promise<Buffer>;
|
|
16
|
+
private sign;
|
|
17
|
+
get list(): Wallet.Key[];
|
|
18
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DfnsWallet = void 0;
|
|
4
|
+
const Wallets_1 = require("@dfns/sdk/codegen/datamodel/Wallets");
|
|
5
|
+
const elliptic_1 = require("elliptic");
|
|
6
|
+
const thor_devkit_1 = require("thor-devkit");
|
|
7
|
+
const sleep = (interval = 0) => new Promise((resolve) => setTimeout(resolve, interval));
|
|
8
|
+
class DfnsWallet {
|
|
9
|
+
constructor(address, options) {
|
|
10
|
+
var _a, _b;
|
|
11
|
+
this.address = address;
|
|
12
|
+
this.options = {
|
|
13
|
+
...options,
|
|
14
|
+
maxRetries: (_a = options.maxRetries) !== null && _a !== void 0 ? _a : 3,
|
|
15
|
+
retryInterval: (_b = options.retryInterval) !== null && _b !== void 0 ? _b : 1000,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
static async init(options) {
|
|
19
|
+
const { walletId, dfnsClient } = options;
|
|
20
|
+
const res = await dfnsClient.wallets.getWallet({ walletId });
|
|
21
|
+
if (res.network !== Wallets_1.BlockchainNetwork.KeyECDSA || !res.signingKey) {
|
|
22
|
+
throw new Error(`wallet ${walletId} is incompatible`);
|
|
23
|
+
}
|
|
24
|
+
const publicKey = new elliptic_1.ec('secp256k1').keyFromPublic(res.signingKey.publicKey, 'hex');
|
|
25
|
+
const addr = thor_devkit_1.address.fromPublicKey(Buffer.from(publicKey.getPublic(false, 'array')));
|
|
26
|
+
return new DfnsWallet(addr, options);
|
|
27
|
+
}
|
|
28
|
+
async waitForSignature(signatureId) {
|
|
29
|
+
const { walletId, dfnsClient, retryInterval } = this.options;
|
|
30
|
+
let maxRetries = this.options.maxRetries;
|
|
31
|
+
while (maxRetries > 0) {
|
|
32
|
+
await sleep(retryInterval);
|
|
33
|
+
const res = await dfnsClient.wallets.getSignature({ walletId, signatureId });
|
|
34
|
+
if (res.status === Wallets_1.SignatureStatus.Signed) {
|
|
35
|
+
if (!res.signature)
|
|
36
|
+
break;
|
|
37
|
+
return Buffer.concat([
|
|
38
|
+
Buffer.from(res.signature.r.substring(2), 'hex'),
|
|
39
|
+
Buffer.from(res.signature.s.substring(2), 'hex'),
|
|
40
|
+
Buffer.from([res.signature.recid]),
|
|
41
|
+
]);
|
|
42
|
+
}
|
|
43
|
+
else if (res.status === Wallets_1.SignatureStatus.Failed) {
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
maxRetries -= 1;
|
|
47
|
+
}
|
|
48
|
+
throw new Error(`signature ${signatureId} not available`);
|
|
49
|
+
}
|
|
50
|
+
async sign(msgHash) {
|
|
51
|
+
const { walletId, dfnsClient } = this.options;
|
|
52
|
+
const res = await dfnsClient.wallets.generateSignature({
|
|
53
|
+
walletId,
|
|
54
|
+
body: { kind: Wallets_1.SignatureKind.Hash, hash: msgHash.toString('hex') },
|
|
55
|
+
});
|
|
56
|
+
return this.waitForSignature(res.id);
|
|
57
|
+
}
|
|
58
|
+
get list() {
|
|
59
|
+
return [
|
|
60
|
+
{
|
|
61
|
+
address: this.address,
|
|
62
|
+
sign: (msgHash) => this.sign(msgHash),
|
|
63
|
+
},
|
|
64
|
+
];
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.DfnsWallet = DfnsWallet;
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dfns/lib-vechain",
|
|
3
|
+
"version": "0.1.0-beta.1",
|
|
4
|
+
"dependencies": {
|
|
5
|
+
"@vechain/connex-driver": "2.0.12",
|
|
6
|
+
"buffer": "6.0.3",
|
|
7
|
+
"cross-fetch": "3.1.6",
|
|
8
|
+
"elliptic": "6.5.4",
|
|
9
|
+
"thor-devkit": "2.0.8",
|
|
10
|
+
"uuid": "9.0.0"
|
|
11
|
+
},
|
|
12
|
+
"peerDependencies": {
|
|
13
|
+
"@dfns/sdk": "0.1.0-beta.1"
|
|
14
|
+
},
|
|
15
|
+
"main": "./index.js",
|
|
16
|
+
"types": "./index.d.ts"
|
|
17
|
+
}
|