@btc-vision/transaction 1.2.1 → 1.2.3
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.
- package/browser/_version.d.ts +1 -1
- package/browser/crypto/crypto-browser.d.ts +1 -1
- package/browser/generators/builders/CalldataGenerator.d.ts +1 -1
- package/browser/generators/builders/DeploymentGenerator.d.ts +1 -1
- package/browser/generators/builders/MineableReward.d.ts +7 -0
- package/browser/index.js +1 -1
- package/browser/opnet.d.ts +4 -0
- package/browser/transaction/ContractAddress.d.ts +1 -0
- package/browser/transaction/TransactionFactory.d.ts +19 -4
- package/browser/transaction/browser/WalletConnection.d.ts +18 -0
- package/browser/transaction/browser/types/Xverse.d.ts +24 -10
- package/browser/transaction/builders/ChallengeSolutionTransaction.d.ts +18 -0
- package/browser/transaction/builders/DeploymentTransaction.d.ts +4 -0
- package/browser/transaction/builders/SharedInteractionTransaction.d.ts +5 -0
- package/browser/transaction/builders/TransactionBuilder.d.ts +2 -0
- package/browser/transaction/enums/TransactionType.d.ts +3 -4
- package/browser/transaction/interfaces/ITransactionParameters.d.ts +6 -0
- package/browser/transaction/mineable/ChallengeGenerator.d.ts +9 -0
- package/browser/utxo/interfaces/IUTXO.d.ts +1 -1
- package/browser/verification/TapscriptVerificator.d.ts +1 -1
- package/build/_version.d.ts +1 -1
- package/build/_version.js +1 -1
- package/build/generators/Generator.js +1 -1
- package/build/generators/builders/CalldataGenerator.d.ts +1 -1
- package/build/generators/builders/CalldataGenerator.js +7 -26
- package/build/generators/builders/DeploymentGenerator.d.ts +1 -1
- package/build/generators/builders/DeploymentGenerator.js +9 -6
- package/build/generators/builders/LegacyCalldataGenerator.js +5 -1
- package/build/generators/builders/MineableReward.d.ts +7 -0
- package/build/generators/builders/MineableReward.js +47 -0
- package/build/opnet.d.ts +4 -0
- package/build/opnet.js +4 -0
- package/build/transaction/ContractAddress.d.ts +1 -0
- package/build/transaction/ContractAddress.js +4 -1
- package/build/transaction/TransactionFactory.d.ts +19 -4
- package/build/transaction/TransactionFactory.js +32 -0
- package/build/transaction/browser/WalletConnection.d.ts +18 -0
- package/build/transaction/browser/WalletConnection.js +95 -0
- package/build/transaction/browser/extensions/UnisatSigner.js +5 -2
- package/build/transaction/browser/extensions/XverseSigner.js +15 -9
- package/build/transaction/browser/types/Xverse.d.ts +24 -10
- package/build/transaction/builders/ChallengeSolutionTransaction.d.ts +18 -0
- package/build/transaction/builders/ChallengeSolutionTransaction.js +51 -0
- package/build/transaction/builders/DeploymentTransaction.d.ts +4 -0
- package/build/transaction/builders/DeploymentTransaction.js +23 -4
- package/build/transaction/builders/InteractionTransaction.js +1 -1
- package/build/transaction/builders/SharedInteractionTransaction.d.ts +5 -0
- package/build/transaction/builders/SharedInteractionTransaction.js +35 -13
- package/build/transaction/builders/TransactionBuilder.d.ts +2 -0
- package/build/transaction/builders/TransactionBuilder.js +2 -0
- package/build/transaction/enums/TransactionType.d.ts +3 -4
- package/build/transaction/enums/TransactionType.js +3 -4
- package/build/transaction/interfaces/ITransactionParameters.d.ts +6 -0
- package/build/transaction/mineable/ChallengeGenerator.d.ts +9 -0
- package/build/transaction/mineable/ChallengeGenerator.js +28 -0
- package/build/transaction/shared/TweakedTransaction.js +1 -1
- package/build/utxo/interfaces/IUTXO.d.ts +1 -1
- package/build/verification/TapscriptVerificator.d.ts +1 -1
- package/build/verification/TapscriptVerificator.js +2 -8
- package/package.json +3 -3
- package/src/_version.ts +1 -1
- package/src/generators/Generator.ts +1 -1
- package/src/generators/builders/CalldataGenerator.ts +10 -41
- package/src/generators/builders/DeploymentGenerator.ts +18 -7
- package/src/generators/builders/LegacyCalldataGenerator.ts +5 -1
- package/src/generators/builders/MineableReward.ts +63 -0
- package/src/opnet.ts +5 -0
- package/src/transaction/ContractAddress.ts +5 -1
- package/src/transaction/TransactionFactory.ts +66 -3
- package/src/transaction/browser/WalletConnection.ts +110 -0
- package/src/transaction/browser/extensions/UnisatSigner.ts +7 -3
- package/src/transaction/browser/extensions/XverseSigner.ts +24 -23
- package/src/transaction/browser/types/Xverse.ts +50 -36
- package/src/transaction/builders/ChallengeSolutionTransaction.ts +88 -0
- package/src/transaction/builders/DeploymentTransaction.ts +46 -3
- package/src/transaction/builders/InteractionTransaction.ts +1 -0
- package/src/transaction/builders/SharedInteractionTransaction.ts +54 -14
- package/src/transaction/builders/TransactionBuilder.ts +3 -0
- package/src/transaction/enums/TransactionType.ts +3 -4
- package/src/transaction/interfaces/ITransactionParameters.ts +8 -15
- package/src/transaction/mineable/ChallengeGenerator.ts +39 -0
- package/src/transaction/shared/TweakedTransaction.ts +1 -1
- package/src/utxo/interfaces/IUTXO.ts +1 -1
- package/src/verification/TapscriptVerificator.ts +3 -18
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { address, Payment, Psbt, PsbtInput, Signer, Taptree, toXOnly } from '@btc-vision/bitcoin';
|
|
2
2
|
import { ECPairInterface } from 'ecpair';
|
|
3
|
-
import { TransactionBuilder } from './TransactionBuilder.js';
|
|
3
|
+
import { MINIMUM_AMOUNT_CA, MINIMUM_AMOUNT_REWARD, TransactionBuilder } from './TransactionBuilder.js';
|
|
4
4
|
import { TransactionType } from '../enums/TransactionType.js';
|
|
5
5
|
import { CalldataGenerator } from '../../generators/builders/CalldataGenerator.js';
|
|
6
6
|
import { SharedInteractionParameters } from '../interfaces/ITransactionParameters.js';
|
|
@@ -8,6 +8,7 @@ import { Compressor } from '../../bytecode/Compressor.js';
|
|
|
8
8
|
import { EcKeyPair } from '../../keypair/EcKeyPair.js';
|
|
9
9
|
import { BitcoinUtils } from '../../utils/BitcoinUtils.js';
|
|
10
10
|
import { UnisatSigner } from '../browser/extensions/UnisatSigner.js';
|
|
11
|
+
import { ChallengeGenerator, IMineableReward } from '../mineable/ChallengeGenerator.js';
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* Shared interaction transaction
|
|
@@ -30,6 +31,9 @@ export abstract class SharedInteractionTransaction<
|
|
|
30
31
|
protected abstract readonly compiledTargetScript: Buffer;
|
|
31
32
|
protected abstract readonly scriptTree: Taptree;
|
|
32
33
|
|
|
34
|
+
protected readonly preimage: Buffer; // ALWAYS 128 bytes for the preimage
|
|
35
|
+
protected readonly rewardChallenge: IMineableReward;
|
|
36
|
+
|
|
33
37
|
protected calldataGenerator: CalldataGenerator;
|
|
34
38
|
|
|
35
39
|
/**
|
|
@@ -63,7 +67,13 @@ export abstract class SharedInteractionTransaction<
|
|
|
63
67
|
throw new Error('Calldata is required');
|
|
64
68
|
}
|
|
65
69
|
|
|
70
|
+
this.preimage = parameters.preimage || BitcoinUtils.getSafeRandomValues(128);
|
|
71
|
+
|
|
66
72
|
this.disableAutoRefund = parameters.disableAutoRefund || false;
|
|
73
|
+
this.rewardChallenge = ChallengeGenerator.generateMineableReward(
|
|
74
|
+
this.preimage,
|
|
75
|
+
this.network,
|
|
76
|
+
);
|
|
67
77
|
|
|
68
78
|
this.calldata = Compressor.compress(parameters.calldata);
|
|
69
79
|
|
|
@@ -93,6 +103,13 @@ export abstract class SharedInteractionTransaction<
|
|
|
93
103
|
return this.randomBytes;
|
|
94
104
|
}
|
|
95
105
|
|
|
106
|
+
/**
|
|
107
|
+
* Get the preimage
|
|
108
|
+
*/
|
|
109
|
+
public getPreimage(): Buffer {
|
|
110
|
+
return this.preimage;
|
|
111
|
+
}
|
|
112
|
+
|
|
96
113
|
/**
|
|
97
114
|
* Generate the secret for the interaction
|
|
98
115
|
* @protected
|
|
@@ -134,8 +151,6 @@ export abstract class SharedInteractionTransaction<
|
|
|
134
151
|
* @throws {Error} If the to address is required
|
|
135
152
|
*/
|
|
136
153
|
protected override async buildTransaction(): Promise<void> {
|
|
137
|
-
if (!this.to) throw new Error('To address is required');
|
|
138
|
-
|
|
139
154
|
const selectedRedeem = this.scriptSigner
|
|
140
155
|
? this.targetScriptRedeem
|
|
141
156
|
: this.leftOverFundsScriptRedeem;
|
|
@@ -162,16 +177,7 @@ export abstract class SharedInteractionTransaction<
|
|
|
162
177
|
this.addInputsFromUTXO();
|
|
163
178
|
}
|
|
164
179
|
|
|
165
|
-
|
|
166
|
-
this.addOutput({
|
|
167
|
-
value: Number(amountSpent),
|
|
168
|
-
address: this.to,
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
const amount = this.addOptionalOutputsAndGetAmount();
|
|
172
|
-
if (!this.disableAutoRefund) {
|
|
173
|
-
await this.addRefundOutput(amountSpent + amount);
|
|
174
|
-
}
|
|
180
|
+
await this.createMineableRewardOutputs();
|
|
175
181
|
}
|
|
176
182
|
|
|
177
183
|
/**
|
|
@@ -236,7 +242,6 @@ export abstract class SharedInteractionTransaction<
|
|
|
236
242
|
|
|
237
243
|
return [
|
|
238
244
|
this.contractSecret,
|
|
239
|
-
this.internalPubKeyToXOnly(),
|
|
240
245
|
input.tapScriptSig[0].signature,
|
|
241
246
|
input.tapScriptSig[1].signature,
|
|
242
247
|
] as Buffer[];
|
|
@@ -311,6 +316,41 @@ export abstract class SharedInteractionTransaction<
|
|
|
311
316
|
}
|
|
312
317
|
}
|
|
313
318
|
|
|
319
|
+
private async createMineableRewardOutputs(): Promise<void> {
|
|
320
|
+
if (!this.to) throw new Error('To address is required');
|
|
321
|
+
|
|
322
|
+
const amountSpent: bigint = this.getTransactionOPNetFee();
|
|
323
|
+
|
|
324
|
+
let amountToCA: bigint;
|
|
325
|
+
if (amountSpent > MINIMUM_AMOUNT_REWARD + MINIMUM_AMOUNT_CA) {
|
|
326
|
+
amountToCA = MINIMUM_AMOUNT_CA;
|
|
327
|
+
} else {
|
|
328
|
+
amountToCA = amountSpent;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// ALWAYS THE FIRST INPUT.
|
|
332
|
+
this.addOutput({
|
|
333
|
+
value: Number(amountToCA),
|
|
334
|
+
address: this.to,
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
// ALWAYS SECOND.
|
|
338
|
+
if (
|
|
339
|
+
amountToCA === MINIMUM_AMOUNT_CA &&
|
|
340
|
+
amountSpent - MINIMUM_AMOUNT_CA > MINIMUM_AMOUNT_REWARD
|
|
341
|
+
) {
|
|
342
|
+
this.addOutput({
|
|
343
|
+
value: Number(amountSpent - amountToCA),
|
|
344
|
+
address: this.rewardChallenge.address,
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const amount = this.addOptionalOutputsAndGetAmount();
|
|
349
|
+
if (!this.disableAutoRefund) {
|
|
350
|
+
await this.addRefundOutput(amountSpent + amount);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
314
354
|
private async signInputsNonWalletBased(transaction: Psbt): Promise<void> {
|
|
315
355
|
for (let i = 0; i < transaction.data.inputs.length; i++) {
|
|
316
356
|
if (i === 0) {
|
|
@@ -26,6 +26,9 @@ import { UnisatSigner } from '../browser/extensions/UnisatSigner.js';
|
|
|
26
26
|
|
|
27
27
|
initEccLib(ecc);
|
|
28
28
|
|
|
29
|
+
export const MINIMUM_AMOUNT_REWARD: bigint = 540n;
|
|
30
|
+
export const MINIMUM_AMOUNT_CA: bigint = 330n;
|
|
31
|
+
|
|
29
32
|
/**
|
|
30
33
|
* Allows to build a transaction like you would on Ethereum.
|
|
31
34
|
* @description The transaction builder class
|
|
@@ -26,10 +26,17 @@ export interface IFundingTransactionParameters extends ITransactionParameters {
|
|
|
26
26
|
splitInputsInto?: number;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
export interface IChallengeSolutionTransactionParameters extends ITransactionParameters {
|
|
30
|
+
amount: bigint;
|
|
31
|
+
|
|
32
|
+
readonly challengeSolution: Buffer;
|
|
33
|
+
}
|
|
34
|
+
|
|
29
35
|
export interface SharedInteractionParameters extends ITransactionParameters {
|
|
30
36
|
calldata?: Buffer;
|
|
31
37
|
disableAutoRefund?: boolean;
|
|
32
38
|
|
|
39
|
+
readonly preimage?: Buffer;
|
|
33
40
|
readonly randomBytes?: Buffer;
|
|
34
41
|
}
|
|
35
42
|
|
|
@@ -39,24 +46,10 @@ export interface IInteractionParameters extends SharedInteractionParameters {
|
|
|
39
46
|
readonly to: string;
|
|
40
47
|
}
|
|
41
48
|
|
|
42
|
-
/*export interface IWrapParameters extends Omit<SharedInteractionParameters, 'optionalOutputs'> {
|
|
43
|
-
readonly to?: string;
|
|
44
|
-
readonly from: string;
|
|
45
|
-
|
|
46
|
-
readonly amount: bigint;
|
|
47
|
-
readonly receiver?: Address;
|
|
48
|
-
|
|
49
|
-
readonly generationParameters: WrappedGeneration;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface IUnwrapParameters extends Omit<SharedInteractionParameters, 'optionalOutputs'> {
|
|
53
|
-
readonly unwrapUTXOs: VaultUTXOs[];
|
|
54
|
-
readonly amount: bigint;
|
|
55
|
-
}*/
|
|
56
|
-
|
|
57
49
|
export interface IDeploymentParameters extends Omit<ITransactionParameters, 'to'> {
|
|
58
50
|
readonly bytecode: Buffer;
|
|
59
51
|
readonly calldata?: Buffer;
|
|
60
52
|
|
|
61
53
|
readonly randomBytes?: Buffer;
|
|
54
|
+
readonly preimage?: Buffer;
|
|
62
55
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import bitcoin, { Network } from '@btc-vision/bitcoin';
|
|
2
|
+
import { MineableReward } from '../../generators/builders/MineableReward.js';
|
|
3
|
+
|
|
4
|
+
export interface IMineableReward {
|
|
5
|
+
address: string;
|
|
6
|
+
p2shOutputScript: Buffer;
|
|
7
|
+
redeemScript: Buffer;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class ChallengeGenerator {
|
|
11
|
+
public static generateMineableReward(preimage1: Buffer, network: Network): IMineableReward {
|
|
12
|
+
const mineableReward = new MineableReward(Buffer.alloc(0), network);
|
|
13
|
+
const redeemScript = mineableReward.compile(preimage1);
|
|
14
|
+
const p2sh = bitcoin.payments.p2sh({
|
|
15
|
+
redeem: { output: redeemScript },
|
|
16
|
+
network,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const outputRedeem: Buffer | undefined = p2sh.redeem?.output;
|
|
20
|
+
if (!outputRedeem) {
|
|
21
|
+
throw new Error('Output redeem is required');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (!p2sh.address) {
|
|
25
|
+
throw new Error('P2SH address is required');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const p2shOutputScript: Buffer | undefined = p2sh?.redeem?.output;
|
|
29
|
+
if (!p2shOutputScript) {
|
|
30
|
+
throw new Error('No redeem output');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
address: p2sh.address,
|
|
35
|
+
p2shOutputScript,
|
|
36
|
+
redeemScript: redeemScript,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -814,7 +814,7 @@ export abstract class TweakedTransaction extends Logger {
|
|
|
814
814
|
} => {
|
|
815
815
|
const inputDecoded = this.inputs[inputIndex];
|
|
816
816
|
if (isP2SH && input.partialSig && inputDecoded && inputDecoded.redeemScript) {
|
|
817
|
-
const signatures = input.partialSig.map((sig) => sig.signature);
|
|
817
|
+
const signatures = input.partialSig.map((sig) => sig.signature) || [];
|
|
818
818
|
const scriptSig = script.compile([...signatures, inputDecoded.redeemScript]);
|
|
819
819
|
|
|
820
820
|
return {
|
|
@@ -9,13 +9,13 @@ import {
|
|
|
9
9
|
} from '@btc-vision/bitcoin';
|
|
10
10
|
import { DeploymentGenerator } from '../generators/builders/DeploymentGenerator.js';
|
|
11
11
|
import { TransactionBuilder } from '../transaction/builders/TransactionBuilder.js';
|
|
12
|
-
import { Address } from '../keypair/Address.js';
|
|
13
12
|
|
|
14
13
|
export interface ContractAddressVerificationParams {
|
|
15
14
|
readonly deployerPubKey: Buffer;
|
|
16
15
|
readonly contractSaltPubKey: Buffer;
|
|
17
16
|
readonly originalSalt: Buffer;
|
|
18
17
|
readonly bytecode: Buffer;
|
|
18
|
+
readonly preimage: Buffer;
|
|
19
19
|
readonly calldata?: Buffer;
|
|
20
20
|
readonly network?: Network;
|
|
21
21
|
}
|
|
@@ -36,6 +36,7 @@ export class TapscriptVerificator {
|
|
|
36
36
|
const compiledTargetScript: Buffer = scriptBuilder.compile(
|
|
37
37
|
params.bytecode,
|
|
38
38
|
params.originalSalt,
|
|
39
|
+
params.preimage,
|
|
39
40
|
params.calldata,
|
|
40
41
|
);
|
|
41
42
|
|
|
@@ -67,6 +68,7 @@ export class TapscriptVerificator {
|
|
|
67
68
|
const compiledTargetScript: Buffer = scriptBuilder.compile(
|
|
68
69
|
params.bytecode,
|
|
69
70
|
params.originalSalt,
|
|
71
|
+
params.preimage,
|
|
70
72
|
params.calldata,
|
|
71
73
|
);
|
|
72
74
|
|
|
@@ -112,23 +114,6 @@ export class TapscriptVerificator {
|
|
|
112
114
|
return bitCrypto.hash256(buf);
|
|
113
115
|
}
|
|
114
116
|
|
|
115
|
-
public static generateContractVirtualAddress(
|
|
116
|
-
deployerPubKey: Buffer,
|
|
117
|
-
bytecode: Buffer,
|
|
118
|
-
saltHash: Buffer,
|
|
119
|
-
network: Network = networks.bitcoin,
|
|
120
|
-
): string {
|
|
121
|
-
const virtualAddress: Buffer = TapscriptVerificator.getContractSeed(
|
|
122
|
-
deployerPubKey,
|
|
123
|
-
bytecode,
|
|
124
|
-
saltHash,
|
|
125
|
-
);
|
|
126
|
-
|
|
127
|
-
const address = new Address(virtualAddress);
|
|
128
|
-
|
|
129
|
-
return address.p2tr(network);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
117
|
public static generateAddressFromScript(
|
|
133
118
|
params: ContractAddressVerificationParams,
|
|
134
119
|
scriptTree: Taptree,
|