@dfns/lib-vechain 0.1.2 → 0.1.3-rc.2

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 +8 -4
  2. package/index.js +35 -41
  3. package/package.json +2 -2
package/index.d.ts CHANGED
@@ -1,18 +1,22 @@
1
- /// <reference types="node" />
2
1
  import { DfnsApiClient } from '@dfns/sdk';
2
+ import { GetWalletResponse } from '@dfns/sdk/types/wallets';
3
3
  import { Wallet } from '@vechain/connex-driver';
4
4
  export type DfnsWalletOptions = {
5
5
  walletId: string;
6
6
  dfnsClient: DfnsApiClient;
7
+ /** @deprecated transaction signing is now synchronous. polling is deprecated. */
7
8
  maxRetries?: number;
9
+ /** @deprecated transaction signing is now synchronous. polling is deprecated. */
8
10
  retryInterval?: number;
9
11
  };
12
+ type WalletMetadata = GetWalletResponse;
10
13
  export declare class DfnsWallet implements Wallet {
14
+ private metadata;
11
15
  readonly address: string;
12
- private options;
13
- private constructor();
16
+ private readonly dfnsClient;
17
+ constructor(metadata: WalletMetadata, options: DfnsWalletOptions);
14
18
  static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
15
- waitForSignature(signatureId: string): Promise<Buffer>;
16
19
  private sign;
17
20
  get list(): Wallet.Key[];
18
21
  }
22
+ export {};
package/index.js CHANGED
@@ -1,59 +1,53 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DfnsWallet = void 0;
4
- const Wallets_1 = require("@dfns/sdk/codegen/datamodel/Wallets");
4
+ const sdk_1 = require("@dfns/sdk");
5
5
  const elliptic_1 = require("elliptic");
6
6
  const thor_devkit_1 = require("thor-devkit");
7
- const sleep = (interval = 0) => new Promise((resolve) => setTimeout(resolve, interval));
7
+ const assertSigned = (res) => {
8
+ if (res.status === 'Failed') {
9
+ throw new sdk_1.DfnsError(-1, 'signing failed', res);
10
+ }
11
+ else if (res.status !== 'Signed') {
12
+ throw new sdk_1.DfnsError(-1, 'cannot complete signing synchronously because this wallet action requires policy approval', res);
13
+ }
14
+ };
8
15
  class DfnsWallet {
9
- constructor(address, options) {
10
- this.address = address;
11
- this.options = {
12
- ...options,
13
- maxRetries: options.maxRetries ?? 3,
14
- retryInterval: options.retryInterval ?? 1000,
15
- };
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')));
16
21
  }
17
22
  static async init(options) {
18
23
  const { walletId, dfnsClient } = options;
19
24
  const res = await dfnsClient.wallets.getWallet({ walletId });
20
- if (res.network !== Wallets_1.BlockchainNetwork.KeyECDSA || !res.signingKey) {
21
- throw new Error(`wallet ${walletId} is incompatible`);
25
+ if (res.status !== 'Active') {
26
+ throw new sdk_1.DfnsError(-1, 'wallet not active', { walletId, status: res.status });
22
27
  }
23
- const publicKey = new elliptic_1.ec('secp256k1').keyFromPublic(res.signingKey.publicKey, 'hex');
24
- const addr = thor_devkit_1.address.fromPublicKey(Buffer.from(publicKey.getPublic(false, 'array')));
25
- return new DfnsWallet(addr, options);
26
- }
27
- async waitForSignature(signatureId) {
28
- const { walletId, dfnsClient, retryInterval } = this.options;
29
- let maxRetries = this.options.maxRetries;
30
- while (maxRetries > 0) {
31
- await sleep(retryInterval);
32
- const res = await dfnsClient.wallets.getSignature({ walletId, signatureId });
33
- if (res.status === Wallets_1.SignatureStatus.Signed) {
34
- if (!res.signature)
35
- break;
36
- return Buffer.concat([
37
- Buffer.from(res.signature.r.substring(2), 'hex'),
38
- Buffer.from(res.signature.s.substring(2), 'hex'),
39
- Buffer.from([res.signature.recid]),
40
- ]);
41
- }
42
- else if (res.status === Wallets_1.SignatureStatus.Failed) {
43
- break;
44
- }
45
- maxRetries -= 1;
28
+ const { scheme, curve } = res.signingKey;
29
+ if (scheme !== 'ECDSA') {
30
+ throw new sdk_1.DfnsError(-1, 'key scheme is not ECDSA', { walletId, scheme });
46
31
  }
47
- const waitedSeconds = Math.floor((this.options.maxRetries * retryInterval) / 1000);
48
- throw new Error(`Signature request ${signatureId} took more than ${waitedSeconds}s to complete, stopping polling. Please update options "maxRetries" or "retryIntervals" to wait longer.`);
32
+ if (curve !== 'secp256k1') {
33
+ throw new sdk_1.DfnsError(-1, 'key curve is not secp256k1', { walletId, curve });
34
+ }
35
+ return new DfnsWallet(res, options);
49
36
  }
50
37
  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') },
38
+ const res = await this.dfnsClient.wallets.generateSignature({
39
+ walletId: this.metadata.id,
40
+ body: { kind: 'Hash', hash: msgHash.toString('hex') },
55
41
  });
56
- return this.waitForSignature(res.id);
42
+ assertSigned(res);
43
+ if (!res.signature) {
44
+ throw new sdk_1.DfnsError(-1, 'signature missing', res);
45
+ }
46
+ return Buffer.concat([
47
+ Buffer.from(res.signature.r.replace(/^0x/, ''), 'hex'),
48
+ Buffer.from(res.signature.s.replace(/^0x/, ''), 'hex'),
49
+ Buffer.from([res.signature.recid]),
50
+ ]);
57
51
  }
58
52
  get list() {
59
53
  return [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dfns/lib-vechain",
3
- "version": "0.1.2",
3
+ "version": "0.1.3-rc.2",
4
4
  "dependencies": {
5
5
  "@vechain/connex-driver": "2.0.12",
6
6
  "buffer": "6.0.3",
@@ -10,7 +10,7 @@
10
10
  "uuid": "9.0.0"
11
11
  },
12
12
  "peerDependencies": {
13
- "@dfns/sdk": "0.1.2"
13
+ "@dfns/sdk": "0.1.3-rc.2"
14
14
  },
15
15
  "main": "./index.js",
16
16
  "type": "commonjs"