@dfns/lib-bitcoinjs 0.2.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.
Files changed (3) hide show
  1. package/index.d.ts +17 -0
  2. package/index.js +60 -0
  3. package/package.json +6 -0
package/index.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ /// <reference types="node" />
2
+ import { DfnsApiClient } from '@dfns/sdk';
3
+ import { Psbt, SignerAsync } from 'bitcoinjs-lib';
4
+ export type DfnsWalletOptions = {
5
+ walletId: string;
6
+ dfnsClient: DfnsApiClient;
7
+ };
8
+ export declare class DfnsWallet implements SignerAsync {
9
+ private readonly metadata;
10
+ private readonly dfnsClient;
11
+ readonly publicKey: Buffer;
12
+ private constructor();
13
+ static init(options: DfnsWalletOptions): Promise<DfnsWallet>;
14
+ get address(): string | undefined;
15
+ sign(hash: Buffer): Promise<Buffer>;
16
+ SignPsbt(psbt: Psbt): Promise<Psbt>;
17
+ }
package/index.js ADDED
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DfnsWallet = void 0;
4
+ const sdk_1 = require("@dfns/sdk");
5
+ const bitcoinjs_lib_1 = require("bitcoinjs-lib");
6
+ const assertSigned = (res) => {
7
+ if (res.status === 'Failed') {
8
+ throw new sdk_1.DfnsError(-1, 'signing failed', res);
9
+ }
10
+ else if (res.status !== 'Signed') {
11
+ throw new sdk_1.DfnsError(-1, 'cannot complete signing synchronously because this wallet action requires policy approval', res);
12
+ }
13
+ };
14
+ class DfnsWallet {
15
+ constructor(metadata, options) {
16
+ this.metadata = metadata;
17
+ this.dfnsClient = options.dfnsClient;
18
+ this.publicKey = Buffer.from(metadata.signingKey.publicKey, 'hex');
19
+ }
20
+ static async init(options) {
21
+ const { walletId, dfnsClient } = options;
22
+ const res = await dfnsClient.wallets.getWallet({ walletId });
23
+ if (res.status !== 'Active') {
24
+ throw new sdk_1.DfnsError(-1, 'wallet not active', { walletId, status: res.status });
25
+ }
26
+ if (res.network !== 'Bitcoin' && res.network !== 'BitcoinTestnet3') {
27
+ throw new sdk_1.DfnsError(-1, 'wallet is not bound to Bitcoin or BitcoinTestnet3', { walletId, network: res.network });
28
+ }
29
+ return new DfnsWallet(res, options);
30
+ }
31
+ get address() {
32
+ return this.metadata.address;
33
+ }
34
+ async sign(hash) {
35
+ const res = await this.dfnsClient.wallets.generateSignature({
36
+ walletId: this.metadata.id,
37
+ body: { kind: 'Hash', hash: `0x${hash.toString('hex')}` },
38
+ });
39
+ assertSigned(res);
40
+ if (!res.signature) {
41
+ throw new sdk_1.DfnsError(-1, 'signature missing', res);
42
+ }
43
+ return Buffer.concat([
44
+ Buffer.from(res.signature.r.replace(/^0x/, ''), 'hex'),
45
+ Buffer.from(res.signature.s.replace(/^0x/, ''), 'hex'),
46
+ ]);
47
+ }
48
+ async SignPsbt(psbt) {
49
+ const res = await this.dfnsClient.wallets.generateSignature({
50
+ walletId: this.metadata.id,
51
+ body: { kind: 'Psbt', psbt: `0x${psbt.toHex()}` },
52
+ });
53
+ assertSigned(res);
54
+ if (!res.signedData) {
55
+ throw new sdk_1.DfnsError(-1, 'signedData missing', res);
56
+ }
57
+ return bitcoinjs_lib_1.Psbt.fromHex(res.signedData.replace(/^0x/, ''));
58
+ }
59
+ }
60
+ exports.DfnsWallet = DfnsWallet;
package/package.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "name": "@dfns/lib-bitcoinjs",
3
+ "version": "0.2.1",
4
+ "main": "./index.js",
5
+ "type": "commonjs"
6
+ }