@teleportdao/bitcoin 1.4.3 → 1.4.6

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 (69) hide show
  1. package/dist/bitcoin-base.d.ts +66 -53
  2. package/dist/bitcoin-base.d.ts.map +1 -1
  3. package/dist/bitcoin-base.js +47 -38
  4. package/dist/bitcoin-base.js.map +1 -1
  5. package/dist/bitcoin-interface-utils.d.ts +12 -10
  6. package/dist/bitcoin-interface-utils.d.ts.map +1 -1
  7. package/dist/bitcoin-interface-utils.js +16 -10
  8. package/dist/bitcoin-interface-utils.js.map +1 -1
  9. package/dist/bitcoin-interface.d.ts +206 -50
  10. package/dist/bitcoin-interface.d.ts.map +1 -1
  11. package/dist/bitcoin-interface.js +42 -27
  12. package/dist/bitcoin-interface.js.map +1 -1
  13. package/dist/bitcoin-utils.d.ts +111 -41
  14. package/dist/bitcoin-utils.d.ts.map +1 -1
  15. package/dist/bitcoin-utils.js +215 -156
  16. package/dist/bitcoin-utils.js.map +1 -1
  17. package/dist/bundle.js +13 -0
  18. package/dist/index.d.ts +5 -6
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +32 -12
  21. package/dist/index.js.map +1 -1
  22. package/dist/sign/sign-transaction.d.ts +9 -5
  23. package/dist/sign/sign-transaction.d.ts.map +1 -1
  24. package/dist/sign/sign-transaction.js +14 -11
  25. package/dist/sign/sign-transaction.js.map +1 -1
  26. package/dist/teleport-dao-payments.d.ts +68 -89
  27. package/dist/teleport-dao-payments.d.ts.map +1 -1
  28. package/dist/teleport-dao-payments.js +16 -4
  29. package/dist/teleport-dao-payments.js.map +1 -1
  30. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +30 -11
  31. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
  32. package/dist/transaction-builder/bitcoin-transaction-builder.js +37 -9
  33. package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
  34. package/dist/transaction-builder/transaction-builder.d.ts +198 -9
  35. package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
  36. package/dist/transaction-builder/transaction-builder.js +291 -38
  37. package/dist/transaction-builder/transaction-builder.js.map +1 -1
  38. package/dist/utils/networks.d.ts +5 -35
  39. package/dist/utils/networks.d.ts.map +1 -1
  40. package/dist/utils/networks.js +26 -2
  41. package/dist/utils/networks.js.map +1 -1
  42. package/dist/utils/tools.d.ts +15 -9
  43. package/dist/utils/tools.d.ts.map +1 -1
  44. package/dist/utils/tools.js +14 -11
  45. package/dist/utils/tools.js.map +1 -1
  46. package/package.json +8 -6
  47. package/src/{bitcoin-base.js → bitcoin-base.ts} +248 -219
  48. package/src/{bitcoin-interface-utils.js → bitcoin-interface-utils.ts} +59 -53
  49. package/src/{bitcoin-interface.js → bitcoin-interface.ts} +420 -350
  50. package/src/{bitcoin-utils.js → bitcoin-utils.ts} +608 -483
  51. package/src/helper/teleport-request-helper.js +179 -179
  52. package/src/index.ts +5 -0
  53. package/src/sign/sign-transaction.ts +96 -0
  54. package/src/{teleport-dao-payments.js → teleport-dao-payments.ts} +341 -280
  55. package/src/transaction-builder/bitcoin-transaction-builder.ts +61 -0
  56. package/src/transaction-builder/transaction-builder.ts +567 -0
  57. package/src/utils/{networks.js → networks.ts} +33 -31
  58. package/src/utils/{tools.js → tools.ts} +80 -72
  59. package/tsconfig.json +10 -9
  60. package/webpack.config.js +16 -0
  61. package/dist/transaction-builder/transaction-builder-common.d.ts +0 -57
  62. package/dist/transaction-builder/transaction-builder-common.d.ts.map +0 -1
  63. package/dist/transaction-builder/transaction-builder-common.js +0 -183
  64. package/dist/transaction-builder/transaction-builder-common.js.map +0 -1
  65. package/src/index.js +0 -15
  66. package/src/sign/sign-transaction.js +0 -36
  67. package/src/transaction-builder/bitcoin-transaction-builder.js +0 -37
  68. package/src/transaction-builder/transaction-builder-common.js +0 -236
  69. package/src/transaction-builder/transaction-builder.js +0 -159
@@ -1,13 +1,202 @@
1
- export = BaseBitcoinLikeTransaction;
2
- declare class BaseBitcoinLikeTransaction extends BaseBitcoinLikeTransactionBuilderCommon {
3
- convertBaseInputsToInputs(baseInputs?: any[]): Promise<any[]>;
4
- createUnsignedTransaction({ inputs, outputs, change, fee, feeRate, }: any): {
1
+ /// <reference types="node" />
2
+ import * as bitcoin from "bitcoinjs-lib";
3
+ export type Utxo = {
4
+ hash: string;
5
+ value: number;
6
+ index: number;
7
+ };
8
+ export type SignerInfo = {
9
+ address: string;
10
+ publicKey: string;
11
+ addressType: string;
12
+ derivationPath?: string;
13
+ masterFingerprint?: string;
14
+ includeHex?: boolean;
15
+ };
16
+ export type ExtendedUtxo = {
17
+ signerInfo: SignerInfo;
18
+ hash: string;
19
+ value: number;
20
+ index: number;
21
+ };
22
+ export type TargetAddress = {
23
+ address: string;
24
+ value: number;
25
+ };
26
+ export type TargetScript = {
27
+ script: Buffer;
28
+ value: number;
29
+ };
30
+ export type Target = TargetAddress | TargetScript;
31
+ export type ChangeTarget = TargetAddress & {
32
+ bip32Derivation?: {
33
+ path: string;
34
+ pubkey: Buffer;
35
+ masterFingerprint: Buffer;
36
+ }[];
37
+ };
38
+ export type BitcoinJSInputInfo = ExtendedUtxo & {
39
+ bip32Derivation?: {
40
+ path: string;
41
+ pubkey: Buffer;
42
+ masterFingerprint: Buffer;
43
+ }[];
44
+ nonWitnessUtxo?: Buffer;
45
+ witnessUtxo?: {
46
+ script: Buffer;
47
+ value: number;
48
+ };
49
+ redeemScript?: Buffer;
50
+ tapInternalKey?: Buffer;
51
+ };
52
+ export type ExtendedUnsignedTransaction = {
53
+ unsignedTransaction: string;
54
+ outputs: Target[];
55
+ inputs: {
56
+ hash: string;
57
+ value: number;
58
+ index: number;
59
+ signerInfo: SignerInfo;
60
+ }[];
61
+ fee: number;
62
+ change: TargetAddress | undefined;
63
+ };
64
+ declare class BaseBitcoinLikeTransaction {
65
+ testnet: boolean;
66
+ network: bitcoin.Network;
67
+ maximumNumberOfOutputsInTransaction: number;
68
+ feeMin: number;
69
+ dustLimit: number;
70
+ constructor({ network, testnet, feeMin, dustLimit, maximumNumberOfOutputsInTransaction, }: {
71
+ network: bitcoin.Network;
72
+ testnet: boolean;
73
+ feeMin?: number;
74
+ dustLimit?: number;
75
+ maximumNumberOfOutputsInTransaction?: number;
76
+ });
77
+ _getUtxo(userAddress: string): Promise<Utxo[]>;
78
+ _getTransactionHex(transactionId: string): Promise<string>;
79
+ createAddressObject(input: {
80
+ addressType: string;
81
+ publicKey: Buffer;
82
+ }): bitcoin.payments.Payment;
83
+ validateAddress(address: string): boolean;
84
+ getOpReturnTarget(dataHex: string): {
85
+ script: Buffer;
86
+ value: number;
87
+ };
88
+ getExtendedUtxo(signerInfo: SignerInfo): Promise<{
89
+ signerInfo: SignerInfo;
90
+ hash: string;
91
+ value: number;
92
+ index: number;
93
+ }[]>;
94
+ static helperHandleInputsAndOutputs({ targets, extendedUtxo, feeRate, changeObject, selectType, }: {
95
+ extendedUtxo: ExtendedUtxo[];
96
+ targets: Target[];
97
+ feeRate: number;
98
+ changeObject?: {
99
+ address: string;
100
+ publicKey?: string;
101
+ addressType?: string;
102
+ derivationPath?: string;
103
+ masterFingerprint?: string;
104
+ };
105
+ selectType?: "normal" | "accumulative" | "full";
106
+ }): {
107
+ inputs: ExtendedUtxo[];
108
+ fee: number;
109
+ outputs: Target[];
110
+ change: ChangeTarget | undefined;
111
+ };
112
+ filterAndConvertTxDataToStandardFormat({ extendedUtxo, targets, changeObject, feeRate, selectType, }: {
113
+ extendedUtxo: ExtendedUtxo[];
114
+ targets: Target[];
115
+ feeRate: number;
116
+ changeObject?: {
117
+ address: string;
118
+ publicKey?: string;
119
+ addressType?: string;
120
+ derivationPath?: string;
121
+ masterFingerprint?: string;
122
+ };
123
+ selectType?: "normal" | "accumulative" | "full";
124
+ }): Promise<{
125
+ inputs: (ExtendedUtxo & {
126
+ bip32Derivation?: {
127
+ path: string;
128
+ pubkey: Buffer;
129
+ masterFingerprint: Buffer;
130
+ }[] | undefined;
131
+ nonWitnessUtxo?: Buffer | undefined;
132
+ witnessUtxo?: {
133
+ script: Buffer;
134
+ value: number;
135
+ } | undefined;
136
+ redeemScript?: Buffer | undefined;
137
+ tapInternalKey?: Buffer | undefined;
138
+ } & {
139
+ signerInfo: SignerInfo;
140
+ })[];
141
+ outputs: Target[];
142
+ change: ChangeTarget | undefined;
143
+ fee: number;
144
+ feeRate: number;
145
+ }>;
146
+ convertExtendedUtxoToInputs(baseInputs?: ExtendedUtxo[]): Promise<(ExtendedUtxo & {
147
+ bip32Derivation?: {
148
+ path: string;
149
+ pubkey: Buffer;
150
+ masterFingerprint: Buffer;
151
+ }[] | undefined;
152
+ nonWitnessUtxo?: Buffer | undefined;
153
+ witnessUtxo?: {
154
+ script: Buffer;
155
+ value: number;
156
+ } | undefined;
157
+ redeemScript?: Buffer | undefined;
158
+ tapInternalKey?: Buffer | undefined;
159
+ } & {
160
+ signerInfo: SignerInfo;
161
+ })[]>;
162
+ createUnsignedTransaction({ inputs, outputs, change, fee, feeRate, }: {
163
+ inputs: BitcoinJSInputInfo[];
164
+ outputs: Target[];
165
+ change?: ChangeTarget;
166
+ fee: number;
167
+ feeRate: number;
168
+ }): {
5
169
  unsignedTransaction: string;
6
- outputs: any;
7
- inputs: any;
8
- fee: any;
9
- change: any;
170
+ outputs: Target[];
171
+ inputs: {
172
+ hash: string;
173
+ value: number;
174
+ index: number;
175
+ signerInfo: SignerInfo;
176
+ }[];
177
+ fee: number;
178
+ change: ChangeTarget | undefined;
10
179
  };
180
+ processUnsignedTransaction({ extendedUtxo, targets, changeAddress, fullAmount, feeRate, selfTransaction, selectType, }: {
181
+ extendedUtxo: ExtendedUtxo[];
182
+ targets: Target[];
183
+ feeRate: number;
184
+ changeAddress?: string | SignerInfo;
185
+ fullAmount?: boolean;
186
+ selfTransaction?: boolean;
187
+ selectType?: "normal" | "accumulative" | "full";
188
+ }): Promise<{
189
+ unsignedTransaction: string;
190
+ outputs: Target[];
191
+ inputs: {
192
+ hash: string;
193
+ value: number;
194
+ index: number;
195
+ signerInfo: SignerInfo;
196
+ }[];
197
+ fee: number;
198
+ change: ChangeTarget | undefined;
199
+ }>;
11
200
  }
12
- import BaseBitcoinLikeTransactionBuilderCommon = require("./transaction-builder-common");
201
+ export default BaseBitcoinLikeTransaction;
13
202
  //# sourceMappingURL=transaction-builder.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"transaction-builder.d.ts","sourceRoot":"","sources":["../../src/transaction-builder/transaction-builder.js"],"names":[],"mappings":";AAGA;IACE,8DA+CC;IAOD;;;;;;MAiGC;CACF"}
1
+ {"version":3,"file":"transaction-builder.d.ts","sourceRoot":"","sources":["../../src/transaction-builder/transaction-builder.ts"],"names":[],"mappings":";AACA,OAAO,KAAK,OAAO,MAAM,eAAe,CAAA;AA0BxC,MAAM,MAAM,IAAI,GAAG;IACjB,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,MAAM,CAAA;CACd,CAAA;AACD,MAAM,MAAM,UAAU,GAAG;IACvB,OAAO,EAAE,MAAM,CAAA;IACf,SAAS,EAAE,MAAM,CAAA;IACjB,WAAW,EAAE,MAAM,CAAA;IACnB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB,CAAA;AACD,MAAM,MAAM,YAAY,GAAG;IACzB,UAAU,EAAE,UAAU,CAAA;IACtB,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,MAAM,CAAA;CACd,CAAA;AAED,MAAM,MAAM,aAAa,GAAG;IAC1B,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,MAAM,CAAA;CACd,CAAA;AAED,MAAM,MAAM,YAAY,GAAG;IACzB,MAAM,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,MAAM,CAAA;CACd,CAAA;AACD,MAAM,MAAM,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C,EAAE;YACb,OAAO,EAAE,MAAM,CAAA;YACf,SAAS,CAAC,EAAE,MAAM,CAAA;YAClB,WAAW,CAAC,EAAE,MAAM,CAAA;YACpB,cAAc,CAAC,EAAE,MAAM,CAAA;YACvB,iBAAiB,CAAC,EAAE,MAAM,CAAA;SAC3B,CAAA;QACD,UAAU,CAAC,EAAE,QAAQ,GAAG,cAAc,GAAG,MAAM,CAAA;KAChD;;;;;;;;;;;;;;;;;;;;;;IA0BK,2BAA2B,CAAC,UAAU,GAAE,YAAY,EAAO;;;;;;;;;;;;;;oBAEjD,UAAU;;IA4F1B,yBAAyB,CAAC,EACxB,MAAM,EACN,OAAO,EACP,MAAM,EACN,GAAG,EACH,OAAO,GACR,EAAE;QACD,MAAM,EAAE,kBAAkB,EAAE,CAAA;QAC5B,OAAO,EAAE,MAAM,EAAE,CAAA;QACjB,MAAM,CAAC,EAAE,YAAY,CAAA;QACrB,GAAG,EAAE,MAAM,CAAA;QACX,OAAO,EAAE,MAAM,CAAA;KAChB;;;;;;;;;;;;IA2GK,0BAA0B,CAAC,EAC/B,YAAY,EACZ,OAAY,EACZ,aAAyB,EACzB,UAAkB,EAClB,OAAO,EACP,eAAuB,EACvB,UAAqB,GACtB,EAAE;QACD,YAAY,EAAE,YAAY,EAAE,CAAA;QAC5B,OAAO,EAAE,MAAM,EAAE,CAAA;QACjB,OAAO,EAAE,MAAM,CAAA;QAEf,aAAa,CAAC,EAAE,MAAM,GAAG,UAAU,CAAA;QACnC,UAAU,CAAC,EAAE,OAAO,CAAA;QACpB,eAAe,CAAC,EAAE,OAAO,CAAA;QACzB,UAAU,CAAC,EAAE,QAAQ,GAAG,cAAc,GAAG,MAAM,CAAA;KAChD;;;;;;;;;;;;CAyBF;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,10 +31,156 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
31
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
32
  });
10
33
  };
11
- const bitcoin = require("bitcoinjs-lib");
12
- const BaseBitcoinLikeTransactionBuilderCommon = require("./transaction-builder-common");
13
- class BaseBitcoinLikeTransaction extends BaseBitcoinLikeTransactionBuilderCommon {
14
- convertBaseInputsToInputs(baseInputs = []) {
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ const bitcoin = __importStar(require("bitcoinjs-lib"));
36
+ const bitcoin_utils_1 = require("../bitcoin-utils");
37
+ const coinselect = require("coinselect");
38
+ const coinselectSplit = require("coinselect/split");
39
+ const coinselectAccumulative = require("coinselect/accumulative");
40
+ const TX_EMPTY_SIZE = 4 + 1 + 1 + 4;
41
+ const TX_INPUT_BASE = 32 + 4 + 1 + 4;
42
+ const TX_INPUT_P2PKH = 107;
43
+ const TX_INPUT_P2SH_P2PKH = 50;
44
+ const TX_INPUT_P2WPKH = 47;
45
+ const TX_OUTPUT_BASE = 8 + 1;
46
+ const TX_OUTPUT_P2PKH = 25;
47
+ const componentBytes = {
48
+ bytePerInput: {
49
+ p2pkh: TX_INPUT_BASE + TX_INPUT_P2PKH,
50
+ p2wpkh: TX_INPUT_BASE + TX_INPUT_P2WPKH,
51
+ p2shp2wpkh: TX_INPUT_BASE + TX_INPUT_P2SH_P2PKH,
52
+ },
53
+ baseTxBytes: TX_EMPTY_SIZE,
54
+ bytePerOutput: TX_OUTPUT_BASE + TX_OUTPUT_P2PKH,
55
+ };
56
+ class BaseBitcoinLikeTransaction {
57
+ constructor({ network, testnet, feeMin = 0, dustLimit, maximumNumberOfOutputsInTransaction = 50, }) {
58
+ this.testnet = testnet;
59
+ this.network = network;
60
+ this.maximumNumberOfOutputsInTransaction = maximumNumberOfOutputsInTransaction;
61
+ this.feeMin = feeMin;
62
+ this.dustLimit = dustLimit || 1 * 2 * componentBytes.bytePerInput.p2pkh;
63
+ }
64
+ _getUtxo(userAddress) {
65
+ return __awaiter(this, void 0, void 0, function* () {
66
+ throw new Error("Do not call abstract method directly");
67
+ });
68
+ }
69
+ _getTransactionHex(transactionId) {
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ throw new Error("Do not call abstract method directly");
72
+ });
73
+ }
74
+ createAddressObject(input) {
75
+ return (0, bitcoin_utils_1.createAddressObjectByPublicKey)(input, this.network);
76
+ }
77
+ validateAddress(address) {
78
+ try {
79
+ (0, bitcoin_utils_1.getAddressType)(address);
80
+ return true;
81
+ }
82
+ catch (error) {
83
+ return false;
84
+ }
85
+ }
86
+ getOpReturnTarget(dataHex) {
87
+ if (!(dataHex.length > 0))
88
+ throw new Error("invalid data in hex");
89
+ const embed = bitcoin.payments.embed({
90
+ data: [Buffer.from(dataHex, "hex")],
91
+ network: this.network,
92
+ });
93
+ return {
94
+ script: embed.output,
95
+ value: 0,
96
+ };
97
+ }
98
+ getExtendedUtxo(signerInfo) {
99
+ return __awaiter(this, void 0, void 0, function* () {
100
+ let utxo = yield this._getUtxo(signerInfo.address);
101
+ const extendedUtxo = utxo.map((input) => (Object.assign(Object.assign({}, input), { signerInfo })));
102
+ if (!extendedUtxo || extendedUtxo.length === 0) {
103
+ throw new Error("no utxo found");
104
+ }
105
+ return extendedUtxo;
106
+ });
107
+ }
108
+ static helperHandleInputsAndOutputs({ targets, extendedUtxo, feeRate, changeObject, selectType = "normal", }) {
109
+ let selectResponse;
110
+ switch (selectType) {
111
+ case "normal":
112
+ selectResponse = coinselect(extendedUtxo, targets, Math.round(feeRate));
113
+ break;
114
+ case "accumulative":
115
+ selectResponse = coinselectAccumulative(extendedUtxo, targets, Math.round(feeRate));
116
+ break;
117
+ case "full":
118
+ if (!targets[0].address) {
119
+ throw new Error();
120
+ }
121
+ selectResponse = coinselectSplit(extendedUtxo, [{ address: targets[0].address }], Math.round(feeRate));
122
+ break;
123
+ default:
124
+ break;
125
+ }
126
+ let { inputs, outputs, fee, } = selectResponse;
127
+ if (!inputs || !outputs) {
128
+ throw new Error("not enough balance");
129
+ }
130
+ let changeIndex = outputs.findIndex((x) => !(x === null || x === void 0 ? void 0 : x.address) && !x.script && (x.value || 0) > 0);
131
+ let change;
132
+ if (changeIndex >= 0) {
133
+ if (!changeObject)
134
+ throw new Error("change not exist");
135
+ if (changeObject.derivationPath && changeObject.masterFingerprint && changeObject.publicKey) {
136
+ change = {
137
+ address: changeObject.address,
138
+ value: outputs[changeIndex].value,
139
+ bip32Derivation: [
140
+ {
141
+ path: changeObject.derivationPath,
142
+ pubkey: Buffer.from(changeObject.publicKey, "hex"),
143
+ masterFingerprint: Buffer.from(changeObject.masterFingerprint, "hex"),
144
+ },
145
+ ],
146
+ };
147
+ }
148
+ else {
149
+ change = {
150
+ address: changeObject.address,
151
+ value: outputs[changeIndex].value,
152
+ };
153
+ }
154
+ outputs.splice(changeIndex, 1);
155
+ }
156
+ return {
157
+ inputs,
158
+ fee,
159
+ outputs: outputs,
160
+ change,
161
+ };
162
+ }
163
+ filterAndConvertTxDataToStandardFormat({ extendedUtxo, targets, changeObject, feeRate, selectType, }) {
164
+ return __awaiter(this, void 0, void 0, function* () {
165
+ let { inputs: filteredInputs, outputs, change, fee, } = BaseBitcoinLikeTransaction.helperHandleInputsAndOutputs({
166
+ targets,
167
+ extendedUtxo,
168
+ feeRate,
169
+ changeObject,
170
+ selectType,
171
+ });
172
+ let inputs = yield this.convertExtendedUtxoToInputs(filteredInputs);
173
+ return {
174
+ inputs,
175
+ outputs,
176
+ change,
177
+ fee,
178
+ feeRate,
179
+ };
180
+ });
181
+ }
182
+ convertExtendedUtxoToInputs(baseInputs = []) {
183
+ var _a;
15
184
  return __awaiter(this, void 0, void 0, function* () {
16
185
  let inputs = baseInputs;
17
186
  let transactionId = null;
@@ -19,11 +188,10 @@ class BaseBitcoinLikeTransaction extends BaseBitcoinLikeTransactionBuilderCommon
19
188
  for (let i in inputs) {
20
189
  let { address, publicKey, derivationPath, masterFingerprint, addressType } = inputs[i].signerInfo;
21
190
  let addressObject = this.createAddressObject({
22
- address,
23
- publicKey: publicKey ? Buffer.from(publicKey, "hex") : null,
191
+ publicKey: Buffer.from(publicKey, "hex"),
24
192
  addressType,
25
193
  });
26
- if (derivationPath && masterFingerprint && publicKey && true) {
194
+ if (derivationPath && masterFingerprint && addressObject.pubkey) {
27
195
  inputs[i].bip32Derivation = [
28
196
  {
29
197
  path: derivationPath,
@@ -33,27 +201,74 @@ class BaseBitcoinLikeTransaction extends BaseBitcoinLikeTransactionBuilderCommon
33
201
  ];
34
202
  }
35
203
  if (addressType === "p2pkh") {
36
- if (transactionId === inputs[i].hash) {
204
+ if (transactionHex && transactionId === inputs[i].hash) {
37
205
  inputs[i].nonWitnessUtxo = Buffer.from(transactionHex, "hex");
38
206
  }
39
207
  else {
40
208
  transactionHex = yield this._getTransactionHex(inputs[i].hash);
41
- inputs[i].nonWitnessUtxo = Buffer.from(transactionHex, "hex");
42
209
  transactionId = inputs[i].hash;
210
+ inputs[i].nonWitnessUtxo = Buffer.from(transactionHex, "hex");
43
211
  }
44
212
  }
45
213
  else if (addressType === "p2wpkh") {
214
+ if (!addressObject.output)
215
+ throw new Error("invalid signer info");
46
216
  inputs[i].witnessUtxo = {
47
217
  script: addressObject.output,
48
218
  value: inputs[i].value,
49
219
  };
220
+ if (inputs[i].signerInfo.includeHex) {
221
+ if (transactionHex && transactionId === inputs[i].hash) {
222
+ inputs[i].nonWitnessUtxo = Buffer.from(transactionHex, "hex");
223
+ }
224
+ else {
225
+ transactionHex = yield this._getTransactionHex(inputs[i].hash);
226
+ transactionId = inputs[i].hash;
227
+ inputs[i].nonWitnessUtxo = Buffer.from(transactionHex, "hex");
228
+ }
229
+ }
50
230
  }
51
231
  else if (addressType === "p2sh-p2wpkh") {
232
+ if (!addressObject.output)
233
+ throw new Error("invalid signer info");
52
234
  inputs[i].witnessUtxo = {
53
235
  script: addressObject.output,
54
236
  value: inputs[i].value,
55
237
  };
238
+ if (!((_a = addressObject === null || addressObject === void 0 ? void 0 : addressObject.redeem) === null || _a === void 0 ? void 0 : _a.output))
239
+ throw new Error("invalid signer info for p2sh address");
56
240
  inputs[i].redeemScript = addressObject.redeem.output;
241
+ if (inputs[i].signerInfo.includeHex) {
242
+ if (transactionHex && transactionId === inputs[i].hash) {
243
+ inputs[i].nonWitnessUtxo = Buffer.from(transactionHex, "hex");
244
+ }
245
+ else {
246
+ transactionHex = yield this._getTransactionHex(inputs[i].hash);
247
+ transactionId = inputs[i].hash;
248
+ inputs[i].nonWitnessUtxo = Buffer.from(transactionHex, "hex");
249
+ }
250
+ }
251
+ }
252
+ else if (addressType === "p2tr") {
253
+ if (!addressObject.output)
254
+ throw new Error("invalid signer info");
255
+ inputs[i].witnessUtxo = {
256
+ script: addressObject.output,
257
+ value: inputs[i].value,
258
+ };
259
+ if (!addressObject.pubkey)
260
+ throw new Error("invalid signer info for p2tr address (pubkey)");
261
+ inputs[i].tapInternalKey = Buffer.from(publicKey, "hex");
262
+ if (inputs[i].signerInfo.includeHex) {
263
+ if (transactionHex && transactionId === inputs[i].hash) {
264
+ inputs[i].nonWitnessUtxo = Buffer.from(transactionHex, "hex");
265
+ }
266
+ else {
267
+ transactionHex = yield this._getTransactionHex(inputs[i].hash);
268
+ transactionId = inputs[i].hash;
269
+ inputs[i].nonWitnessUtxo = Buffer.from(transactionHex, "hex");
270
+ }
271
+ }
57
272
  }
58
273
  }
59
274
  return inputs;
@@ -62,7 +277,7 @@ class BaseBitcoinLikeTransaction extends BaseBitcoinLikeTransactionBuilderCommon
62
277
  createUnsignedTransaction({ inputs, outputs, change, fee, feeRate, }) {
63
278
  const { network } = this;
64
279
  const newPsbt = new bitcoin.Psbt({ network });
65
- newPsbt.setMaximumFeeRate = feeRate + feeRate / 100;
280
+ newPsbt.setMaximumFeeRate(+(feeRate + feeRate / 100).toFixed());
66
281
  for (const input of inputs) {
67
282
  let { addressType } = input.signerInfo;
68
283
  switch (addressType) {
@@ -72,9 +287,10 @@ class BaseBitcoinLikeTransaction extends BaseBitcoinLikeTransactionBuilderCommon
72
287
  index: Number(input.index),
73
288
  nonWitnessUtxo: input.nonWitnessUtxo,
74
289
  sequence: 0xffffffff - 1,
290
+ bip32Derivation: input.bip32Derivation,
75
291
  };
76
- if (input.bip32Derivation)
77
- i.bip32Derivation = input.bip32Derivation;
292
+ if (!i.bip32Derivation)
293
+ delete i.bip32Derivation;
78
294
  newPsbt.addInput(i);
79
295
  break;
80
296
  }
@@ -83,10 +299,14 @@ class BaseBitcoinLikeTransaction extends BaseBitcoinLikeTransactionBuilderCommon
83
299
  hash: input.hash,
84
300
  index: Number(input.index),
85
301
  witnessUtxo: input.witnessUtxo,
302
+ nonWitnessUtxo: input.nonWitnessUtxo,
86
303
  sequence: 0xffffffff - 1,
304
+ bip32Derivation: input.bip32Derivation,
87
305
  };
88
- if (input.bip32Derivation)
89
- i.bip32Derivation = input.bip32Derivation;
306
+ if (!i.bip32Derivation)
307
+ delete i.bip32Derivation;
308
+ if (!i.nonWitnessUtxo)
309
+ delete i.nonWitnessUtxo;
90
310
  newPsbt.addInput(i);
91
311
  break;
92
312
  }
@@ -95,11 +315,32 @@ class BaseBitcoinLikeTransaction extends BaseBitcoinLikeTransactionBuilderCommon
95
315
  hash: input.hash,
96
316
  index: Number(input.index),
97
317
  witnessUtxo: input.witnessUtxo,
318
+ nonWitnessUtxo: input.nonWitnessUtxo,
98
319
  redeemScript: input.redeemScript,
99
320
  sequence: 0xffffffff - 1,
321
+ bip32Derivation: input.bip32Derivation,
322
+ };
323
+ if (!i.bip32Derivation)
324
+ delete i.bip32Derivation;
325
+ if (!i.nonWitnessUtxo)
326
+ delete i.nonWitnessUtxo;
327
+ newPsbt.addInput(i);
328
+ break;
329
+ }
330
+ case "p2tr": {
331
+ let i = {
332
+ hash: input.hash,
333
+ index: Number(input.index),
334
+ witnessUtxo: input.witnessUtxo,
335
+ nonWitnessUtxo: input.nonWitnessUtxo,
336
+ tapInternalKey: input.tapInternalKey,
337
+ sequence: 0xffffffff - 1,
338
+ bip32Derivation: input.bip32Derivation,
100
339
  };
101
- if (input.bip32Derivation)
102
- i.bip32Derivation = input.bip32Derivation;
340
+ if (!i.bip32Derivation)
341
+ delete i.bip32Derivation;
342
+ if (!i.nonWitnessUtxo)
343
+ delete i.nonWitnessUtxo;
103
344
  newPsbt.addInput(i);
104
345
  break;
105
346
  }
@@ -108,44 +349,56 @@ class BaseBitcoinLikeTransaction extends BaseBitcoinLikeTransactionBuilderCommon
108
349
  }
109
350
  }
110
351
  for (const target of outputs) {
111
- newPsbt.addOutput(target);
352
+ newPsbt.addOutput(Object.assign({}, target));
112
353
  }
113
354
  if (change && Object.keys(change).length !== 0) {
114
- newPsbt.addOutput({
115
- address: change.address,
116
- value: Number(change.value),
117
- });
118
- }
119
- for (let i in outputs) {
120
- if (newPsbt.txOutputs[i].address !== outputs[i].address) {
121
- throw new Error("error address");
122
- }
123
- if (newPsbt.txOutputs[i].value !== outputs[i].value) {
124
- throw new Error("error value");
125
- }
355
+ newPsbt.addOutput(change);
126
356
  }
127
357
  if (change && Object.keys(change).length !== 0) {
128
358
  if (newPsbt.txOutputs[outputs.length].address !== change.address) {
129
359
  throw new Error("error change address");
130
360
  }
131
- if (newPsbt.txOutputs[outputs.length].value !== change.value) {
132
- throw new Error("error change value");
133
- }
134
361
  }
135
362
  const unsignedPsbtBaseText = newPsbt.toBase64();
136
363
  return {
137
364
  unsignedTransaction: unsignedPsbtBaseText,
138
365
  outputs,
139
- inputs: inputs.map((tx) => ({
140
- hash: tx.hash,
141
- value: Number(tx.value),
142
- index: tx.index,
143
- signerInfo: tx.signerInfo,
366
+ inputs: inputs.map((utx) => ({
367
+ hash: utx.hash,
368
+ value: Number(utx.value),
369
+ index: utx.index,
370
+ signerInfo: utx.signerInfo,
144
371
  })),
145
372
  fee,
146
373
  change,
147
374
  };
148
375
  }
376
+ processUnsignedTransaction({ extendedUtxo, targets = [], changeAddress = undefined, fullAmount = false, feeRate, selfTransaction = false, selectType = "normal", }) {
377
+ return __awaiter(this, void 0, void 0, function* () {
378
+ if (!selfTransaction && targets.length === 0)
379
+ throw new Error("no target");
380
+ let changeObject = typeof changeAddress === "string"
381
+ ? {
382
+ address: changeAddress,
383
+ }
384
+ : changeAddress;
385
+ const { inputs, outputs, change, fee } = yield this.filterAndConvertTxDataToStandardFormat({
386
+ extendedUtxo,
387
+ targets,
388
+ changeObject,
389
+ feeRate,
390
+ selectType: fullAmount ? "full" : selectType,
391
+ });
392
+ let unsignedTransaction = this.createUnsignedTransaction({
393
+ inputs,
394
+ outputs,
395
+ change,
396
+ fee,
397
+ feeRate,
398
+ });
399
+ return unsignedTransaction;
400
+ });
401
+ }
149
402
  }
150
- module.exports = BaseBitcoinLikeTransaction;
403
+ exports.default = BaseBitcoinLikeTransaction;
151
404
  //# sourceMappingURL=transaction-builder.js.map