@teleportdao/bitcoin 1.4.8 → 1.4.9

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.
@@ -1 +1 @@
1
- {"version":3,"file":"sign-transaction.d.ts","sourceRoot":"","sources":["../../src/sign/sign-transaction.ts"],"names":[],"mappings":";AAAA,OAAO,EAAgB,OAAO,EAAU,MAAM,eAAe,CAAA;AAc7D,cAAM,0BAA0B;IAC9B,OAAO,EAAE,OAAO,CAAA;gBACJ,OAAO,EAAE,OAAO;IAoCtB,QAAQ,CACZ,YAAY,EAAE;QACZ,mBAAmB,EAAE,MAAM,CAAA;KAC5B,EACD,UAAU,EAAE,MAAM;IAsBpB,aAAa,CAAC,WAAW,GAAE,MAAM,EAAO;CAezC;AAED,eAAe,0BAA0B,CAAA"}
1
+ {"version":3,"file":"sign-transaction.d.ts","sourceRoot":"","sources":["../../src/sign/sign-transaction.ts"],"names":[],"mappings":";AAAA,OAAO,EAAgB,OAAO,EAAE,MAAM,eAAe,CAAA;AA0CrD,cAAM,0BAA0B;IAC9B,OAAO,EAAE,OAAO,CAAA;gBACJ,OAAO,EAAE,OAAO;IAItB,QAAQ,CACZ,YAAY,EAAE;QACZ,mBAAmB,EAAE,MAAM,CAAA;KAC5B,EACD,UAAU,EAAE,MAAM;IA4BpB,aAAa,CAAC,WAAW,GAAE,MAAM,EAAO;CAczC;AAED,eAAe,0BAA0B,CAAA"}
@@ -1,4 +1,27 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
26
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
27
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -8,8 +31,36 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
31
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
32
  });
10
33
  };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
11
37
  Object.defineProperty(exports, "__esModule", { value: true });
12
38
  const bitcoinjs_lib_1 = require("bitcoinjs-lib");
39
+ const secp256k1_1 = __importDefault(require("@bitcoinerlab/secp256k1"));
40
+ const bitcoinEcPair = __importStar(require("bitcoinjs-ecpair"));
41
+ const ECPair = bitcoinEcPair.ECPair;
42
+ function tapTweakHash(pubKey, h) {
43
+ return bitcoinjs_lib_1.crypto.taggedHash("TapTweak", Buffer.concat(h ? [pubKey, h] : [pubKey]));
44
+ }
45
+ function tweakSigner(privateKey, opts = {}, network) {
46
+ let newPrv = privateKey;
47
+ let keyPair = ECPair.fromPrivateKey(privateKey, {
48
+ network: network,
49
+ compressed: true,
50
+ });
51
+ if (!keyPair.privateKey)
52
+ throw new Error("private key not exist");
53
+ if (keyPair.publicKey.toString("hex").startsWith("03")) {
54
+ newPrv = secp256k1_1.default.privateNegate(keyPair.privateKey);
55
+ }
56
+ const tweakedPrivateKey = secp256k1_1.default.privateAdd(newPrv, tapTweakHash(Buffer.from(keyPair.publicKey.toString("hex").slice(2), "hex"), opts === null || opts === void 0 ? void 0 : opts.tweakHash));
57
+ if (!tweakedPrivateKey) {
58
+ throw new Error("Invalid tweaked private key!");
59
+ }
60
+ return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {
61
+ network: network,
62
+ });
63
+ }
13
64
  class BitcoinLikeSignTransaction {
14
65
  constructor(network) {
15
66
  this.network = network;
@@ -17,7 +68,7 @@ class BitcoinLikeSignTransaction {
17
68
  signPsbt(unsignedPsbt, privateKey) {
18
69
  return __awaiter(this, void 0, void 0, function* () {
19
70
  const { network } = this;
20
- const keyPair = bitcoinjs_lib_1.ECPair.fromPrivateKey(privateKey, {
71
+ const keyPair = ECPair.fromPrivateKey(privateKey, {
21
72
  network,
22
73
  compressed: true,
23
74
  });
@@ -26,7 +77,14 @@ class BitcoinLikeSignTransaction {
26
77
  });
27
78
  let numberOfInputs = psbt.inputCount;
28
79
  for (let i = 0; i < numberOfInputs; i += 1) {
29
- yield psbt.signInputAsync(i, keyPair);
80
+ let type = psbt.getInputType(i);
81
+ if (type === "nonstandard") {
82
+ let a = tweakSigner(privateKey, undefined, this.network);
83
+ yield psbt.signInputAsync(i, a);
84
+ }
85
+ else {
86
+ yield psbt.signInputAsync(i, keyPair);
87
+ }
30
88
  }
31
89
  const partialSigendPsbt = psbt.toBase64();
32
90
  return partialSigendPsbt;
@@ -1 +1 @@
1
- {"version":3,"file":"sign-transaction.js","sourceRoot":"","sources":["../../src/sign/sign-transaction.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,iDAA6D;AAc7D,MAAM,0BAA0B;IAE9B,YAAY,OAAgB;QAC1B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;IACxB,CAAC;IAkCK,QAAQ,CACZ,YAEC,EACD,UAAkB;;YAElB,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,CAAA;YACxB,MAAM,OAAO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,UAAU,CAAC,UAAU,EAAE,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,CAAC,CACvD,CAAA;QACD,MAAM,IAAI,GACR,MAAM,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,oBAAI,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC,CAAA;QAE1F,IAAI,CAAC,iBAAiB,EAAE,CAAA;QAExB,IAAI,UAAU,GAAG,IAAI,CAAC,kBAAkB,EAAE,CAAA;QAG1C,OAAO,UAAU,CAAC,KAAK,EAAE,CAAA;IAC3B,CAAC;CACF;AAED,kBAAe,0BAA0B,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teleportdao/bitcoin",
3
- "version": "1.4.8",
3
+ "version": "1.4.9",
4
4
  "description": "teleswap bitcoin package",
5
5
  "main": "dist/index.js",
6
6
  "browser": "dist/bundle.js",
@@ -14,24 +14,28 @@
14
14
  "author": "",
15
15
  "license": "ISC",
16
16
  "dependencies": {
17
+ "@bitcoinerlab/secp256k1": "^1.0.5",
17
18
  "@teleportdao/configs": "^1.4.6",
18
- "@teleportdao/providers": "^1.4.8",
19
+ "@teleportdao/providers": "^1.4.9",
19
20
  "axios": "^0.27.2",
20
21
  "bignumber.js": "^9.1.1",
21
- "bip32": "^2.0.6",
22
- "bip39": "^3.0.4",
23
- "bitcoinjs-lib": "^5.2.0",
24
- "buffer": "^6.0.3",
22
+ "bip32": "^4.0.0",
23
+ "bip39": "^3.1.0",
24
+ "bitcoinjs-ecpair": "npm:bitcoinjs-lib@^5.2.0",
25
+ "bitcoinjs-lib": "^6.1.5",
25
26
  "coinselect": "^3.1.13",
26
- "ecpair": "^2.1.0",
27
+ "ecpair": "^2.0.1",
27
28
  "merkle-lib": "^2.0.10",
28
29
  "merkle-patricia-tree": "^2.3.2",
29
- "stream": "^0.0.2",
30
- "tiny-secp256k1": "^2.2.1",
30
+ "npm": "^10.2.3",
31
+ "uninstall": "^0.0.0",
31
32
  "varuint-bitcoin": "^1.1.2"
32
33
  },
33
34
  "publishConfig": {
34
35
  "access": "public"
35
36
  },
36
- "gitHead": "de0ae6fdd49608b800612b57316c0a6f3e7c829a"
37
+ "gitHead": "cb98230f4b9dddad7318f3830bc1890e20a3cda4",
38
+ "devDependencies": {
39
+ "@babel/preset-react": "^7.23.3"
40
+ }
37
41
  }
@@ -6,13 +6,17 @@ import type { ExtendedUtxo, SignerInfo, Target } from "./transaction-builder/tra
6
6
  import BitcoinSign from "./sign/sign-transaction"
7
7
 
8
8
  import * as bip39 from "bip39"
9
- import * as bip32 from "bip32"
10
9
  import { hdWalletPath } from "@teleportdao/configs"
11
10
  import { getPubKeyFromPrivateKeyHex } from "./bitcoin-utils"
12
11
  import networks from "./utils/networks"
13
12
  import { Network, Payment } from "bitcoinjs-lib"
14
13
  import { BitcoinInterface } from "./bitcoin-interface"
15
14
 
15
+ import BIP32Factory from "bip32"
16
+ import ecc from "@bitcoinerlab/secp256k1"
17
+
18
+ const bip32 = BIP32Factory(ecc)
19
+
16
20
  class BitcoinBase {
17
21
  network: Network
18
22
  hdWalletPath: {
@@ -244,5 +248,4 @@ class BitcoinBase {
244
248
  return txId
245
249
  }
246
250
  }
247
-
248
251
  export { BitcoinBase }
@@ -266,9 +266,11 @@ export class BitcoinInterface extends BitcoinInterfaceUtils {
266
266
  }
267
267
  let blockTxs = []
268
268
  for (let blockNumber = +startBlockNumber + 1; blockNumber <= endBlockNumber; blockNumber += 1) {
269
- const response = this.getBlockTransactions(addresses, blockNumber, inputTxIds)
269
+ console.log(blockNumber)
270
+
271
+ const response = await this.getBlockTransactions(addresses, blockNumber, inputTxIds)
270
272
  blockTxs.push(response)
271
- await sleep(200)
273
+ // await sleep(200)
272
274
  }
273
275
  blockTxs = await Promise.all(blockTxs)
274
276
  return blockTxs.flat(1)
@@ -1,15 +1,13 @@
1
- import * as bip39 from "bip39"
2
- import * as bip32 from "bip32"
3
1
  import networks from "./utils/networks"
4
2
 
5
- // import BIP32Factory from "bip32"
6
- // import ECPairFactory from "ecpair"
7
- // import * as ecc from "tiny-secp256k1"
3
+ import * as bip39 from "bip39"
4
+ import BIP32Factory from "bip32"
5
+ import * as ecc from "@bitcoinerlab/secp256k1"
8
6
  import * as bitcoin from "bitcoinjs-lib"
7
+ import * as bitcoinEcPair from "bitcoinjs-ecpair"
9
8
 
10
- // bitcoin.initEccLib(ecc)
11
- // const bip32 = BIP32Factory(ecc)
12
- const ECPair = bitcoin.ECPair
9
+ const bip32 = BIP32Factory(ecc)
10
+ const ECPair = bitcoinEcPair.ECPair
13
11
 
14
12
  let varUnit = require("varuint-bitcoin")
15
13
  const fastRoot = require("merkle-lib/fastRoot")
@@ -46,6 +44,44 @@ export function generateMnemonic() {
46
44
  return mnemonic
47
45
  }
48
46
 
47
+ export function reverseBytes(hexInput: string) {
48
+ return Buffer.from(hexInput, "hex").reverse().toString("hex")
49
+ }
50
+
51
+ export function getPublicKeyHexByXpubAndIndex(
52
+ xpub: string,
53
+ index = 0,
54
+ isChange = false,
55
+ network = networks.bitcoin,
56
+ ) {
57
+ const node = bip32.fromBase58(xpub, network)
58
+ return node
59
+ .derive(isChange ? 1 : 0)
60
+ .derive(index)
61
+ .publicKey.toString("hex")
62
+ }
63
+
64
+ export function getPubKeyFromPrivateKeyWIF(
65
+ privateKeyWIF: string,
66
+ network = bitcoin.networks.bitcoin,
67
+ ) {
68
+ let key = ECPair.fromWIF(privateKeyWIF, network)
69
+ return key.publicKey
70
+ }
71
+
72
+ export function getPubKeyFromPrivateKeyHex(
73
+ privateKeyHex: string,
74
+ network = bitcoin.networks.bitcoin,
75
+ ) {
76
+ let key = ECPair.fromPrivateKey(Buffer.from(privateKeyHex, "hex"), { network })
77
+ return key.publicKey
78
+ }
79
+
80
+ export function getPrivateKeyHexFromWIF(privateKeyWIF: string, network = bitcoin.networks.bitcoin) {
81
+ let key = ECPair.fromWIF(privateKeyWIF, network)
82
+ return key.privateKey!.toString("hex")
83
+ }
84
+
49
85
  export function parseRawTransaction(rawTransaction: string) {
50
86
  const size = {
51
87
  version: 4,
@@ -102,44 +138,6 @@ export function parseRawTransaction(rawTransaction: string) {
102
138
  return { version, flag, vin, vout, witness, locktime }
103
139
  }
104
140
 
105
- export function reverseBytes(hexInput: string) {
106
- return Buffer.from(hexInput, "hex").reverse().toString("hex")
107
- }
108
-
109
- export function getPublicKeyHexByXpubAndIndex(
110
- xpub: string,
111
- index = 0,
112
- isChange = false,
113
- network = networks.bitcoin,
114
- ) {
115
- const node = bip32.fromBase58(xpub, network)
116
- return node
117
- .derive(isChange ? 1 : 0)
118
- .derive(index)
119
- .publicKey.toString("hex")
120
- }
121
-
122
- export function getPubKeyFromPrivateKeyWIF(
123
- privateKeyWIF: string,
124
- network = bitcoin.networks.bitcoin,
125
- ) {
126
- let key = bitcoin.ECPair.fromWIF(privateKeyWIF, network)
127
- return key.publicKey
128
- }
129
-
130
- export function getPubKeyFromPrivateKeyHex(
131
- privateKeyHex: string,
132
- network = bitcoin.networks.bitcoin,
133
- ) {
134
- let key = bitcoin.ECPair.fromPrivateKey(Buffer.from(privateKeyHex, "hex"), { network })
135
- return key.publicKey
136
- }
137
-
138
- export function getPrivateKeyHexFromWIF(privateKeyWIF: string, network = bitcoin.networks.bitcoin) {
139
- let key = bitcoin.ECPair.fromWIF(privateKeyWIF, network)
140
- return key.privateKey!.toString("hex")
141
- }
142
-
143
141
  export function calculateMerkleProof(
144
142
  blockTransactions: string[],
145
143
  txId: string,
@@ -202,6 +200,7 @@ export function parseBlockHeader(headerHex: string) {
202
200
  }
203
201
  }
204
202
 
203
+ // used in block parser functions ( newt 2 functions )
205
204
  export function convertBitcoinScriptToAddress(script: Buffer, network = bitcoin.networks.bitcoin) {
206
205
  try {
207
206
  return bitcoin.address.fromOutputScript(script, network)
@@ -414,11 +413,11 @@ function toXOnlyPublicKey(pubKey: Buffer) {
414
413
 
415
414
  export function getAddressType(address: string, network = bitcoin.networks.bitcoin) {
416
415
  if (address.startsWith(`${network.bech32}1p`)) {
417
- // todo : check length - it could be p2wsh
416
+ // todo validate p2tr address
418
417
  return "p2tr"
419
418
  }
420
419
  if (address.startsWith(network.bech32)) {
421
- // todo : check length - it could be p2wsh
420
+ bitcoin.address.fromBech32(address)
422
421
  return "p2wpkh"
423
422
  }
424
423
  let base58Data = bitcoin.address.fromBase58Check(address)
@@ -462,12 +461,12 @@ export function createAddressObjectByScript(
462
461
  network,
463
462
  })
464
463
  break
465
- // case "p2tr":
466
- // addressObject = bitcoin.payments.p2tr({
467
- // output: script,
468
- // network,
469
- // })
470
- // break
464
+ case "p2tr":
465
+ addressObject = bitcoin.payments.p2tr({
466
+ output: script,
467
+ network,
468
+ })
469
+ break
471
470
  default:
472
471
  throw new Error("address type is incorrect")
473
472
  }
@@ -504,13 +503,13 @@ export function createAddressObjectByHash(
504
503
  network,
505
504
  })
506
505
  break
507
- // case "p2tr":
508
- // // here hash is the public key of tweaked private key
509
- // addressObject = bitcoin.payments.p2tr({
510
- // pubkey: hash,
511
- // network,
512
- // })
513
- // break
506
+ case "p2tr":
507
+ // here hash is the public key of tweaked private key (internal public key)
508
+ addressObject = bitcoin.payments.p2tr({
509
+ pubkey: hash,
510
+ network,
511
+ })
512
+ break
514
513
  default:
515
514
  throw new Error("address type is incorrect")
516
515
  }
@@ -544,12 +543,12 @@ export function createAddressObjectByPublicKey(
544
543
  })
545
544
  break
546
545
  case "p2tr":
547
- // // this public key is public key of main private key
548
- // addressObject = bitcoin.payments.p2tr({
549
- // internalPubkey: toXOnlyPublicKey(publicKey),
550
- // network,
551
- // })
552
- // break
546
+ // this public key is public key of main private key
547
+ addressObject = bitcoin.payments.p2tr({
548
+ internalPubkey: toXOnlyPublicKey(publicKey),
549
+ network,
550
+ })
551
+ break
553
552
  default:
554
553
  throw new Error("address type is incorrect")
555
554
  }
@@ -578,12 +577,12 @@ export function createAddressObjectByAddress(address: string, network = bitcoin.
578
577
  network,
579
578
  })
580
579
  break
581
- // case "p2tr":
582
- // addressObject = bitcoin.payments.p2tr({
583
- // address,
584
- // network,
585
- // })
586
- // break
580
+ case "p2tr":
581
+ addressObject = bitcoin.payments.p2tr({
582
+ address,
583
+ network,
584
+ })
585
+ break
587
586
  default:
588
587
  throw new Error("address type is incorrect")
589
588
  }
@@ -1,16 +1,44 @@
1
- import { Psbt, crypto, Network, ECPair } from "bitcoinjs-lib"
2
- // import ECPairFactory from "ecpair"
3
- // import * as ecc from "tiny-secp256k1"
4
- import { ExtendedUnsignedTransaction } from "../transaction-builder/transaction-builder"
5
- // import * as bitcoin from "bitcoinjs-lib"
1
+ import { Psbt, crypto, Network } from "bitcoinjs-lib"
2
+ // import BIP32Factory from "bip32"
3
+ import ecc from "@bitcoinerlab/secp256k1"
4
+ import * as bitcoinEcPair from "bitcoinjs-ecpair"
5
+ const ECPair = bitcoinEcPair.ECPair
6
6
 
7
- // bitcoin.initEccLib(ecc)
8
- // // const bip32 = BIP32Factory(ecc)
9
- // const ECPair = ECPairFactory(ecc)
7
+ function tapTweakHash(pubKey: Buffer, h?: Buffer) {
8
+ return crypto.taggedHash("TapTweak", Buffer.concat(h ? [pubKey, h] : [pubKey]))
9
+ }
10
+
11
+ function tweakSigner(
12
+ privateKey: Buffer,
13
+ opts = {} as {
14
+ [key: string]: Buffer
15
+ },
16
+ network: Network,
17
+ ) {
18
+ let newPrv = privateKey
19
+ let keyPair = ECPair.fromPrivateKey(privateKey, {
20
+ network: network,
21
+ compressed: true,
22
+ })
23
+
24
+ if (!keyPair.privateKey) throw new Error("private key not exist")
25
+
26
+ if (keyPair.publicKey.toString("hex").startsWith("03")) {
27
+ newPrv = ecc.privateNegate(keyPair.privateKey) as Buffer
28
+ }
29
+
30
+ const tweakedPrivateKey = ecc.privateAdd(
31
+ newPrv,
32
+ tapTweakHash(Buffer.from(keyPair.publicKey.toString("hex").slice(2), "hex"), opts?.tweakHash),
33
+ )
34
+ if (!tweakedPrivateKey) {
35
+ throw new Error("Invalid tweaked private key!")
36
+ }
10
37
 
11
- // function tapTweakHash(pubKey: Buffer, h?: Buffer) {
12
- // return crypto.taggedHash('TapTweak', Buffer.concat(h ? [pubKey, h] : [pubKey]))
13
- // }
38
+ return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {
39
+ network: network,
40
+ })
41
+ }
14
42
 
15
43
  class BitcoinLikeSignTransaction {
16
44
  network: Network
@@ -18,38 +46,6 @@ class BitcoinLikeSignTransaction {
18
46
  this.network = network
19
47
  }
20
48
 
21
- // tweakSigner(
22
- // privateKey: Buffer,
23
- // opts = {} as {
24
- // [key: string]: Buffer
25
- // },
26
- // ) {
27
- // let newPrv = privateKey
28
- // let keyPair = ECPair.fromPrivateKey(privateKey, {
29
- // network: this.network,
30
- // compressed: true,
31
- // })
32
-
33
- // if (!keyPair.privateKey) throw new Error("private key not exist")
34
-
35
- // if (keyPair.publicKey.toString("hex").startsWith("03")) {
36
- // newPrv = ecc.privateNegate(keyPair.privateKey) as Buffer
37
- // }
38
-
39
- // const tweakedPrivateKey = ecc.privateAdd(
40
- // newPrv,
41
- // tapTweakHash(Buffer.from(keyPair.publicKey.toString("hex").slice(2), "hex"), opts?.tweakHash),
42
- // )
43
- // if (!tweakedPrivateKey) {
44
- // throw new Error("Invalid tweaked private key!")
45
- // }
46
-
47
- // return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {
48
- // network: this.network,
49
- // })
50
- // }
51
-
52
- // todo : change to psbt
53
49
  async signPsbt(
54
50
  unsignedPsbt: {
55
51
  unsignedTransaction: string
@@ -68,7 +64,13 @@ class BitcoinLikeSignTransaction {
68
64
  let numberOfInputs = psbt.inputCount
69
65
 
70
66
  for (let i = 0; i < numberOfInputs; i += 1) {
71
- await psbt.signInputAsync(i, keyPair)
67
+ let type = psbt.getInputType(i)
68
+ if (type === "nonstandard") {
69
+ let a = tweakSigner(privateKey, undefined, this.network)
70
+ await psbt.signInputAsync(i, a)
71
+ } else {
72
+ await psbt.signInputAsync(i, keyPair)
73
+ }
72
74
  }
73
75
 
74
76
  // psbt.signAllInputs(keyPair)
@@ -82,13 +84,12 @@ class BitcoinLikeSignTransaction {
82
84
  )
83
85
  const psbt =
84
86
  finals.length === 1 ? finals[0] : new Psbt({ network: this.network }).combine(...finals)
87
+
85
88
  psbt.finalizeAllInputs()
86
89
 
87
- // psbt.finalizeAllInputs()
88
- // const tx = psbt.extractTransaction()
89
- // const rawTx = tx.toBuffer()
90
- // const hex = rawTx.toString("hex")
91
90
  let finalizeTx = psbt.extractTransaction()
91
+ // const rawTx = finalizeTx.toBuffer()
92
+ // const hex = rawTx.toString("hex")
92
93
  return finalizeTx.toHex()
93
94
  }
94
95
  }