@btc-vision/transaction 1.2.3 → 1.2.5

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.
@@ -6,6 +6,7 @@ import { TransactionBuilder } from './builders/TransactionBuilder.js';
6
6
  import { TransactionType } from './enums/TransactionType.js';
7
7
  import { IChallengeSolutionTransactionParameters, IDeploymentParameters, IFundingTransactionParameters, IInteractionParameters } from './interfaces/ITransactionParameters.js';
8
8
  import { ChallengeSolutionTransaction } from './builders/ChallengeSolutionTransaction.js';
9
+ import { InteractionParametersWithoutSigner } from './browser/Web3Provider.js';
9
10
  export interface DeploymentResult {
10
11
  readonly transaction: [string, string];
11
12
  readonly contractAddress: string;
@@ -38,6 +39,13 @@ export interface BitcoinTransferBase {
38
39
  readonly estimatedFees: bigint;
39
40
  readonly nextUTXOs: UTXO[];
40
41
  }
42
+ export interface InteractionResponse {
43
+ readonly fundingTransaction: string;
44
+ readonly interactionTransaction: string;
45
+ readonly estimatedFees: bigint;
46
+ readonly nextUTXOs: UTXO[];
47
+ readonly preimage: string;
48
+ }
41
49
  export interface ChallengeSolution extends BitcoinTransferBase {
42
50
  readonly original: ChallengeSolutionTransaction;
43
51
  }
@@ -46,11 +54,12 @@ export interface BitcoinTransferResponse extends BitcoinTransferBase {
46
54
  }
47
55
  export declare class TransactionFactory {
48
56
  createCustomScriptTransaction(interactionParameters: ICustomTransactionParameters): Promise<[string, string, UTXO[]]>;
49
- signInteraction(interactionParameters: IInteractionParameters): Promise<[string, string, UTXO[], string]>;
57
+ signInteraction(interactionParameters: IInteractionParameters | InteractionParametersWithoutSigner): Promise<InteractionResponse>;
50
58
  signDeployment(deploymentParameters: IDeploymentParameters): Promise<DeploymentResult>;
51
59
  createBTCTransfer(parameters: IFundingTransactionParameters): Promise<BitcoinTransferResponse>;
52
60
  createChallengeSolution(parameters: IChallengeSolutionTransactionParameters): Promise<ChallengeSolution>;
53
61
  getAllNewUTXOs(original: TransactionBuilder<TransactionType>, tx: Transaction, to: string): UTXO[];
62
+ private detectInteractionOPWallet;
54
63
  private _createChallengeSolution;
55
64
  private createFundTransaction;
56
65
  private writePSBTHeader;
@@ -1,6 +1,6 @@
1
1
  import { IDeploymentParameters, IInteractionParameters } from '../interfaces/ITransactionParameters.js';
2
2
  import { UTXO } from '../../utxo/interfaces/IUTXO.js';
3
- import { DeploymentResult } from '../TransactionFactory';
3
+ import { DeploymentResult, InteractionResponse } from '../TransactionFactory';
4
4
  export type InteractionParametersWithoutSigner = Omit<IInteractionParameters, 'signer'>;
5
5
  export type IDeploymentParametersWithoutSigner = Omit<IDeploymentParameters, 'signer' | 'network'>;
6
6
  export interface BroadcastTransactionOptions {
@@ -15,8 +15,8 @@ export interface BroadcastedTransaction {
15
15
  readonly identifier: bigint | string;
16
16
  }
17
17
  export interface Web3Provider {
18
- signInteraction(interactionParameters: InteractionParametersWithoutSigner): Promise<[string, string, UTXO[]]>;
19
- signAndBroadcastInteraction(interactionParameters: InteractionParametersWithoutSigner): Promise<[BroadcastedTransaction, BroadcastedTransaction, UTXO[]]>;
18
+ signInteraction(interactionParameters: InteractionParametersWithoutSigner): Promise<InteractionResponse>;
19
+ signAndBroadcastInteraction(interactionParameters: InteractionParametersWithoutSigner): Promise<[BroadcastedTransaction, BroadcastedTransaction, UTXO[], string]>;
20
20
  deployContract(params: IDeploymentParametersWithoutSigner): Promise<DeploymentResult>;
21
21
  broadcast(transactions: BroadcastTransactionOptions[]): Promise<BroadcastedTransaction[]>;
22
22
  }
@@ -1 +1 @@
1
- export declare const version = "1.2.3";
1
+ export declare const version = "1.2.5";
package/build/_version.js CHANGED
@@ -1 +1 @@
1
- export const version = '1.2.3';
1
+ export const version = '1.2.5';
@@ -8,27 +8,27 @@ export class BinaryWriter {
8
8
  }
9
9
  writeU8(value) {
10
10
  if (value > 255)
11
- throw new Error('Value is too large.');
11
+ throw new Error('u8 value is too large.');
12
12
  this.allocSafe(U8_BYTE_LENGTH);
13
13
  this.buffer.setUint8(this.currentOffset++, value);
14
14
  }
15
15
  writeU16(value) {
16
16
  if (value > 65535)
17
- throw new Error('Value is too large.');
17
+ throw new Error('u16 value is too large.');
18
18
  this.allocSafe(U16_BYTE_LENGTH);
19
19
  this.buffer.setUint16(this.currentOffset, value, true);
20
20
  this.currentOffset += 2;
21
21
  }
22
22
  writeU32(value, le = true) {
23
23
  if (value > 4294967295)
24
- throw new Error('Value is too large.');
24
+ throw new Error('u32 value is too large.');
25
25
  this.allocSafe(U32_BYTE_LENGTH);
26
26
  this.buffer.setUint32(this.currentOffset, value, le);
27
27
  this.currentOffset += 4;
28
28
  }
29
29
  writeU64(value) {
30
30
  if (value > 18446744073709551615n)
31
- throw new Error('Value is too large.');
31
+ throw new Error('u64 value is too large.');
32
32
  this.allocSafe(U64_BYTE_LENGTH);
33
33
  this.buffer.setBigUint64(this.currentOffset, value, true);
34
34
  this.currentOffset += 8;
@@ -42,7 +42,7 @@ export class BinaryWriter {
42
42
  writeU256(bigIntValue) {
43
43
  if (bigIntValue >
44
44
  115792089237316195423570985008687907853269984665640564039457584007913129639935n) {
45
- throw new Error('Value is too large.');
45
+ throw new Error('u256 value is too large.');
46
46
  }
47
47
  this.allocSafe(U256_BYTE_LENGTH);
48
48
  const bytesToHex = BufferHelper.valueToUint8Array(bigIntValue);
@@ -55,7 +55,7 @@ export class BinaryWriter {
55
55
  }
56
56
  writeU128(bigIntValue) {
57
57
  if (bigIntValue > 340282366920938463463374607431768211455n) {
58
- throw new Error('Value is too large.');
58
+ throw new Error('u128 value is too large.');
59
59
  }
60
60
  this.allocSafe(U128_BYTE_LENGTH);
61
61
  const bytesToHex = BufferHelper.valueToUint8Array(bigIntValue, U128_BYTE_LENGTH);
@@ -42,6 +42,7 @@ export class MineableReward extends Generator {
42
42
  return compiled;
43
43
  }
44
44
  isTestnet() {
45
- return this.network === networks.testnet || this.network === networks.regtest;
45
+ return (this.network.bech32 === networks.testnet.bech32 ||
46
+ this.network.bech32 === networks.regtest.bech32);
46
47
  }
47
48
  }
@@ -6,6 +6,7 @@ import { TransactionBuilder } from './builders/TransactionBuilder.js';
6
6
  import { TransactionType } from './enums/TransactionType.js';
7
7
  import { IChallengeSolutionTransactionParameters, IDeploymentParameters, IFundingTransactionParameters, IInteractionParameters } from './interfaces/ITransactionParameters.js';
8
8
  import { ChallengeSolutionTransaction } from './builders/ChallengeSolutionTransaction.js';
9
+ import { InteractionParametersWithoutSigner } from './browser/Web3Provider.js';
9
10
  export interface DeploymentResult {
10
11
  readonly transaction: [string, string];
11
12
  readonly contractAddress: string;
@@ -38,6 +39,13 @@ export interface BitcoinTransferBase {
38
39
  readonly estimatedFees: bigint;
39
40
  readonly nextUTXOs: UTXO[];
40
41
  }
42
+ export interface InteractionResponse {
43
+ readonly fundingTransaction: string;
44
+ readonly interactionTransaction: string;
45
+ readonly estimatedFees: bigint;
46
+ readonly nextUTXOs: UTXO[];
47
+ readonly preimage: string;
48
+ }
41
49
  export interface ChallengeSolution extends BitcoinTransferBase {
42
50
  readonly original: ChallengeSolutionTransaction;
43
51
  }
@@ -46,11 +54,12 @@ export interface BitcoinTransferResponse extends BitcoinTransferBase {
46
54
  }
47
55
  export declare class TransactionFactory {
48
56
  createCustomScriptTransaction(interactionParameters: ICustomTransactionParameters): Promise<[string, string, UTXO[]]>;
49
- signInteraction(interactionParameters: IInteractionParameters): Promise<[string, string, UTXO[], string]>;
57
+ signInteraction(interactionParameters: IInteractionParameters | InteractionParametersWithoutSigner): Promise<InteractionResponse>;
50
58
  signDeployment(deploymentParameters: IDeploymentParameters): Promise<DeploymentResult>;
51
59
  createBTCTransfer(parameters: IFundingTransactionParameters): Promise<BitcoinTransferResponse>;
52
60
  createChallengeSolution(parameters: IChallengeSolutionTransactionParameters): Promise<ChallengeSolution>;
53
61
  getAllNewUTXOs(original: TransactionBuilder<TransactionType>, tx: Transaction, to: string): UTXO[];
62
+ private detectInteractionOPWallet;
54
63
  private _createChallengeSolution;
55
64
  private createFundTransaction;
56
65
  private writePSBTHeader;
@@ -59,6 +59,13 @@ export class TransactionFactory {
59
59
  if (!interactionParameters.utxos[0]) {
60
60
  throw new Error('Missing at least one UTXO.');
61
61
  }
62
+ const opWalletInteraction = await this.detectInteractionOPWallet(interactionParameters);
63
+ if (opWalletInteraction) {
64
+ return opWalletInteraction;
65
+ }
66
+ if (!('signer' in interactionParameters)) {
67
+ throw new Error('Field "signer" not provided, OP_WALLET not detected.');
68
+ }
62
69
  const preTransaction = new InteractionTransaction({
63
70
  ...interactionParameters,
64
71
  utxos: [interactionParameters.utxos[0]],
@@ -96,12 +103,13 @@ export class TransactionFactory {
96
103
  };
97
104
  const finalTransaction = new InteractionTransaction(newParams);
98
105
  const outTx = await finalTransaction.signTransaction();
99
- return [
100
- signedTransaction.tx.toHex(),
101
- outTx.toHex(),
102
- this.getUTXOAsTransaction(signedTransaction.tx, interactionParameters.from, 1),
103
- preTransaction.getPreimage().toString('hex'),
104
- ];
106
+ return {
107
+ fundingTransaction: signedTransaction.tx.toHex(),
108
+ interactionTransaction: outTx.toHex(),
109
+ estimatedFees: preTransaction.estimatedFees,
110
+ nextUTXOs: this.getUTXOAsTransaction(signedTransaction.tx, interactionParameters.from, 1),
111
+ preimage: preTransaction.getPreimage().toString('hex'),
112
+ };
105
113
  }
106
114
  async signDeployment(deploymentParameters) {
107
115
  const preTransaction = new DeploymentTransaction(deploymentParameters);
@@ -195,6 +203,17 @@ export class TransactionFactory {
195
203
  }
196
204
  return utxos;
197
205
  }
206
+ async detectInteractionOPWallet(interactionParameters) {
207
+ if (typeof window === 'undefined' || !window.opnet || !window.opnet.web3) {
208
+ return null;
209
+ }
210
+ const opnet = window.opnet.web3;
211
+ const interaction = await opnet.signInteraction(interactionParameters);
212
+ if (!interaction) {
213
+ throw new Error('Could not sign interaction transaction.');
214
+ }
215
+ return interaction;
216
+ }
198
217
  async _createChallengeSolution(parameters) {
199
218
  if (!parameters.to)
200
219
  throw new Error('Field "to" not provided.');
@@ -1,6 +1,6 @@
1
1
  import { IDeploymentParameters, IInteractionParameters } from '../interfaces/ITransactionParameters.js';
2
2
  import { UTXO } from '../../utxo/interfaces/IUTXO.js';
3
- import { DeploymentResult } from '../TransactionFactory';
3
+ import { DeploymentResult, InteractionResponse } from '../TransactionFactory';
4
4
  export type InteractionParametersWithoutSigner = Omit<IInteractionParameters, 'signer'>;
5
5
  export type IDeploymentParametersWithoutSigner = Omit<IDeploymentParameters, 'signer' | 'network'>;
6
6
  export interface BroadcastTransactionOptions {
@@ -15,8 +15,8 @@ export interface BroadcastedTransaction {
15
15
  readonly identifier: bigint | string;
16
16
  }
17
17
  export interface Web3Provider {
18
- signInteraction(interactionParameters: InteractionParametersWithoutSigner): Promise<[string, string, UTXO[]]>;
19
- signAndBroadcastInteraction(interactionParameters: InteractionParametersWithoutSigner): Promise<[BroadcastedTransaction, BroadcastedTransaction, UTXO[]]>;
18
+ signInteraction(interactionParameters: InteractionParametersWithoutSigner): Promise<InteractionResponse>;
19
+ signAndBroadcastInteraction(interactionParameters: InteractionParametersWithoutSigner): Promise<[BroadcastedTransaction, BroadcastedTransaction, UTXO[], string]>;
20
20
  deployContract(params: IDeploymentParametersWithoutSigner): Promise<DeploymentResult>;
21
21
  broadcast(transactions: BroadcastTransactionOptions[]): Promise<BroadcastedTransaction[]>;
22
22
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@btc-vision/transaction",
3
3
  "type": "module",
4
- "version": "1.2.3",
4
+ "version": "1.2.5",
5
5
  "author": "BlobMaster41",
6
6
  "description": "OPNet transaction library allows you to create and sign transactions for the OPNet network.",
7
7
  "engines": {
package/src/_version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '1.2.3';
1
+ export const version = '1.2.5';
@@ -22,14 +22,14 @@ export class BinaryWriter {
22
22
  }
23
23
 
24
24
  public writeU8(value: u8): void {
25
- if (value > 255) throw new Error('Value is too large.');
25
+ if (value > 255) throw new Error('u8 value is too large.');
26
26
 
27
27
  this.allocSafe(U8_BYTE_LENGTH);
28
28
  this.buffer.setUint8(this.currentOffset++, value);
29
29
  }
30
30
 
31
31
  public writeU16(value: u16): void {
32
- if (value > 65535) throw new Error('Value is too large.');
32
+ if (value > 65535) throw new Error('u16 value is too large.');
33
33
 
34
34
  this.allocSafe(U16_BYTE_LENGTH);
35
35
  this.buffer.setUint16(this.currentOffset, value, true);
@@ -37,7 +37,7 @@ export class BinaryWriter {
37
37
  }
38
38
 
39
39
  public writeU32(value: u32, le: boolean = true): void {
40
- if (value > 4294967295) throw new Error('Value is too large.');
40
+ if (value > 4294967295) throw new Error('u32 value is too large.');
41
41
 
42
42
  this.allocSafe(U32_BYTE_LENGTH);
43
43
  this.buffer.setUint32(this.currentOffset, value, le);
@@ -45,7 +45,7 @@ export class BinaryWriter {
45
45
  }
46
46
 
47
47
  public writeU64(value: u64): void {
48
- if (value > 18446744073709551615n) throw new Error('Value is too large.');
48
+ if (value > 18446744073709551615n) throw new Error('u64 value is too large.');
49
49
 
50
50
  this.allocSafe(U64_BYTE_LENGTH);
51
51
  this.buffer.setBigUint64(this.currentOffset, value, true);
@@ -65,7 +65,7 @@ export class BinaryWriter {
65
65
  bigIntValue >
66
66
  115792089237316195423570985008687907853269984665640564039457584007913129639935n
67
67
  ) {
68
- throw new Error('Value is too large.');
68
+ throw new Error('u256 value is too large.');
69
69
  }
70
70
 
71
71
  this.allocSafe(U256_BYTE_LENGTH);
@@ -82,7 +82,7 @@ export class BinaryWriter {
82
82
 
83
83
  public writeU128(bigIntValue: bigint): void {
84
84
  if (bigIntValue > 340282366920938463463374607431768211455n) {
85
- throw new Error('Value is too large.');
85
+ throw new Error('u128 value is too large.');
86
86
  }
87
87
 
88
88
  this.allocSafe(U128_BYTE_LENGTH);
@@ -58,6 +58,9 @@ export class MineableReward extends Generator {
58
58
  }
59
59
 
60
60
  private isTestnet(): boolean {
61
- return this.network === networks.testnet || this.network === networks.regtest;
61
+ return (
62
+ this.network.bech32 === networks.testnet.bech32 ||
63
+ this.network.bech32 === networks.regtest.bech32
64
+ );
62
65
  }
63
66
  }
@@ -19,6 +19,7 @@ import {
19
19
  } from './interfaces/ITransactionParameters.js';
20
20
  import { PSBTTypes } from './psbt/PSBTTypes.js';
21
21
  import { ChallengeSolutionTransaction } from './builders/ChallengeSolutionTransaction.js';
22
+ import { InteractionParametersWithoutSigner } from './browser/Web3Provider.js';
22
23
 
23
24
  export interface DeploymentResult {
24
25
  readonly transaction: [string, string];
@@ -60,6 +61,14 @@ export interface BitcoinTransferBase {
60
61
  readonly nextUTXOs: UTXO[];
61
62
  }
62
63
 
64
+ export interface InteractionResponse {
65
+ readonly fundingTransaction: string;
66
+ readonly interactionTransaction: string;
67
+ readonly estimatedFees: bigint;
68
+ readonly nextUTXOs: UTXO[];
69
+ readonly preimage: string;
70
+ }
71
+
63
72
  export interface ChallengeSolution extends BitcoinTransferBase {
64
73
  readonly original: ChallengeSolutionTransaction;
65
74
  }
@@ -141,11 +150,11 @@ export class TransactionFactory {
141
150
 
142
151
  /**
143
152
  * @description Generates the required transactions.
144
- * @returns {Promise<[string, string]>} - The signed transaction
153
+ * @returns {Promise<InteractionResponse>} - The signed transaction
145
154
  */
146
155
  public async signInteraction(
147
- interactionParameters: IInteractionParameters,
148
- ): Promise<[string, string, UTXO[], string]> {
156
+ interactionParameters: IInteractionParameters | InteractionParametersWithoutSigner,
157
+ ): Promise<InteractionResponse> {
149
158
  if (!interactionParameters.to) {
150
159
  throw new Error('Field "to" not provided.');
151
160
  }
@@ -158,6 +167,16 @@ export class TransactionFactory {
158
167
  throw new Error('Missing at least one UTXO.');
159
168
  }
160
169
 
170
+ // If OP_WALLET is used...
171
+ const opWalletInteraction = await this.detectInteractionOPWallet(interactionParameters);
172
+ if (opWalletInteraction) {
173
+ return opWalletInteraction;
174
+ }
175
+
176
+ if (!('signer' in interactionParameters)) {
177
+ throw new Error('Field "signer" not provided, OP_WALLET not detected.');
178
+ }
179
+
161
180
  const preTransaction: InteractionTransaction = new InteractionTransaction({
162
181
  ...interactionParameters,
163
182
  utxos: [interactionParameters.utxos[0]], // we simulate one input here.
@@ -212,12 +231,17 @@ export class TransactionFactory {
212
231
 
213
232
  // We have to regenerate using the new utxo
214
233
  const outTx: Transaction = await finalTransaction.signTransaction();
215
- return [
216
- signedTransaction.tx.toHex(),
217
- outTx.toHex(),
218
- this.getUTXOAsTransaction(signedTransaction.tx, interactionParameters.from, 1), // always 1
219
- preTransaction.getPreimage().toString('hex'),
220
- ];
234
+ return {
235
+ fundingTransaction: signedTransaction.tx.toHex(),
236
+ interactionTransaction: outTx.toHex(),
237
+ estimatedFees: preTransaction.estimatedFees,
238
+ nextUTXOs: this.getUTXOAsTransaction(
239
+ signedTransaction.tx,
240
+ interactionParameters.from,
241
+ 1,
242
+ ), // always 1
243
+ preimage: preTransaction.getPreimage().toString('hex'),
244
+ };
221
245
  }
222
246
 
223
247
  /**
@@ -365,6 +389,22 @@ export class TransactionFactory {
365
389
  return utxos;
366
390
  }
367
391
 
392
+ private async detectInteractionOPWallet(
393
+ interactionParameters: IInteractionParameters | InteractionParametersWithoutSigner,
394
+ ): Promise<InteractionResponse | null> {
395
+ if (typeof window === 'undefined' || !window.opnet || !window.opnet.web3) {
396
+ return null;
397
+ }
398
+
399
+ const opnet = window.opnet.web3;
400
+ const interaction = await opnet.signInteraction(interactionParameters);
401
+ if (!interaction) {
402
+ throw new Error('Could not sign interaction transaction.');
403
+ }
404
+
405
+ return interaction;
406
+ }
407
+
368
408
  private async _createChallengeSolution(
369
409
  parameters: IChallengeSolutionTransactionParameters,
370
410
  ): Promise<ChallengeSolutionResponse> {
@@ -3,7 +3,7 @@ import {
3
3
  IInteractionParameters,
4
4
  } from '../interfaces/ITransactionParameters.js';
5
5
  import { UTXO } from '../../utxo/interfaces/IUTXO.js';
6
- import { DeploymentResult } from '../TransactionFactory';
6
+ import { DeploymentResult, InteractionResponse } from '../TransactionFactory';
7
7
 
8
8
  export type InteractionParametersWithoutSigner = Omit<IInteractionParameters, 'signer'>;
9
9
  export type IDeploymentParametersWithoutSigner = Omit<IDeploymentParameters, 'signer' | 'network'>;
@@ -41,11 +41,11 @@ export interface BroadcastedTransaction {
41
41
  export interface Web3Provider {
42
42
  signInteraction(
43
43
  interactionParameters: InteractionParametersWithoutSigner,
44
- ): Promise<[string, string, UTXO[]]>;
44
+ ): Promise<InteractionResponse>;
45
45
 
46
46
  signAndBroadcastInteraction(
47
47
  interactionParameters: InteractionParametersWithoutSigner,
48
- ): Promise<[BroadcastedTransaction, BroadcastedTransaction, UTXO[]]>;
48
+ ): Promise<[BroadcastedTransaction, BroadcastedTransaction, UTXO[], string]>;
49
49
 
50
50
  deployContract(params: IDeploymentParametersWithoutSigner): Promise<DeploymentResult>;
51
51