# README
txscript
Package txscript implements the kaspa transaction script language. There is a comprehensive test suite.
Kaspa Scripts
Kaspa provides a stack-based, FORTH-like language for the scripts in the kaspa transactions. This language is not turing complete although it is still fairly powerful.
Examples
-
Standard Pay-to-pubkey Script
Demonstrates creating a script which pays to a kaspa address. It also prints the created script hex and uses the DisasmString function to display the disassembled script. -
Extracting Details from Standard Scripts
Demonstrates extracting information from a standard public key script.
# Functions
CalcScriptInfo returns a structure providing data about the provided script pair.
DisasmString formats a disassembled script for one line printing.
ExtractAtomicSwapDataPushes returns the data pushes from an atomic swap contract.
ExtractScriptPubKeyAddress returns the type of script and its addresses.
GetPreciseSigOpCount returns the number of signature operations in scriptPubKey.
GetScriptClass returns the class of the script passed.
GetSigOpCount provides a quick count of the number of signature operations in a script.
IsErrorCode returns whether or not the provided error is a script error with the provided error code.
IsPayToScriptHash returns true if the script is in the standard pay-to-script-hash (P2SH) format, false otherwise.
IsUnspendable returns whether the passed public key script is unspendable, or guaranteed to fail at execution.
NewEngine returns a new script engine for the provided public key script, transaction, and input index.
NewScriptBuilder returns a new instance of a script builder.
NewSigCache creates and initializes a new instance of SigCache.
NewSigCacheECDSA creates and initializes a new instance of SigCache.
PayToAddrScript creates a new script to pay a transaction output to a the specified address.
PayToScriptHashScript takes a script and returns an equivalent pay-to-script-hash script.
PayToScriptHashSignatureScript generates a signature script that fits a pay-to-script-hash script.
PushedData returns an array of byte slices containing any pushed data found in the passed script.
RawTxInSignature returns the serialized Schnorr signature for the input idx of the given transaction, with hashType appended to it.
RawTxInSignatureECDSA returns the serialized ECDSA signature for the input idx of the given transaction, with hashType appended to it.
SignatureScript creates an input signature script for tx to spend KAS sent from a previous output to the owner of a Schnorr private key.
SignatureScriptECDSA creates an input signature script for tx to spend KAS sent from a previous output to the owner of an ECDSA private key.
SignTxOutput signs output idx of the given tx to resolve the script given in scriptPublicKey with a signature type of hashType.
# Constants
ErrCheckSigVerify is returned when OP_CHECKMULTISIGVERIFY is encountered in a script and the top item on the data stack does not evaluate to true.
ErrCheckSigVerify is returned when OP_CHECKSIGVERIFY is encountered in a script and the top item on the data stack does not evaluate to true.
ErrCleanStack is returned when after evaluation, the stack contains more than one element.
ErrDisabledOpcode is returned when a disabled opcode is encountered in a script.
ErrEarlyReturn is returned when OP_RETURN is executed in the script.
ErrElementTooBig is returned if the size of an element to be pushed to the stack is over MaxScriptElementSize.
ErrEmptyStack is returned when the script evaluated without error, but terminated with an empty top stack element.
ErrEqualVerify is returned when OP_EQUALVERIFY is encountered in a script and the top item on the data stack does not evaluate to true.
ErrEvalFalse is returned when the script evaluated without error but terminated with a false top stack element.
ErrInternal is returned if internal consistency checks fail.
ErrInvalidFlags is returned when the passed flags to NewEngine contain an invalid combination.
ErrInvalidIndex is returned when an out-of-bounds index is passed to a function.
ErrScriptDone is returned when an attempt to execute an opcode is made once all of them have already been executed.
ErrInvalidPubKeyCount is returned when the number of public keys specified for a multsig is either negative or greater than MaxPubKeysPerMultiSig.
ErrInvalidSigHashType is returned when a signature hash type is not one of the supported types.
ErrInvalidSignatureCount is returned when the number of signatures specified for a multisig is either negative or greater than the number of public keys.
ErrInvalidStackOperation is returned when a stack operation is attempted with a number that is invalid for the current stack size.
ErrMalformedPush is returned when a data push opcode tries to push more bytes than are left in the script.
ErrMinimalData is returned when the script contains push operations that do not use the minimal opcode required.
ErrMinimalIf is returned if the operand of an OP_IF/OP_NOTIF is not either an empty vector or [0x01].
ErrNegativeLockTime is returned when a script contains an opcode that interprets a negative lock time.
ErrNotMultisigScript is returned from CalcMultiSigStats when the provided script is not a multisig script.
ErrNotPushOnly is returned when a script that is required to only push data to the stack performs other operations.
ErrNullFail is returned when signatures are not empty on failed checksig or checkmultisig operations.
ErrNumberTooBig is returned when the argument for an opcode that expects numeric input is larger than the expected maximum number of bytes.
ErrNumEqualVerify is returned when OP_NUMEQUALVERIFY is encountered in a script and the top item on the data stack does not evaluate to true.
ErrPubKeyFormat is returned when the script contains invalid public keys.
ErrReservedOpcode is returned when an opcode marked as reserved is encountered in a script.
ErrScriptTooBig is returned if a script is larger than MaxScriptSize.
ErrScriptUnfinished is returned when CheckErrorCondition is called on a script that has not finished executing.
ErrSigHighS is returned when the ScriptVerifyLowS flag is set and the script contains any signatures whose S values are higher than the half order.
ErrSigLength is returned when Schnorr signature is of incorrect length.
ErrStackOverflow is returned when stack and altstack combined depth is over the limit.
ErrTooManyOperations is returned if a script has more than MaxOpsPerScript opcodes that do not push data.
ErrTooManyRequiredSigs is returned from MultiSigScript when the specified number of required signatures is larger than the number of provided public keys.
ErrUnbalancedConditional is returned when an OP_ELSE or OP_ENDIF is encountered in a script without first having an OP_IF or OP_NOTIF or the end of script is reached without encountering an OP_ENDIF when an OP_IF or OP_NOTIF was previously encountered.
ErrUnsatisfiedLockTime is returned when a script contains an opcode that involves a lock time and the required lock time has not been reached.
ErrUnsupportedAddress is returned when a concrete type that implements a util.Address is not a supported type.
ErrVerify is returned when OP_VERIFY is encountered in a script and the top item on the data stack does not evaluate to true.
Max number of non-push operations.
Multisig can't have more sigs than this.
Max bytes pushable to the stack.
MaxScriptSize is the maximum allowed length of a raw script.
MaxStackSize is the maximum combined height of stack and alt stack during execution.
None of the recognized forms.
0.
146.
81 - AKA OpTrue.
90.
91.
92.
93.
94.
95.
96.
139.
79.
140.
82.
142.
109.
110.
141.
112.
113.
114.
83.
111.
84.
85.
86.
87.
88.
89.
144.
147.
132.
170.
154.
155.
126.
176.
174.
169.
175.
177.
172.
171.
173.
Conditional execution constants.
Conditional execution constants.
Conditional execution constants.
1.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
2.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
3.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
4.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
5.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
6.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
7.
70.
71.
72.
73.
74.
75.
8.
9.
116.
150.
117.
118.
103.
104.
135.
136.
0 - AKA Op0.
108.
160.
162.
99.
115.
255.
131.
128.
159.
161.
152.
164.
163.
151.
149.
143.
119.
97.
145.
100.
156.
157.
158.
133.
120.
121.
254.
253.
251.
76.
77.
78.
80.
137.
138.
106.
129.
122.
123.
153.
168.
130.
250.
148.
127.
124.
107.
81.
125.
166.
167.
178.
179.
180.
181.
182.
183.
184.
185.
186.
187.
188.
189.
190.
191.
192.
193.
194.
195.
196.
197.
198.
199.
200.
201.
202.
203.
204.
205.
206.
207.
208.
209.
210.
211.
212.
213.
214.
215.
216.
217.
218.
219.
220.
221.
222.
223.
224.
225.
226.
227.
228.
229.
230.
231.
232.
233.
234.
235.
236.
237.
238.
239.
240.
241.
242.
243.
244.
245.
246.
247.
248.
249.
252.
98.
101.
105.
102.
165.
134.
Pay to pubkey ECDSA.
Pay to pubkey.
Pay to script hash.
ScriptNoFlags is used when you want to use ScriptFlags without raising any flags.
# Variables
OpcodeByName is a map that can be used to lookup an opcode by its human-readable name (OP_CHECKMULTISIG, OP_CHECKSIG, etc).
# Structs
AtomicSwapDataPushes houses the data pushes found in atomic swap contracts.
Engine is the virtual machine that executes scripts.
Error identifies a script-related error.
ScriptBuilder provides a facility for building custom scripts.
ScriptInfo houses information about a script pair that is determined by CalcScriptInfo.
SigCache implements an Schnorr signature verification cache with a randomized entry eviction policy.
SigCacheECDSA implements an ECDSA signature verification cache with a randomized entry eviction policy.
# Type aliases
ErrorCode identifies a kind of script error.
ErrScriptNotCanonical identifies a non-canonical script.
KeyClosure implements KeyDB with a closure.
ScriptClass is an enumeration for the list of standard types of script.
ScriptClosure implements ScriptDB with a closure.
ScriptFlags is a bitmask defining additional operations or tests that will be done when executing a script pair.