cashscript 0.13.0-next.5 → 0.13.0-next.7
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/dist/Argument.d.ts +16 -0
- package/dist/Argument.js +16 -0
- package/dist/Contract.d.ts +44 -6
- package/dist/Contract.js +38 -11
- package/dist/Errors.d.ts +9 -0
- package/dist/Errors.js +15 -0
- package/dist/SignatureTemplate.d.ts +50 -0
- package/dist/SignatureTemplate.js +49 -0
- package/dist/TransactionBuilder.d.ts +162 -3
- package/dist/TransactionBuilder.js +157 -7
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/interfaces.d.ts +4 -0
- package/dist/libauth-template/LibauthTemplate.d.ts +2 -2
- package/dist/libauth-template/LibauthTemplate.js +1 -2
- package/dist/network/ElectrumNetworkProvider.d.ts +33 -0
- package/dist/network/ElectrumNetworkProvider.js +78 -1
- package/dist/network/MockNetworkProvider.d.ts +35 -0
- package/dist/network/MockNetworkProvider.js +26 -0
- package/dist/network/errors.d.ts +19 -0
- package/dist/network/errors.js +38 -0
- package/dist/network/index.d.ts +0 -2
- package/dist/network/index.js +0 -2
- package/dist/transaction-utils.d.ts +30 -0
- package/dist/transaction-utils.js +55 -0
- package/dist/utils.d.ts +24 -2
- package/dist/utils.js +50 -14
- package/dist/walletconnect-utils.d.ts +23 -0
- package/dist/walletconnect-utils.js +23 -0
- package/package.json +5 -5
- package/dist/network/BitcoinRpcNetworkProvider.d.ts +0 -17
- package/dist/network/BitcoinRpcNetworkProvider.js +0 -32
- package/dist/network/FullStackNetworkProvider.d.ts +0 -41
- package/dist/network/FullStackNetworkProvider.js +0 -36
package/dist/Argument.d.ts
CHANGED
|
@@ -5,6 +5,22 @@ export type FunctionArgument = ConstructorArgument | SignatureTemplate;
|
|
|
5
5
|
export type EncodedConstructorArgument = Uint8Array;
|
|
6
6
|
export type EncodedFunctionArgument = Uint8Array | SignatureTemplate;
|
|
7
7
|
export type EncodeFunction = (arg: FunctionArgument, typeStr: string) => EncodedFunctionArgument;
|
|
8
|
+
/**
|
|
9
|
+
* Encode a single CashScript function argument to its on-stack byte representation.
|
|
10
|
+
*
|
|
11
|
+
* The encoding is chosen based on `typeStr`:
|
|
12
|
+
* - `bool`, `int`, `string` → encoded via their CashScript primitive encoders.
|
|
13
|
+
* - `sig` passed as a `SignatureTemplate` → returned as-is so the transaction builder can
|
|
14
|
+
* produce the signature at build time.
|
|
15
|
+
* - `sig` / `datasig` / `bytes[N]` passed as a `Uint8Array` or hex string → byte-checked and
|
|
16
|
+
* returned as bytes.
|
|
17
|
+
*
|
|
18
|
+
* @param argument - The runtime argument value provided by the caller.
|
|
19
|
+
* @param typeStr - The CashScript type string from the contract ABI (e.g. `int`, `bytes20`, `sig`).
|
|
20
|
+
* @returns The encoded argument, ready for inclusion in an unlocking script.
|
|
21
|
+
* @throws A `TypeError` when the JS type does not match the expected CashScript type, or when a
|
|
22
|
+
* bounded bytes type receives a value of the wrong length.
|
|
23
|
+
*/
|
|
8
24
|
export declare function encodeFunctionArgument(argument: FunctionArgument, typeStr: string): EncodedFunctionArgument;
|
|
9
25
|
export declare const encodeConstructorArguments: (artifact: Artifact, constructorArgs: ConstructorArgument[], encodeFunction?: EncodeFunction) => Uint8Array[];
|
|
10
26
|
export declare const encodeFunctionArguments: (abiFunction: AbiFunction, functionArgs: FunctionArgument[], encodeFunction?: EncodeFunction) => EncodedFunctionArgument[];
|
package/dist/Argument.js
CHANGED
|
@@ -2,6 +2,22 @@ import { hexToBin } from '@bitauth/libauth';
|
|
|
2
2
|
import { BytesType, encodeBool, encodeInt, encodeString, parseType, PrimitiveType, } from '@cashscript/utils';
|
|
3
3
|
import { TypeError } from './Errors.js';
|
|
4
4
|
import SignatureTemplate from './SignatureTemplate.js';
|
|
5
|
+
/**
|
|
6
|
+
* Encode a single CashScript function argument to its on-stack byte representation.
|
|
7
|
+
*
|
|
8
|
+
* The encoding is chosen based on `typeStr`:
|
|
9
|
+
* - `bool`, `int`, `string` → encoded via their CashScript primitive encoders.
|
|
10
|
+
* - `sig` passed as a `SignatureTemplate` → returned as-is so the transaction builder can
|
|
11
|
+
* produce the signature at build time.
|
|
12
|
+
* - `sig` / `datasig` / `bytes[N]` passed as a `Uint8Array` or hex string → byte-checked and
|
|
13
|
+
* returned as bytes.
|
|
14
|
+
*
|
|
15
|
+
* @param argument - The runtime argument value provided by the caller.
|
|
16
|
+
* @param typeStr - The CashScript type string from the contract ABI (e.g. `int`, `bytes20`, `sig`).
|
|
17
|
+
* @returns The encoded argument, ready for inclusion in an unlocking script.
|
|
18
|
+
* @throws A `TypeError` when the JS type does not match the expected CashScript type, or when a
|
|
19
|
+
* bounded bytes type receives a value of the wrong length.
|
|
20
|
+
*/
|
|
5
21
|
export function encodeFunctionArgument(argument, typeStr) {
|
|
6
22
|
let type = parseType(typeStr);
|
|
7
23
|
if (type === PrimitiveType.BOOL) {
|
package/dist/Contract.d.ts
CHANGED
|
@@ -11,23 +11,61 @@ type DefaultResolved<TArtifact extends Artifact> = {
|
|
|
11
11
|
constructorInputs: ParamsToTuple<TArtifact['constructorInputs']>;
|
|
12
12
|
unlock: AbiToFunctionMap<TArtifact['abi'], Unlocker>;
|
|
13
13
|
};
|
|
14
|
-
declare class
|
|
15
|
-
|
|
16
|
-
private options;
|
|
14
|
+
declare class ContractBase {
|
|
15
|
+
/** The contract name as defined in the source CashScript code. */
|
|
17
16
|
name: string;
|
|
17
|
+
/** The CashAddress of the contract. Not available for `p2s` contracts. */
|
|
18
18
|
address: string;
|
|
19
|
+
/** The token-aware CashAddress of the contract. Not available for `p2s` contracts. */
|
|
19
20
|
tokenAddress: string;
|
|
21
|
+
/** Hex-encoded locking bytecode of the contract. */
|
|
20
22
|
lockingBytecode: string;
|
|
23
|
+
/** Hex-encoded redeem bytecode of the contract (constructor args prepended to the compiled script). */
|
|
21
24
|
bytecode: string;
|
|
25
|
+
/** Size of the contract bytecode in bytes. */
|
|
22
26
|
bytesize: number;
|
|
27
|
+
/** Number of opcodes in the contract bytecode. */
|
|
23
28
|
opcount: number;
|
|
24
|
-
|
|
29
|
+
/** The network provider used to query UTXOs for this contract and get network information. */
|
|
25
30
|
provider: NetworkProvider;
|
|
26
|
-
|
|
31
|
+
/** The address type of this contract: `p2sh20`, `p2sh32`, or `p2s`. */
|
|
32
|
+
contractType: ContractType;
|
|
33
|
+
/** Encoded constructor arguments in the order expected by the contract's constructor. */
|
|
27
34
|
encodedConstructorArgs: Uint8Array[];
|
|
28
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Retrieve the total BCH balance of the contract by summing the satoshis of all UTXOs at the
|
|
37
|
+
* contract's address.
|
|
38
|
+
*
|
|
39
|
+
* @returns The total balance in satoshis.
|
|
40
|
+
*/
|
|
29
41
|
getBalance(): Promise<bigint>;
|
|
42
|
+
/**
|
|
43
|
+
* Retrieve all UTXOs (confirmed and unconfirmed) locked at the contract's address.
|
|
44
|
+
*
|
|
45
|
+
* @returns A list of UTXOs spendable by this contract.
|
|
46
|
+
*/
|
|
30
47
|
getUtxos(): Promise<Utxo[]>;
|
|
48
|
+
}
|
|
49
|
+
declare class ContractInternal<TArtifact extends Artifact, TResolved extends ResolvedConstraint, TContractType extends ContractType> extends ContractBase {
|
|
50
|
+
artifact: TArtifact;
|
|
51
|
+
private options;
|
|
52
|
+
contractType: TContractType;
|
|
53
|
+
/**
|
|
54
|
+
* Call a contract function to spend a UTXO locked by this contract. Use as
|
|
55
|
+
* `contract.unlock.<functionName>(...args)` — the returned value is passed as the `unlocker`
|
|
56
|
+
* argument of `transactionBuilder.addInput(utxo, unlocker)`.
|
|
57
|
+
*/
|
|
58
|
+
unlock: TResolved['unlock'];
|
|
59
|
+
/**
|
|
60
|
+
* Instantiate a contract from a compiled CashScript artifact.
|
|
61
|
+
*
|
|
62
|
+
* @param artifact - The compiled contract artifact produced by `cashc`.
|
|
63
|
+
* @param constructorArgs - Constructor arguments in the order defined in the contract source.
|
|
64
|
+
* @param options - Contract options including the network provider and (optional) address type.
|
|
65
|
+
* @throws If the artifact is missing required properties, was compiled with an unsupported
|
|
66
|
+
* compiler version, or if the number or types of constructor arguments does not match the artifact.
|
|
67
|
+
*/
|
|
68
|
+
constructor(artifact: TArtifact, constructorArgs: TResolved['constructorInputs'], options: ContractOptions<TContractType>);
|
|
31
69
|
private createUnlocker;
|
|
32
70
|
}
|
|
33
71
|
export type Contract<TArtifact extends Artifact = Artifact, TResolved extends ResolvedConstraint = DefaultResolved<TArtifact>, TContractType extends ContractType = ContractType> = [TContractType] extends ['p2s'] ? Omit<ContractInternal<TArtifact, TResolved, TContractType>, 'address' | 'tokenAddress'> : ContractInternal<TArtifact, TResolved, TContractType>;
|
package/dist/Contract.js
CHANGED
|
@@ -4,8 +4,45 @@ import { encodeFunctionArgument, encodeConstructorArguments, } from './Argument.
|
|
|
4
4
|
import { addressToLockScript, createUnlockingBytecode, createSighashPreimage, scriptToAddress, } from './utils.js';
|
|
5
5
|
import SignatureTemplate from './SignatureTemplate.js';
|
|
6
6
|
import semver from 'semver';
|
|
7
|
-
class
|
|
7
|
+
// Non-generic base class holding the public API whose types do not depend on the Contract's
|
|
8
|
+
// generic parameters. Declaring these members here (rather than on `ContractInternal<...>`) keeps
|
|
9
|
+
// IDE hovers clean — tooltips show `ContractBase.name` instead of the fully resolved generic
|
|
10
|
+
// `ContractInternal<{ readonly contractName: ...; readonly abi: ...; ... }, ...>.name`.
|
|
11
|
+
class ContractBase {
|
|
12
|
+
/**
|
|
13
|
+
* Retrieve the total BCH balance of the contract by summing the satoshis of all UTXOs at the
|
|
14
|
+
* contract's address.
|
|
15
|
+
*
|
|
16
|
+
* @returns The total balance in satoshis.
|
|
17
|
+
*/
|
|
18
|
+
async getBalance() {
|
|
19
|
+
const utxos = await this.getUtxos();
|
|
20
|
+
return utxos.reduce((acc, utxo) => acc + utxo.satoshis, 0n);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Retrieve all UTXOs (confirmed and unconfirmed) locked at the contract's address.
|
|
24
|
+
*
|
|
25
|
+
* @returns A list of UTXOs spendable by this contract.
|
|
26
|
+
*/
|
|
27
|
+
async getUtxos() {
|
|
28
|
+
if (this.contractType === 'p2s') {
|
|
29
|
+
return this.provider.getUtxosForLockingBytecode(this.bytecode);
|
|
30
|
+
}
|
|
31
|
+
return this.provider.getUtxos(this.address);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
class ContractInternal extends ContractBase {
|
|
35
|
+
/**
|
|
36
|
+
* Instantiate a contract from a compiled CashScript artifact.
|
|
37
|
+
*
|
|
38
|
+
* @param artifact - The compiled contract artifact produced by `cashc`.
|
|
39
|
+
* @param constructorArgs - Constructor arguments in the order defined in the contract source.
|
|
40
|
+
* @param options - Contract options including the network provider and (optional) address type.
|
|
41
|
+
* @throws If the artifact is missing required properties, was compiled with an unsupported
|
|
42
|
+
* compiler version, or if the number or types of constructor arguments does not match the artifact.
|
|
43
|
+
*/
|
|
8
44
|
constructor(artifact, constructorArgs, options) {
|
|
45
|
+
super();
|
|
9
46
|
this.artifact = artifact;
|
|
10
47
|
this.options = options;
|
|
11
48
|
this.provider = this.options.provider;
|
|
@@ -50,16 +87,6 @@ class ContractInternal {
|
|
|
50
87
|
this.bytesize = calculateBytesize(contractBytecodeScript);
|
|
51
88
|
this.opcount = countOpcodes(contractBytecodeScript);
|
|
52
89
|
}
|
|
53
|
-
async getBalance() {
|
|
54
|
-
const utxos = await this.getUtxos();
|
|
55
|
-
return utxos.reduce((acc, utxo) => acc + utxo.satoshis, 0n);
|
|
56
|
-
}
|
|
57
|
-
async getUtxos() {
|
|
58
|
-
if (this.contractType === 'p2s') {
|
|
59
|
-
return this.provider.getUtxosForLockingBytecode(this.bytecode);
|
|
60
|
-
}
|
|
61
|
-
return this.provider.getUtxos(this.address);
|
|
62
|
-
}
|
|
63
90
|
createUnlocker(abiFunction, selector) {
|
|
64
91
|
return (...args) => {
|
|
65
92
|
if (abiFunction.inputs.length !== args.length) {
|
package/dist/Errors.d.ts
CHANGED
|
@@ -11,9 +11,18 @@ export declare class OutputSatoshisTooSmallError extends Error {
|
|
|
11
11
|
export declare class OutputTokenAmountTooSmallError extends Error {
|
|
12
12
|
constructor(amount: bigint);
|
|
13
13
|
}
|
|
14
|
+
export declare class OutputTokenCategoryInvalidError extends Error {
|
|
15
|
+
constructor(category: string);
|
|
16
|
+
}
|
|
17
|
+
export declare class OutputTokenCommitmentInvalidError extends Error {
|
|
18
|
+
constructor(commitment: string);
|
|
19
|
+
}
|
|
14
20
|
export declare class TokensToNonTokenAddressError extends Error {
|
|
15
21
|
constructor(address: string);
|
|
16
22
|
}
|
|
23
|
+
export declare class OutputAddressNetworkMismatchError extends Error {
|
|
24
|
+
constructor(address: string, expectedNetworkPrefix: string);
|
|
25
|
+
}
|
|
17
26
|
export declare class NoDebugInformationInArtifactError extends Error {
|
|
18
27
|
constructor();
|
|
19
28
|
}
|
package/dist/Errors.js
CHANGED
|
@@ -19,11 +19,26 @@ export class OutputTokenAmountTooSmallError extends Error {
|
|
|
19
19
|
super(`Tried to add an output with ${amount} tokens, which is invalid`);
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
|
+
export class OutputTokenCategoryInvalidError extends Error {
|
|
23
|
+
constructor(category) {
|
|
24
|
+
super(`Provided token category ${category} is not a hex string`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export class OutputTokenCommitmentInvalidError extends Error {
|
|
28
|
+
constructor(commitment) {
|
|
29
|
+
super(`Provided token commitment ${commitment} is not a hex string`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
22
32
|
export class TokensToNonTokenAddressError extends Error {
|
|
23
33
|
constructor(address) {
|
|
24
34
|
super(`Tried to send tokens to an address without token support, ${address}.`);
|
|
25
35
|
}
|
|
26
36
|
}
|
|
37
|
+
export class OutputAddressNetworkMismatchError extends Error {
|
|
38
|
+
constructor(address, expectedNetworkPrefix) {
|
|
39
|
+
super(`Tried to add an output to an address on the wrong network, ${address}. Expected network prefix: ${expectedNetworkPrefix}.`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
27
42
|
export class NoDebugInformationInArtifactError extends Error {
|
|
28
43
|
constructor() {
|
|
29
44
|
super('No debug information found in artifact, please recompile with cashc version 0.10.0 or newer.');
|
|
@@ -1,14 +1,64 @@
|
|
|
1
1
|
import { HashType, SignatureAlgorithm, P2PKHUnlocker } from './interfaces.js';
|
|
2
|
+
/**
|
|
3
|
+
* A signature template used to sign CashScript transactions. Wraps a private key together with
|
|
4
|
+
* the desired `HashType` and `SignatureAlgorithm`, and is consumed by the `TransactionBuilder`
|
|
5
|
+
* whenever a `sig` argument is required or when unlocking a P2PKH input.
|
|
6
|
+
*/
|
|
2
7
|
export default class SignatureTemplate {
|
|
3
8
|
private hashtype;
|
|
4
9
|
private signatureAlgorithm;
|
|
10
|
+
/** The raw private key bytes used for signing. */
|
|
5
11
|
privateKey: Uint8Array;
|
|
12
|
+
/**
|
|
13
|
+
* Create a new SignatureTemplate.
|
|
14
|
+
*
|
|
15
|
+
* @param signer - A 32-byte private key (Uint8Array), a WIF or hex-encoded private key string,
|
|
16
|
+
* or any object exposing a `toWIF()` method (e.g. bitcore-lib or bitcoincashjs `ECPair`).
|
|
17
|
+
* @param hashtype - Sighash flags to use when signing. Defaults to `SIGHASH_ALL | SIGHASH_UTXOS`.
|
|
18
|
+
* @param signatureAlgorithm - The signature algorithm to use. Defaults to
|
|
19
|
+
* `SignatureAlgorithm.SCHNORR`.
|
|
20
|
+
*/
|
|
6
21
|
constructor(signer: Keypair | Uint8Array | string, hashtype?: HashType, signatureAlgorithm?: SignatureAlgorithm);
|
|
22
|
+
/**
|
|
23
|
+
* Sign the provided sighash payload and return the signature concatenated with the hashtype
|
|
24
|
+
* byte, ready to be used as a transaction signature.
|
|
25
|
+
*
|
|
26
|
+
* @param payload - The 32-byte sighash to sign.
|
|
27
|
+
* @param bchForkId - Whether to include the BCH fork id flag in the appended hashtype byte.
|
|
28
|
+
* Defaults to `true`.
|
|
29
|
+
* @returns The signature bytes followed by the hashtype byte.
|
|
30
|
+
*/
|
|
7
31
|
generateSignature(payload: Uint8Array, bchForkId?: boolean): Uint8Array;
|
|
32
|
+
/**
|
|
33
|
+
* Sign a raw 32-byte message hash using the template's private key and signature algorithm.
|
|
34
|
+
*
|
|
35
|
+
* @param payload - The 32-byte hash to sign.
|
|
36
|
+
* @returns The raw signature bytes (without an appended hashtype byte).
|
|
37
|
+
*/
|
|
8
38
|
signMessageHash(payload: Uint8Array): Uint8Array;
|
|
39
|
+
/**
|
|
40
|
+
* Get the sighash flags used by this template.
|
|
41
|
+
*
|
|
42
|
+
* @param bchForkId - Whether to OR in the BCH fork id flag. Defaults to `true`.
|
|
43
|
+
* @returns The combined hashtype byte.
|
|
44
|
+
*/
|
|
9
45
|
getHashType(bchForkId?: boolean): number;
|
|
46
|
+
/**
|
|
47
|
+
* @returns The signature algorithm (ECDSA or Schnorr) used by this template.
|
|
48
|
+
*/
|
|
10
49
|
getSignatureAlgorithm(): SignatureAlgorithm;
|
|
50
|
+
/**
|
|
51
|
+
* Derive the compressed public key that corresponds to the template's private key.
|
|
52
|
+
*
|
|
53
|
+
* @returns The 33-byte compressed public key.
|
|
54
|
+
*/
|
|
11
55
|
getPublicKey(): Uint8Array;
|
|
56
|
+
/**
|
|
57
|
+
* Build a P2PKH `Unlocker` for the address derived from this template's private key. The
|
|
58
|
+
* returned unlocker can be passed directly to `TransactionBuilder.addInput`.
|
|
59
|
+
*
|
|
60
|
+
* @returns An unlocker that signs the corresponding P2PKH UTXO.
|
|
61
|
+
*/
|
|
12
62
|
unlockP2PKH(): P2PKHUnlocker;
|
|
13
63
|
}
|
|
14
64
|
interface Keypair {
|
|
@@ -2,7 +2,21 @@ import { decodePrivateKeyWif, hexToBin, isHex, secp256k1, SigningSerializationFl
|
|
|
2
2
|
import { hash256, scriptToBytecode } from '@cashscript/utils';
|
|
3
3
|
import { HashType, SignatureAlgorithm, } from './interfaces.js';
|
|
4
4
|
import { createSighashPreimage, publicKeyToP2PKHLockingBytecode } from './utils.js';
|
|
5
|
+
/**
|
|
6
|
+
* A signature template used to sign CashScript transactions. Wraps a private key together with
|
|
7
|
+
* the desired `HashType` and `SignatureAlgorithm`, and is consumed by the `TransactionBuilder`
|
|
8
|
+
* whenever a `sig` argument is required or when unlocking a P2PKH input.
|
|
9
|
+
*/
|
|
5
10
|
export default class SignatureTemplate {
|
|
11
|
+
/**
|
|
12
|
+
* Create a new SignatureTemplate.
|
|
13
|
+
*
|
|
14
|
+
* @param signer - A 32-byte private key (Uint8Array), a WIF or hex-encoded private key string,
|
|
15
|
+
* or any object exposing a `toWIF()` method (e.g. bitcore-lib or bitcoincashjs `ECPair`).
|
|
16
|
+
* @param hashtype - Sighash flags to use when signing. Defaults to `SIGHASH_ALL | SIGHASH_UTXOS`.
|
|
17
|
+
* @param signatureAlgorithm - The signature algorithm to use. Defaults to
|
|
18
|
+
* `SignatureAlgorithm.SCHNORR`.
|
|
19
|
+
*/
|
|
6
20
|
constructor(signer, hashtype = HashType.SIGHASH_ALL | HashType.SIGHASH_UTXOS, signatureAlgorithm = SignatureAlgorithm.SCHNORR) {
|
|
7
21
|
this.hashtype = hashtype;
|
|
8
22
|
this.signatureAlgorithm = signatureAlgorithm;
|
|
@@ -23,25 +37,60 @@ export default class SignatureTemplate {
|
|
|
23
37
|
this.privateKey = signer;
|
|
24
38
|
}
|
|
25
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Sign the provided sighash payload and return the signature concatenated with the hashtype
|
|
42
|
+
* byte, ready to be used as a transaction signature.
|
|
43
|
+
*
|
|
44
|
+
* @param payload - The 32-byte sighash to sign.
|
|
45
|
+
* @param bchForkId - Whether to include the BCH fork id flag in the appended hashtype byte.
|
|
46
|
+
* Defaults to `true`.
|
|
47
|
+
* @returns The signature bytes followed by the hashtype byte.
|
|
48
|
+
*/
|
|
26
49
|
generateSignature(payload, bchForkId) {
|
|
27
50
|
const signature = this.signMessageHash(payload);
|
|
28
51
|
return Uint8Array.from([...signature, this.getHashType(bchForkId)]);
|
|
29
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Sign a raw 32-byte message hash using the template's private key and signature algorithm.
|
|
55
|
+
*
|
|
56
|
+
* @param payload - The 32-byte hash to sign.
|
|
57
|
+
* @returns The raw signature bytes (without an appended hashtype byte).
|
|
58
|
+
*/
|
|
30
59
|
signMessageHash(payload) {
|
|
31
60
|
const signature = this.signatureAlgorithm === SignatureAlgorithm.SCHNORR
|
|
32
61
|
? secp256k1.signMessageHashSchnorr(this.privateKey, payload)
|
|
33
62
|
: secp256k1.signMessageHashDER(this.privateKey, payload);
|
|
34
63
|
return signature;
|
|
35
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Get the sighash flags used by this template.
|
|
67
|
+
*
|
|
68
|
+
* @param bchForkId - Whether to OR in the BCH fork id flag. Defaults to `true`.
|
|
69
|
+
* @returns The combined hashtype byte.
|
|
70
|
+
*/
|
|
36
71
|
getHashType(bchForkId = true) {
|
|
37
72
|
return bchForkId ? (this.hashtype | SigningSerializationFlag.forkId) : this.hashtype;
|
|
38
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* @returns The signature algorithm (ECDSA or Schnorr) used by this template.
|
|
76
|
+
*/
|
|
39
77
|
getSignatureAlgorithm() {
|
|
40
78
|
return this.signatureAlgorithm;
|
|
41
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Derive the compressed public key that corresponds to the template's private key.
|
|
82
|
+
*
|
|
83
|
+
* @returns The 33-byte compressed public key.
|
|
84
|
+
*/
|
|
42
85
|
getPublicKey() {
|
|
43
86
|
return secp256k1.derivePublicKeyCompressed(this.privateKey);
|
|
44
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* Build a P2PKH `Unlocker` for the address derived from this template's private key. The
|
|
90
|
+
* returned unlocker can be passed directly to `TransactionBuilder.addInput`.
|
|
91
|
+
*
|
|
92
|
+
* @returns An unlocker that signs the corresponding P2PKH UTXO.
|
|
93
|
+
*/
|
|
45
94
|
unlockP2PKH() {
|
|
46
95
|
const publicKey = this.getPublicKey();
|
|
47
96
|
const prevOutScript = publicKeyToP2PKHLockingBytecode(publicKey);
|
|
@@ -1,39 +1,198 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Unlocker, Output, TransactionDetails, UnlockableUtxo, Utxo, InputOptions, VmResourceUsage } from './interfaces.js';
|
|
1
|
+
import { WalletTemplate } from '@bitauth/libauth';
|
|
2
|
+
import { Unlocker, Output, TransactionDetails, UnlockableUtxo, Utxo, InputOptions, VmResourceUsage, BchChangeOutputOptions } from './interfaces.js';
|
|
3
3
|
import { NetworkProvider } from './network/index.js';
|
|
4
4
|
import { DebugResults } from './debugging.js';
|
|
5
5
|
import { WcTransactionOptions } from './walletconnect-utils.js';
|
|
6
6
|
import { WcTransactionObject } from './walletconnect-utils.js';
|
|
7
|
+
/**
|
|
8
|
+
* Options accepted by the `TransactionBuilder` constructor.
|
|
9
|
+
*/
|
|
7
10
|
export interface TransactionBuilderOptions {
|
|
11
|
+
/** Network provider used to broadcast the transaction and fetch details after sending. */
|
|
8
12
|
provider: NetworkProvider;
|
|
13
|
+
/** Optional absolute cap on the transaction fee. Build/send throws if the fee exceeds this value. */
|
|
9
14
|
maximumFeeSatoshis?: bigint;
|
|
15
|
+
/** Optional cap on the transaction fee rate in sats/byte. Build/send throws if exceeded. */
|
|
10
16
|
maximumFeeSatsPerByte?: number;
|
|
17
|
+
/** Allow fungible token inputs to exceed token outputs (implicit burn). Defaults to `false`. */
|
|
11
18
|
allowImplicitFungibleTokenBurn?: boolean;
|
|
12
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Fluent builder for constructing, debugging, and broadcasting CashScript transactions.
|
|
22
|
+
*
|
|
23
|
+
* Inputs are added via `addInput` / `addInputs` with an `Unlocker` produced by a `Contract` or
|
|
24
|
+
* `SignatureTemplate`, outputs are added via `addOutput` / `addOutputs`, and the resulting
|
|
25
|
+
* transaction can be built (`build`), debugged (`debug`), or broadcast (`send`).
|
|
26
|
+
*/
|
|
13
27
|
export declare class TransactionBuilder {
|
|
14
28
|
provider: NetworkProvider;
|
|
15
29
|
inputs: UnlockableUtxo[];
|
|
16
30
|
outputs: Output[];
|
|
17
31
|
locktime: number;
|
|
18
32
|
options: TransactionBuilderOptions;
|
|
33
|
+
/**
|
|
34
|
+
* Create a new TransactionBuilder.
|
|
35
|
+
*
|
|
36
|
+
* @param options - Builder options, including the network provider and optional fee/burn limits.
|
|
37
|
+
*/
|
|
19
38
|
constructor(options: TransactionBuilderOptions);
|
|
39
|
+
/**
|
|
40
|
+
* Add a single UTXO as an input to the transaction.
|
|
41
|
+
*
|
|
42
|
+
* @param utxo - The UTXO to spend.
|
|
43
|
+
* @param unlocker - The unlocker to generate the unlocking bytecode for this input. Typically
|
|
44
|
+
* obtained from `contract.unlock.<functionName>(...args)` or `signatureTemplate.unlockP2PKH()`.
|
|
45
|
+
* @param options - Optional per-input options such as a custom sequence number.
|
|
46
|
+
* @returns This builder for chaining.
|
|
47
|
+
* @throws If the UTXO is invalid.
|
|
48
|
+
*/
|
|
20
49
|
addInput(utxo: Utxo, unlocker: Unlocker, options?: InputOptions): this;
|
|
50
|
+
/**
|
|
51
|
+
* Add multiple UTXOs to the transaction that all share the same unlocker.
|
|
52
|
+
*
|
|
53
|
+
* @param utxos - The UTXOs to spend.
|
|
54
|
+
* @param unlocker - The shared unlocker used to unlock all provided UTXOs.
|
|
55
|
+
* @param options - Optional per-input options applied to every added input.
|
|
56
|
+
* @returns This builder for chaining.
|
|
57
|
+
* @throws If any UTXO is invalid.
|
|
58
|
+
*/
|
|
21
59
|
addInputs(utxos: Utxo[], unlocker: Unlocker, options?: InputOptions): this;
|
|
60
|
+
/**
|
|
61
|
+
* Add multiple UTXOs that each carry their own unlocker.
|
|
62
|
+
*
|
|
63
|
+
* @param utxos - UTXOs that have already been paired with their individual unlockers.
|
|
64
|
+
* @returns This builder for chaining.
|
|
65
|
+
* @throws If any UTXO is missing its unlocker.
|
|
66
|
+
*/
|
|
22
67
|
addInputs(utxos: UnlockableUtxo[]): this;
|
|
68
|
+
/**
|
|
69
|
+
* Add a single output to the transaction.
|
|
70
|
+
*
|
|
71
|
+
* @param output - The output to add. Its address, amount and optional token are validated
|
|
72
|
+
* against the provider's network.
|
|
73
|
+
* @returns This builder for chaining.
|
|
74
|
+
* @throws If the output is invalid.
|
|
75
|
+
*/
|
|
23
76
|
addOutput(output: Output): this;
|
|
77
|
+
/**
|
|
78
|
+
* Add multiple outputs to the transaction.
|
|
79
|
+
*
|
|
80
|
+
* @param outputs - The outputs to add. Each output is validated against the provider's network.
|
|
81
|
+
* @returns This builder for chaining.
|
|
82
|
+
* @throws If any output is invalid.
|
|
83
|
+
*/
|
|
24
84
|
addOutputs(outputs: Output[]): this;
|
|
85
|
+
/**
|
|
86
|
+
* Append an `OP_RETURN` output containing the provided data chunks. Hex strings prefixed with
|
|
87
|
+
* `0x` are decoded as bytes; other strings are encoded as UTF-8.
|
|
88
|
+
*
|
|
89
|
+
* @param chunks - The data chunks to include after the `OP_RETURN` opcode.
|
|
90
|
+
* @returns This builder for chaining.
|
|
91
|
+
*/
|
|
25
92
|
addOpReturnOutput(chunks: string[]): this;
|
|
93
|
+
/**
|
|
94
|
+
* Add a BCH change output for the remaining value after fees, if it exceeds the dust limit. The
|
|
95
|
+
* fee is computed from the transaction size at the configured fee rate; dust-sized change is
|
|
96
|
+
* simply absorbed into the fee.
|
|
97
|
+
*
|
|
98
|
+
* @param changeOutputOptions - The destination address and the fee rate (in sats/byte) to use.
|
|
99
|
+
* @returns This builder for chaining.
|
|
100
|
+
* @throws If the available surplus is insufficient to cover the fee for the configured rate.
|
|
101
|
+
*/
|
|
102
|
+
addBchChangeOutputIfNeeded(changeOutputOptions: BchChangeOutputOptions): this;
|
|
103
|
+
/**
|
|
104
|
+
* Build the transaction (skipping fee and burn checks) and return its encoded byte length.
|
|
105
|
+
*
|
|
106
|
+
* @returns The size of the transaction in bytes.
|
|
107
|
+
*/
|
|
108
|
+
getTransactionSize(): bigint;
|
|
109
|
+
/**
|
|
110
|
+
* Set the `nLockTime` of the transaction.
|
|
111
|
+
*
|
|
112
|
+
* @param locktime - The absolute locktime to use (block height or UNIX timestamp).
|
|
113
|
+
* @returns This builder for chaining.
|
|
114
|
+
*/
|
|
26
115
|
setLocktime(locktime: number): this;
|
|
27
116
|
private checkMaxFee;
|
|
28
117
|
private checkFungibleTokenBurn;
|
|
29
|
-
buildLibauthTransaction
|
|
118
|
+
private buildLibauthTransaction;
|
|
119
|
+
/**
|
|
120
|
+
* Build the transaction, applying fee and implicit-burn checks, and return the hex-encoded
|
|
121
|
+
* transaction bytes.
|
|
122
|
+
*
|
|
123
|
+
* @returns The signed transaction as a hex string.
|
|
124
|
+
* @throws If the transaction fee exceeds the configured maximum, or if fungible tokens are
|
|
125
|
+
* implicitly burned without `allowImplicitFungibleTokenBurn` enabled.
|
|
126
|
+
*/
|
|
30
127
|
build(): string;
|
|
128
|
+
/**
|
|
129
|
+
* Locally evaluate the transaction against the Bitcoin Cash VM using debug information from the
|
|
130
|
+
* contract artifacts. Throws a descriptive error if any input fails evaluation.
|
|
131
|
+
*
|
|
132
|
+
* @returns The full debug execution trace for every scenario in the generated libauth template.
|
|
133
|
+
* @throws If the transaction contains inputs with custom (non-standard) unlockers, or if the
|
|
134
|
+
* evaluation fails (e.g. a failing `require` statement).
|
|
135
|
+
*/
|
|
31
136
|
debug(): DebugResults;
|
|
137
|
+
/**
|
|
138
|
+
* Compute VM resource usage (ops, op cost budget, sigchecks, hash iterations) for each input
|
|
139
|
+
* by running the transaction through `debug`.
|
|
140
|
+
*
|
|
141
|
+
* @param verbose - When `true`, also prints a formatted table of the results via `console.table`.
|
|
142
|
+
* @returns One entry per input with its VM resource usage metrics.
|
|
143
|
+
* @throws If the transaction contains inputs with custom (non-standard) unlockers, or if the
|
|
144
|
+
* evaluation fails.
|
|
145
|
+
*/
|
|
32
146
|
getVmResourceUsage(verbose?: boolean): Array<VmResourceUsage>;
|
|
147
|
+
/**
|
|
148
|
+
* Build a Bitauth IDE URI that loads the transaction (and all private keys required to sign it)
|
|
149
|
+
* in the online Bitauth IDE debugger.
|
|
150
|
+
*
|
|
151
|
+
* WARNING: The URI embeds every private key used in the transaction. Do not share this URI if
|
|
152
|
+
* the transaction is signed with real private keys.
|
|
153
|
+
*
|
|
154
|
+
* @returns A Bitauth IDE URL for debugging this transaction.
|
|
155
|
+
* @throws If the transaction cannot be built (fee exceeds limit or fungible tokens burned).
|
|
156
|
+
*/
|
|
33
157
|
getBitauthUri(): string;
|
|
158
|
+
/**
|
|
159
|
+
* Build the transaction and return the corresponding libauth `WalletTemplate`. Useful for
|
|
160
|
+
* exporting the transaction to external libauth-compatible tooling.
|
|
161
|
+
*
|
|
162
|
+
* @returns A libauth `WalletTemplate` describing this transaction.
|
|
163
|
+
* @throws If the transaction cannot be built (fee exceeds limit or fungible tokens burned).
|
|
164
|
+
*/
|
|
34
165
|
getLibauthTemplate(): WalletTemplate;
|
|
166
|
+
/**
|
|
167
|
+
* Build and broadcast the transaction via the configured network provider. Before broadcasting,
|
|
168
|
+
* the transaction is evaluated locally (when all inputs use standard unlockers) so that failing
|
|
169
|
+
* require statements or VM errors surface with descriptive messages.
|
|
170
|
+
*
|
|
171
|
+
* @returns The decoded transaction details (including txid and raw hex).
|
|
172
|
+
* @throws A `FailedTransactionError` if the network rejects the transaction, or any of the
|
|
173
|
+
* build / local evaluation errors (e.g. fee cap, implicit burn, failing require statement).
|
|
174
|
+
*/
|
|
35
175
|
send(): Promise<TransactionDetails>;
|
|
176
|
+
/**
|
|
177
|
+
* Build and broadcast the transaction, returning only the raw transaction hex string as retrieved
|
|
178
|
+
* from the network after broadcast.
|
|
179
|
+
*
|
|
180
|
+
* @param raw - Pass `true` to receive the raw transaction hex instead of decoded details.
|
|
181
|
+
* @returns The raw transaction hex as retrieved from the network after broadcast.
|
|
182
|
+
* @throws A `FailedTransactionError` if the network rejects the transaction, or any of the
|
|
183
|
+
* build / local evaluation errors (e.g. fee cap, implicit burn, failing require statement).
|
|
184
|
+
*/
|
|
36
185
|
send(raw: true): Promise<string>;
|
|
37
186
|
private getTxDetails;
|
|
187
|
+
/**
|
|
188
|
+
* Build the transaction and format it as a BCH WalletConnect transaction object suitable for
|
|
189
|
+
* signing and broadcasting via a BCH WalletConnect-compatible Bitcoin Cash wallet.
|
|
190
|
+
*
|
|
191
|
+
* See the [BCH WalletConnect spec](https://github.com/mainnet-pat/wc2-bch-bcr) for the object format.
|
|
192
|
+
*
|
|
193
|
+
* @param options - Optional WalletConnect options such as `broadcast` and `userPrompt`.
|
|
194
|
+
* @returns A WalletConnect transaction object ready to be sent to a WalletConnect wallet.
|
|
195
|
+
* @throws If the transaction cannot be built (fee exceeds limit or fungible tokens burned).
|
|
196
|
+
*/
|
|
38
197
|
generateWcTransactionObject(options?: WcTransactionOptions): WcTransactionObject;
|
|
39
198
|
}
|