@dfns/lib-bitcoinjs 0.6.0-rc1 → 0.6.0

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 +6 -0
  2. package/index.js +40 -2
  3. package/package.json +2 -2
package/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import { DfnsApiClient } from '@dfns/sdk';
3
+ import { GetWalletResponse } from '@dfns/sdk/types/wallets';
3
4
  import { Network, Psbt, SignerAsync } from 'bitcoinjs-lib';
4
5
  export type DfnsWalletOptions = {
5
6
  walletId: string;
@@ -8,12 +9,17 @@ export type DfnsWalletOptions = {
8
9
  export declare class DfnsWallet implements SignerAsync {
9
10
  private readonly metadata;
10
11
  private readonly dfnsClient;
12
+ readonly scheme: GetWalletResponse['signingKey']['scheme'];
11
13
  readonly publicKey: Buffer;
14
+ readonly internalPubkey?: Buffer;
12
15
  private constructor();
13
16
  static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
14
17
  get address(): string | undefined;
15
18
  sign(hash: Buffer): Promise<Buffer>;
19
+ signSchnorr(hash: Buffer): Promise<Buffer>;
20
+ private _sign;
16
21
  SignPsbt(psbt: Psbt): Promise<Psbt>;
22
+ tweakSigner(taprootMerkleRoot: Buffer): SignerAsync;
17
23
  }
18
24
  export declare const litecoin: {
19
25
  mainnet: Network;
package/index.js CHANGED
@@ -3,7 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.litecoin = exports.DfnsWallet = void 0;
4
4
  const sdk_1 = require("@dfns/sdk");
5
5
  const bitcoinjs_lib_1 = require("bitcoinjs-lib");
6
- const compatibleNetworks = ['Bitcoin', 'BitcoinTestnet3', 'Litecoin', 'LitecoinTestnet'];
6
+ const bip341_1 = require("bitcoinjs-lib/src/payments/bip341");
7
+ const compatibleNetworks = ['Bitcoin', 'BitcoinSignet', 'BitcoinTestnet3', 'Litecoin', 'LitecoinTestnet'];
7
8
  const assertSigned = (res) => {
8
9
  if (res.status === 'Failed') {
9
10
  throw new sdk_1.DfnsError(-1, 'signing failed', res);
@@ -12,11 +13,24 @@ 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
  };
16
+ const xOnlyPubkey = (publicKey) => {
17
+ return publicKey.subarray(1, 33);
18
+ };
19
+ const getTweakedPublicKey = (publicKey, taprootMerkleRoot) => {
20
+ const xOnlyPublicKey = xOnlyPubkey(publicKey);
21
+ const tweakedKey = (0, bip341_1.tweakKey)(xOnlyPublicKey, taprootMerkleRoot);
22
+ return Buffer.concat([Uint8Array.from([tweakedKey.parity + 2]), tweakedKey.x]);
23
+ };
15
24
  class DfnsWallet {
16
25
  constructor(metadata, options) {
17
26
  this.metadata = metadata;
18
27
  this.dfnsClient = options.dfnsClient;
28
+ this.scheme = metadata.signingKey.scheme;
19
29
  this.publicKey = Buffer.from(metadata.signingKey.publicKey, 'hex');
30
+ if (this.scheme === 'Schnorr') {
31
+ this.internalPubkey = this.publicKey;
32
+ this.publicKey = getTweakedPublicKey(this.internalPubkey, undefined);
33
+ }
20
34
  }
21
35
  static async init(options) {
22
36
  const { walletId, dfnsClient } = options;
@@ -33,9 +47,15 @@ class DfnsWallet {
33
47
  return this.metadata.address;
34
48
  }
35
49
  async sign(hash) {
50
+ return this._sign(hash);
51
+ }
52
+ async signSchnorr(hash) {
53
+ return this._sign(hash, Buffer.alloc(0));
54
+ }
55
+ async _sign(hash, taprootMerkleRoot) {
36
56
  const res = await this.dfnsClient.wallets.generateSignature({
37
57
  walletId: this.metadata.id,
38
- body: { kind: 'Hash', hash: `0x${hash.toString('hex')}` },
58
+ body: { kind: 'Hash', hash: `0x${hash.toString('hex')}`, taprootMerkleRoot: taprootMerkleRoot && `0x${taprootMerkleRoot.toString('hex')}` },
39
59
  });
40
60
  assertSigned(res);
41
61
  if (!res.signature) {
@@ -57,6 +77,24 @@ class DfnsWallet {
57
77
  }
58
78
  return bitcoinjs_lib_1.Psbt.fromHex(res.signedData.replace(/^0x/, ''));
59
79
  }
80
+ tweakSigner(taprootMerkleRoot) {
81
+ if (this.scheme !== 'Schnorr') {
82
+ throw new sdk_1.DfnsError(400, 'Only available for Schnorr keys');
83
+ }
84
+ const publicKey = getTweakedPublicKey(this.internalPubkey, taprootMerkleRoot);
85
+ const wallet = this;
86
+ return new (class {
87
+ get publicKey() {
88
+ return publicKey;
89
+ }
90
+ async sign(_hash) {
91
+ throw new sdk_1.DfnsError(400, 'Expecting to only do schnorr signatures');
92
+ }
93
+ async signSchnorr(hash) {
94
+ return wallet._sign(hash, taprootMerkleRoot);
95
+ }
96
+ })();
97
+ }
60
98
  }
61
99
  exports.DfnsWallet = DfnsWallet;
62
100
  /// Litecoin networks
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@dfns/lib-bitcoinjs",
3
- "version": "0.6.0-rc1",
3
+ "version": "0.6.0",
4
4
  "peerDependencies": {
5
5
  "bitcoinjs-lib": "^6.0.0",
6
- "@dfns/sdk": "0.6.0-rc1"
6
+ "@dfns/sdk": "0.6.0"
7
7
  },
8
8
  "dependencies": {
9
9
  "buffer": "6.0.3",