@dfns/lib-vechain 0.7.11 → 0.7.13
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 +10 -9
- package/index.js +41 -18
- package/package.json +4 -7
package/index.d.ts
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
import { DfnsApiClient } from '@dfns/sdk';
|
|
2
|
-
import {
|
|
2
|
+
import { type AvailableVeChainProviders, type TransactionRequestInput, VeChainAbstractSigner } from '@vechain/sdk-network';
|
|
3
3
|
export type DfnsWalletOptions = {
|
|
4
4
|
walletId: string;
|
|
5
5
|
dfnsClient: DfnsApiClient;
|
|
6
|
-
/** @deprecated transaction signing is now synchronous. polling is deprecated. */
|
|
7
|
-
maxRetries?: number;
|
|
8
|
-
/** @deprecated transaction signing is now synchronous. polling is deprecated. */
|
|
9
|
-
retryInterval?: number;
|
|
10
6
|
};
|
|
11
|
-
export declare class DfnsWallet
|
|
12
|
-
private
|
|
7
|
+
export declare class DfnsWallet extends VeChainAbstractSigner {
|
|
8
|
+
private ad;
|
|
13
9
|
readonly address: string;
|
|
14
10
|
private readonly dfnsClient;
|
|
11
|
+
readonly walletId: string;
|
|
15
12
|
private constructor();
|
|
16
|
-
static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
|
|
13
|
+
static init(options: DfnsWalletOptions, provider?: AvailableVeChainProviders): Promise<DfnsWallet>;
|
|
17
14
|
private sign;
|
|
18
|
-
|
|
15
|
+
connect(provider: AvailableVeChainProviders): this;
|
|
16
|
+
getAddress(): Promise<string>;
|
|
17
|
+
signTransaction(transactionToSign: TransactionRequestInput): Promise<string>;
|
|
18
|
+
sendTransaction(transactionToSend: TransactionRequestInput): Promise<string>;
|
|
19
|
+
signPayload(payload: Uint8Array): Promise<string>;
|
|
19
20
|
}
|
package/index.js
CHANGED
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DfnsWallet = void 0;
|
|
4
4
|
const sdk_1 = require("@dfns/sdk");
|
|
5
|
+
const sdk_network_1 = require("@vechain/sdk-network");
|
|
6
|
+
const sdk_core_1 = require("@vechain/sdk-core");
|
|
5
7
|
const elliptic_1 = require("elliptic");
|
|
6
|
-
const thor_devkit_1 = require("thor-devkit");
|
|
7
8
|
const assertSigned = (res) => {
|
|
8
9
|
if (res.status === 'Failed') {
|
|
9
10
|
throw new sdk_1.DfnsError(-1, 'signing failed', res);
|
|
@@ -12,14 +13,15 @@ const assertSigned = (res) => {
|
|
|
12
13
|
throw new sdk_1.DfnsError(-1, 'cannot complete signing synchronously because this wallet action requires policy approval', res);
|
|
13
14
|
}
|
|
14
15
|
};
|
|
15
|
-
class DfnsWallet {
|
|
16
|
-
constructor(
|
|
17
|
-
|
|
18
|
-
this.
|
|
19
|
-
|
|
20
|
-
this.address =
|
|
21
|
-
|
|
22
|
-
|
|
16
|
+
class DfnsWallet extends sdk_network_1.VeChainAbstractSigner {
|
|
17
|
+
constructor(ad, client, walletId, provider) {
|
|
18
|
+
super(provider);
|
|
19
|
+
this.ad = ad;
|
|
20
|
+
this.dfnsClient = client;
|
|
21
|
+
this.address = ad;
|
|
22
|
+
this.walletId = walletId;
|
|
23
|
+
}
|
|
24
|
+
static async init(options, provider) {
|
|
23
25
|
const { walletId, dfnsClient } = options;
|
|
24
26
|
const res = await dfnsClient.wallets.getWallet({ walletId });
|
|
25
27
|
if (res.status !== 'Active') {
|
|
@@ -32,11 +34,13 @@ class DfnsWallet {
|
|
|
32
34
|
if (curve !== 'secp256k1') {
|
|
33
35
|
throw new sdk_1.DfnsError(-1, 'key curve is not secp256k1', { walletId, curve });
|
|
34
36
|
}
|
|
35
|
-
|
|
37
|
+
const publicKey = new elliptic_1.ec('secp256k1').keyFromPublic(res.signingKey.publicKey, 'hex');
|
|
38
|
+
const ad = sdk_core_1.Address.ofPublicKey(Buffer.from(publicKey.getPublic(false, 'array'))).toString();
|
|
39
|
+
return new DfnsWallet(ad, dfnsClient, walletId, provider);
|
|
36
40
|
}
|
|
37
41
|
async sign(msgHash) {
|
|
38
42
|
const res = await this.dfnsClient.wallets.generateSignature({
|
|
39
|
-
walletId: this.
|
|
43
|
+
walletId: this.walletId,
|
|
40
44
|
body: { kind: 'Hash', hash: msgHash.toString('hex') },
|
|
41
45
|
});
|
|
42
46
|
assertSigned(res);
|
|
@@ -49,13 +53,32 @@ class DfnsWallet {
|
|
|
49
53
|
Buffer.from([res.signature.recid]),
|
|
50
54
|
]);
|
|
51
55
|
}
|
|
52
|
-
|
|
53
|
-
return
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
connect(provider) {
|
|
57
|
+
return new DfnsWallet(this.address, this.dfnsClient, this.walletId, provider);
|
|
58
|
+
}
|
|
59
|
+
async getAddress() {
|
|
60
|
+
return Promise.resolve(this.address);
|
|
61
|
+
}
|
|
62
|
+
async signTransaction(transactionToSign) {
|
|
63
|
+
const transactionBody = await this.populateTransaction(transactionToSign);
|
|
64
|
+
// Get the transaction object
|
|
65
|
+
const transaction = sdk_core_1.Transaction.of(transactionBody);
|
|
66
|
+
const transactionHash = transaction.getTransactionHash().bytes;
|
|
67
|
+
const signature = await this.sign(Buffer.from(transactionHash));
|
|
68
|
+
return sdk_core_1.Hex.of(signature).toString();
|
|
69
|
+
}
|
|
70
|
+
async sendTransaction(transactionToSend) {
|
|
71
|
+
const signedTransaction = await this.signTransaction(transactionToSend);
|
|
72
|
+
return this.provider?.request({
|
|
73
|
+
method: sdk_network_1.RPC_METHODS.eth_sendRawTransaction,
|
|
74
|
+
params: [sdk_core_1.Hex.of(signedTransaction).toString()],
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
async signPayload(payload) {
|
|
78
|
+
const signature = await this.sign(Buffer.from(payload));
|
|
79
|
+
// SCP256K1 encodes the recovery flag in the last byte. EIP-191 adds 27 to it.
|
|
80
|
+
signature[signature.length - 1] += 27;
|
|
81
|
+
return sdk_core_1.Hex.of(signature).toString();
|
|
59
82
|
}
|
|
60
83
|
}
|
|
61
84
|
exports.DfnsWallet = DfnsWallet;
|
package/package.json
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dfns/lib-vechain",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.13",
|
|
4
4
|
"peerDependencies": {
|
|
5
|
-
"@vechain/
|
|
6
|
-
"
|
|
7
|
-
"@dfns/sdk": "0.7.11"
|
|
5
|
+
"@vechain/sdk-core": "^2.0.5",
|
|
6
|
+
"@vechain/sdk-network": "^2.0.5"
|
|
8
7
|
},
|
|
9
8
|
"dependencies": {
|
|
10
|
-
"
|
|
11
|
-
"cross-fetch": "3.1.6",
|
|
12
|
-
"elliptic": "6.6.1"
|
|
9
|
+
"elliptic": "^6.6.1"
|
|
13
10
|
},
|
|
14
11
|
"main": "./index.js",
|
|
15
12
|
"type": "commonjs"
|