API
Links: API, Interfaces, Classes, Functions, Types, Enums, Variables
Interfaces
ScriptChunk |
ScriptTemplate |
ScriptTemplateUnlock |
Links: API, Interfaces, Classes, Functions, Types, Enums, Variables
Interface: ScriptChunk
A representation of a chunk of a script, which includes an opcode. For push operations, the associated data to push onto the stack is also included.
Links: API, Interfaces, Classes, Functions, Types, Enums, Variables
Interface: ScriptTemplate
export default interface ScriptTemplate {
lock: (...params: any[]) => LockingScript | Promise<LockingScript>;
unlock: (...params: any[]) => ScriptTemplateUnlock;
}
See also: LockingScript, ScriptTemplateUnlock
Property lock
Creates a locking script with the given parameters.
See also: LockingScriptProperty unlock
Creates a function that generates an unlocking script along with its signature and length estimation.
This method returns an object containing two functions: 1. sign
- A function that, when called with a transaction and an input index, returns an UnlockingScript instance. 2. estimateLength
- A function that returns the estimated length of the unlocking script in bytes.
Links: API, Interfaces, Classes, Functions, Types, Enums, Variables
Interface: ScriptTemplateUnlock
export default interface ScriptTemplateUnlock {
sign: (tx: Transaction, inputIndex: number) => Promise<UnlockingScript>;
estimateLength: (tx: Transaction, inputIndex: number) => Promise<number>;
}
See also: Transaction, UnlockingScript, sign
Links: API, Interfaces, Classes, Functions, Types, Enums, Variables
Classes
LockingScript |
P2PKH |
PushDrop |
RPuzzle |
Script |
ScriptEvaluationError |
Spend |
UnlockingScript |
Links: API, Interfaces, Classes, Functions, Types, Enums, Variables
Class: LockingScript
The LockingScript class represents a locking script in a Bitcoin SV transaction. It extends the Script class and is used specifically for output scripts that lock funds.
Inherits all properties and methods from the Script class.
export default class LockingScript extends Script {
isLockingScript(): boolean
isUnlockingScript(): boolean
}
See also: Script
Method isLockingScript
Returns
Always returns true for a LockingScript instance.
Method isUnlockingScript
Returns
Always returns false for a LockingScript instance.
Links: API, Interfaces, Classes, Functions, Types, Enums, Variables
Class: P2PKH
P2PKH (Pay To Public Key Hash) class implementing ScriptTemplate.
This class provides methods to create Pay To Public Key Hash locking and unlocking scripts, including the unlocking of P2PKH UTXOs with the private key.
export default class P2PKH implements ScriptTemplate {
lock(pubkeyhash: string | number[]): LockingScript
unlock(privateKey: PrivateKey, signOutputs: "all" | "none" | "single" = "all", anyoneCanPay: boolean = false, sourceSatoshis?: number, lockingScript?: Script): {
sign: (tx: Transaction, inputIndex: number) => Promise<UnlockingScript>;
estimateLength: () => Promise<108>;
}
}
See also: LockingScript, PrivateKey, Script, ScriptTemplate, Transaction, UnlockingScript, sign
Method lock
Creates a P2PKH locking script for a given public key hash or address string
See also: LockingScriptReturns
- A P2PKH locking script.
Argument Details
- pubkeyhash
- or address - An array or address representing the public key hash.
Method unlock
Creates a function that generates a P2PKH unlocking script along with its signature and length estimation.
The returned object contains: 1. sign
- A function that, when invoked with a transaction and an input index, produces an unlocking script suitable for a P2PKH locked output. 2. estimateLength
- A function that returns the estimated length of the unlocking script in bytes.
unlock(privateKey: PrivateKey, signOutputs: "all" | "none" | "single" = "all", anyoneCanPay: boolean = false, sourceSatoshis?: number, lockingScript?: Script): {
sign: (tx: Transaction, inputIndex: number) => Promise<UnlockingScript>;
estimateLength: () => Promise<108>;
}
Returns
- An object containing the
sign
andestimateLength
functions.
Argument Details
- privateKey
- The private key used for signing the transaction.
- signOutputs
- The signature scope for outputs.
- anyoneCanPay
- Flag indicating if the signature allows for other inputs to be added later.
- sourceSatoshis
- Optional. The amount being unlocked. Otherwise the input.sourceTransaction is required.
- lockingScript
- Optional. The lockinScript. Otherwise the input.sourceTransaction is required.
Links: API, Interfaces, Classes, Functions, Types, Enums, Variables
Class: PushDrop
export default class PushDrop implements ScriptTemplate {
wallet: WalletInterface;
originator?: string;
static decode(script: LockingScript): {
lockingPublicKey: PublicKey;
fields: number[][];
}
constructor(wallet: WalletInterface, originator?: string)
async lock(fields: number[][], protocolID: [
SecurityLevel,
string
], keyID: string, counterparty: string, forSelf = false, includeSignature = true, lockPosition: "before" | "after" = "before"): Promise<LockingScript>
unlock(protocolID: [
SecurityLevel,
string
], keyID: string, counterparty: string, signOutputs: "all" | "none" | "single" = "all", anyoneCanPay = false, sourceSatoshis?: number, lockingScript?: LockingScript): {
sign: (tx: Transaction, inputIndex: number) => Promise<UnlockingScript>;
estimateLength: () => Promise<73>;
}
}
See also: LockingScript, PublicKey, ScriptTemplate, SecurityLevel, Transaction, UnlockingScript, WalletInterface, sign
Constructor
Constructs a new instance of the PushDrop class.
See also: WalletInterfaceArgument Details
- wallet
- The wallet interface used for creating signatures and accessing public keys.
- originator
- — The originator to use with Wallet requests
Method decode
Decodes a PushDrop script back into its token fields and the locking public key. If a signature was present, it will be the last field returned. Warning: Only works with a P2PK lock at the beginning of the script.
See also: LockingScript, PublicKeyReturns
An object containing PushDrop token fields and the locking public key. If a signature was included, it will be the last field.
Argument Details
- script
- PushDrop script to decode back into token fields
Method lock
Creates a PushDrop locking script with arbitrary data fields and a public key lock.
async lock(fields: number[][], protocolID: [
SecurityLevel,
string
], keyID: string, counterparty: string, forSelf = false, includeSignature = true, lockPosition: "before" | "after" = "before"): Promise<LockingScript>
Returns
The generated PushDrop locking script.
Argument Details
- fields
- The token fields to include in the locking script.
- protocolID
- The protocol ID to use.
- keyID
- The key ID to use.
- counterparty
- The counterparty involved in the transaction, "self" or "anyone".
- forSelf
- Flag indicating if the lock is for the creator (default no).
- includeSignature
- Flag indicating if a signature should be included in the script (default yes).
Method unlock
Creates an unlocking script for spending a PushDrop token output.
unlock(protocolID: [
SecurityLevel,
string
], keyID: string, counterparty: string, signOutputs: "all" | "none" | "single" = "all", anyoneCanPay = false, sourceSatoshis?: number, lockingScript?: LockingScript): {
sign: (tx: Transaction, inputIndex: number) => Promise<UnlockingScript>;
estimateLength: () => Promise<73>;
}
Returns
An object containing functions to sign the transaction and estimate the script length.
Argument Details
- protocolID
- The protocol ID to use.
- keyID
- The key ID to use.
- counterparty
- The counterparty involved in the transaction, "self" or "anyone".
- sourceTXID
- The TXID of the source transaction.
- sourceSatoshis
- The number of satoshis in the source output.
- lockingScript
- The locking script of the source output.
- signOutputs
- Specifies which outputs to sign.
- anyoneCanPay
- Specifies if the anyone-can-pay flag is set.
Links: API, Interfaces, Classes, Functions, Types, Enums, Variables
Class: RPuzzle
RPuzzle class implementing ScriptTemplate.
This class provides methods to create R Puzzle and R Puzzle Hash locking and unlocking scripts, including the unlocking of UTXOs with the correct K value.
export default class RPuzzle implements ScriptTemplate {
type: "raw" | "SHA1" | "SHA256" | "HASH256" | "RIPEMD160" | "HASH160" = "raw";
constructor(type: "raw" | "SHA1" | "SHA256" | "HASH256" | "RIPEMD160" | "HASH160" = "raw")
lock(value: number[]): LockingScript
unlock(k: BigNumber, privateKey: PrivateKey, signOutputs: "all" | "none" | "single" = "all", anyoneCanPay: boolean = false): {
sign: (tx: Transaction, inputIndex: number) => Promise<UnlockingScript>;
estimateLength: () => Promise<108>;
}
}
See also: BigNumber, LockingScript, PrivateKey, ScriptTemplate, Transaction, UnlockingScript, sign
Constructor
Argument Details
- type
- Denotes the type of puzzle to create
Method lock
Creates an R puzzle locking script for a given R value or R value hash.
See also: LockingScriptReturns
- An R puzzle locking script.
Argument Details
- value
- An array representing the R value or its hash.
Method unlock
Creates a function that generates an R puzzle unlocking script along with its signature and length estimation.
The returned object contains: 1. sign
- A function that, when invoked with a transaction and an input index, produces an unlocking script suitable for an R puzzle locked output. 2. estimateLength
- A function that returns the estimated length of the unlocking script in bytes.
unlock(k: BigNumber, privateKey: PrivateKey, signOutputs: "all" | "none" | "single" = "all", anyoneCanPay: boolean = false): {
sign: (tx: Transaction, inputIndex: number) => Promise<UnlockingScript>;
estimateLength: () => Promise<108>;
}
Returns
- An object containing the
sign
andestimateLength
functions.
Argument Details
- k
- — The K-value used to unlock the R-puzzle.
- privateKey
- The private key used for signing the transaction. If not provided, a random key will be generated.
- signOutputs
- The signature scope for outputs.
- anyoneCanPay
- Flag indicating if the signature allows for other inputs to be added later.
Links: API, Interfaces, Classes, Functions, Types, Enums, Variables
Class: Script
The Script class represents a script in a Bitcoin SV transaction, encapsulating the functionality to construct, parse, and serialize scripts used in both locking (output) and unlocking (input) scripts.
export default class Script {
chunks: ScriptChunk[];
static fromASM(asm: string): Script
static fromHex(hex: string): Script
static fromBinary(bin: number[]): Script
constructor(chunks: ScriptChunk[] = [])
toASM(): string
toHex(): string
toBinary(): number[]
writeScript(script: Script): Script
writeOpCode(op: number): Script
setChunkOpCode(i: number, op: number): Script
writeBn(bn: BigNumber): Script
writeBin(bin: number[]): Script
writeNumber(num: number): Script
removeCodeseparators(): Script
findAndDelete(script: Script): Script
isPushOnly(): boolean
isLockingScript(): boolean
isUnlockingScript(): boolean
}
See also: BigNumber, ScriptChunk, toHex
Constructor
See also: ScriptChunkArgument Details
- chunks
- =[] - An array of script chunks to directly initialize the script.
Method findAndDelete
Deletes the given item wherever it appears in the current script.
See also: ScriptReturns
This script instance for chaining.
Argument Details
- script
- The script containing the item to delete from the current script.
Method fromASM
See also: ScriptReturns
A new Script instance.
Argument Details
- asm
- The script in ASM string format.
Example
Method fromBinary
See also: ScriptReturns
A new Script instance.
Argument Details
- bin
- The script in binary array format.
Example
Method fromHex
See also: ScriptReturns
A new Script instance.
Argument Details
- hex
- The script in hexadecimal format.
Example
Method isLockingScript
Returns
True if the script is a locking script, otherwise false.
Method isPushOnly
Returns
True if the script is push-only, otherwise false.
Method isUnlockingScript
Returns
True if the script is an unlocking script, otherwise false.
Method removeCodeseparators
See also: ScriptReturns
This script instance for chaining.
Method setChunkOpCode
See also: ScriptReturns
This script instance for chaining.
Argument Details
- i
- The index of the chunk.
- op
- The opcode to set.
Method toASM
Returns
The script in ASM string format.
Method toBinary
Returns
The script in binary array format.
Method toHex
Returns
The script in hexadecimal format.
Method writeBin
See also: ScriptReturns
This script instance for chaining.
Argument Details
- bin
- The binary data to append.
Throws
Throws an error if the data is too large to be pushed.
Method writeBn
See also: BigNumber, ScriptReturns
This script instance for chaining.
Argument Details
- bn
- The BigNumber to append.
Method writeNumber
See also: ScriptReturns
This script instance for chaining.
Argument Details
- num
- The number to append.
Method writeOpCode
See also: ScriptReturns
This script instance for chaining.
Argument Details
- op
- The opcode to append.
Method writeScript
See also: ScriptReturns
This script instance for chaining.
Argument Details
- script
- The script to append.
Links: API, Interfaces, Classes, Functions, Types, Enums, Variables
Class: ScriptEvaluationError
export default class ScriptEvaluationError extends Error {
txid: string;
outputIndex: number;
context: "UnlockingScript" | "LockingScript";
programCounter: number;
stackState: number[][];
altStackState: number[][];
ifStackState: boolean[];
stackMem: number;
altStackMem: number;
constructor(params: {
message: string;
txid: string;
outputIndex: number;
context: "UnlockingScript" | "LockingScript";
programCounter: number;
stackState: number[][];
altStackState: number[][];
ifStackState: boolean[];
stackMem: number;
altStackMem: number;
})
}
Links: API, Interfaces, Classes, Functions, Types, Enums, Variables
Class: Spend
The Spend class represents a spend action within a Bitcoin SV transaction. It encapsulates all the necessary data required for spending a UTXO (Unspent Transaction Output) and includes details about the source transaction, output, and the spending transaction itself.
export default class Spend {
sourceTXID: string;
sourceOutputIndex: number;
sourceSatoshis: number;
lockingScript: LockingScript;
transactionVersion: number;
otherInputs: TransactionInput[];
outputs: TransactionOutput[];
inputIndex: number;
unlockingScript: UnlockingScript;
inputSequence: number;
lockTime: number;
context: "UnlockingScript" | "LockingScript";
programCounter: number;
lastCodeSeparator: number | null;
stack: number[][];
altStack: number[][];
ifStack: boolean[];
memoryLimit: number;
stackMem: number;
altStackMem: number;
constructor(params: {
sourceTXID: string;
sourceOutputIndex: number;
sourceSatoshis: number;
lockingScript: LockingScript;
transactionVersion: number;
otherInputs: TransactionInput[];
outputs: TransactionOutput[];
unlockingScript: UnlockingScript;
inputSequence: number;
inputIndex: number;
lockTime: number;
memoryLimit?: number;
})
reset(): void
step(): boolean
validate(): boolean
}
See also: LockingScript, TransactionInput, TransactionOutput, UnlockingScript
Constructor
constructor(params: {
sourceTXID: string;
sourceOutputIndex: number;
sourceSatoshis: number;
lockingScript: LockingScript;
transactionVersion: number;
otherInputs: TransactionInput[];
outputs: TransactionOutput[];
unlockingScript: UnlockingScript;
inputSequence: number;
inputIndex: number;
lockTime: number;
memoryLimit?: number;
})
Argument Details
- params.sourceTXID
- The transaction ID of the source UTXO.
- params.sourceOutputIndex
- The index of the output in the source transaction.
- params.sourceSatoshis
- The amount of satoshis in the source UTXO.
- params.lockingScript
- The locking script associated with the UTXO.
- params.transactionVersion
- The version of the current transaction.
- params.otherInputs
- - An array of other inputs in the transaction.
- params.outputs
- - The outputs of the current transaction.
- params.inputIndex
- The index of this input in the current transaction.
- params.unlockingScript
- The unlocking script for this spend.
- params.inputSequence
- The sequence number of this input.
- params.lockTime
- The lock time of the transaction.
Example
const spend = new Spend({
sourceTXID: "abcd1234", // sourceTXID
sourceOutputIndex: 0, // sourceOutputIndex
sourceSatoshis: new BigNumber(1000), // sourceSatoshis
lockingScript: LockingScript.fromASM("OP_DUP OP_HASH160 abcd1234... OP_EQUALVERIFY OP_CHECKSIG"),
transactionVersion: 1, // transactionVersion
otherInputs: [{ sourceTXID: "abcd1234", sourceOutputIndex: 1, sequence: 0xffffffff }], // otherInputs
outputs: [{ satoshis: new BigNumber(500), lockingScript: LockingScript.fromASM("OP_DUP...") }], // outputs
inputIndex: 0, // inputIndex
unlockingScript: UnlockingScript.fromASM("3045... 02ab..."),
inputSequence: 0xffffffff // inputSequence
memoryLimit: 100000 // memoryLimit
});
Method validate
Returns
Returns true if the scripts are valid and the spend is legitimate, otherwise false.
Example
Links: API, Interfaces, Classes, Functions, Types, Enums, Variables
Class: UnlockingScript
The UnlockingScript class represents an unlocking script in a Bitcoin SV transaction. It extends the Script class and is used specifically for input scripts that unlock funds.
Inherits all properties and methods from the Script class.
export default class UnlockingScript extends Script {
isLockingScript(): boolean
isUnlockingScript(): boolean
}
See also: Script
Method isLockingScript
Returns
Always returns false for an UnlockingScript instance.
Method isUnlockingScript
Returns
Always returns true for an UnlockingScript instance.
Links: API, Interfaces, Classes, Functions, Types, Enums, Variables