@dfns/lib-vechain 0.7.11-rc.1 → 0.7.12

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 +10 -9
  2. package/index.js +41 -18
  3. package/package.json +5 -8
package/index.d.ts CHANGED
@@ -1,19 +1,20 @@
1
1
  import { DfnsApiClient } from '@dfns/sdk';
2
- import { Wallet } from '@vechain/connex-driver';
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 implements Wallet {
12
- private metadata;
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
- get list(): Wallet.Key[];
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(metadata, options) {
17
- this.metadata = metadata;
18
- this.dfnsClient = options.dfnsClient;
19
- const publicKey = new elliptic_1.ec('secp256k1').keyFromPublic(metadata.signingKey.publicKey, 'hex');
20
- this.address = thor_devkit_1.address.fromPublicKey(Buffer.from(publicKey.getPublic(false, 'array')));
21
- }
22
- static async init(options) {
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
- return new DfnsWallet(res, options);
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.metadata.id,
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
- get list() {
53
- return [
54
- {
55
- address: this.address,
56
- sign: (msgHash) => this.sign(msgHash),
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,16 +1,13 @@
1
1
  {
2
2
  "name": "@dfns/lib-vechain",
3
- "version": "0.7.11-rc.1",
3
+ "version": "0.7.12",
4
4
  "peerDependencies": {
5
- "@vechain/connex-driver": "2.0.12",
6
- "thor-devkit": "2.0.8"
5
+ "@vechain/sdk-core": "^2.0.5",
6
+ "@vechain/sdk-network": "^2.0.5"
7
7
  },
8
8
  "dependencies": {
9
- "cross-fetch": "3.1.6",
10
- "elliptic": "6.5.4",
11
- "@dfns/sdk": "0.7.11-rc.1"
9
+ "elliptic": "^6.6.1"
12
10
  },
13
- "types": "./index.d.ts",
14
11
  "main": "./index.js",
15
12
  "type": "commonjs"
16
- }
13
+ }