@bsv/sdk 1.9.3 → 1.9.4

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.
Files changed (60) hide show
  1. package/dist/cjs/package.json +1 -1
  2. package/docs/fast-docs.png +0 -0
  3. package/docs/index.md +49 -44
  4. package/docs/swagger.png +0 -0
  5. package/package.json +1 -1
  6. package/docs/MARKDOWN_VALIDATION_GUIDE.md +0 -175
  7. package/docs/concepts/beef.md +0 -92
  8. package/docs/concepts/chain-tracking.md +0 -134
  9. package/docs/concepts/decentralized-identity.md +0 -221
  10. package/docs/concepts/fees.md +0 -249
  11. package/docs/concepts/identity-certificates.md +0 -307
  12. package/docs/concepts/index.md +0 -77
  13. package/docs/concepts/key-management.md +0 -185
  14. package/docs/concepts/script-templates.md +0 -176
  15. package/docs/concepts/sdk-philosophy.md +0 -80
  16. package/docs/concepts/signatures.md +0 -194
  17. package/docs/concepts/spv-verification.md +0 -118
  18. package/docs/concepts/transaction-encoding.md +0 -167
  19. package/docs/concepts/transaction-structure.md +0 -67
  20. package/docs/concepts/trust-model.md +0 -139
  21. package/docs/concepts/verification.md +0 -250
  22. package/docs/concepts/wallet-integration.md +0 -101
  23. package/docs/guides/development-wallet-setup.md +0 -374
  24. package/docs/guides/direct-transaction-creation.md +0 -147
  25. package/docs/guides/http-client-configuration.md +0 -488
  26. package/docs/guides/index.md +0 -138
  27. package/docs/guides/large-transactions.md +0 -448
  28. package/docs/guides/multisig-transactions.md +0 -792
  29. package/docs/guides/security-best-practices.md +0 -494
  30. package/docs/guides/transaction-batching.md +0 -132
  31. package/docs/guides/transaction-signing-methods.md +0 -419
  32. package/docs/reference/arc-config.md +0 -698
  33. package/docs/reference/brc-100.md +0 -33
  34. package/docs/reference/configuration.md +0 -835
  35. package/docs/reference/debugging.md +0 -705
  36. package/docs/reference/errors.md +0 -597
  37. package/docs/reference/index.md +0 -111
  38. package/docs/reference/network-config.md +0 -914
  39. package/docs/reference/op-codes.md +0 -325
  40. package/docs/reference/transaction-signatures.md +0 -95
  41. package/docs/tutorials/advanced-transaction.md +0 -572
  42. package/docs/tutorials/aes-encryption.md +0 -949
  43. package/docs/tutorials/authfetch-tutorial.md +0 -986
  44. package/docs/tutorials/ecdh-key-exchange.md +0 -549
  45. package/docs/tutorials/elliptic-curve-fundamentals.md +0 -606
  46. package/docs/tutorials/error-handling.md +0 -1216
  47. package/docs/tutorials/first-transaction-low-level.md +0 -205
  48. package/docs/tutorials/first-transaction.md +0 -275
  49. package/docs/tutorials/hashes-and-hmacs.md +0 -788
  50. package/docs/tutorials/identity-management.md +0 -729
  51. package/docs/tutorials/index.md +0 -219
  52. package/docs/tutorials/key-management.md +0 -538
  53. package/docs/tutorials/protowallet-development.md +0 -743
  54. package/docs/tutorials/script-construction.md +0 -690
  55. package/docs/tutorials/spv-merkle-proofs.md +0 -685
  56. package/docs/tutorials/testnet-transactions-low-level.md +0 -359
  57. package/docs/tutorials/transaction-broadcasting.md +0 -538
  58. package/docs/tutorials/transaction-types.md +0 -420
  59. package/docs/tutorials/type-42.md +0 -568
  60. package/docs/tutorials/uhrp-storage.md +0 -599
@@ -1,325 +0,0 @@
1
- # OP Codes
2
-
3
- *Understanding Bitcoin script opcodes and their usage in the BSV TypeScript SDK.*
4
-
5
- ## What are OP Codes?
6
-
7
- OP codes (operation codes) are the fundamental building blocks of Bitcoin's scripting language. They are operational instructions that manipulate data on the script execution stack to define the conditions under which Bitcoin can be spent. Each opcode performs a specific function such as arithmetic operations, logical comparisons, cryptographic operations, or stack manipulation.
8
-
9
- ## Script Execution Environment
10
-
11
- Bitcoin scripts operate in a **stack-based execution environment** with two primary data structures:
12
-
13
- - **Main Stack**: Where most operations are performed
14
- - **Alt Stack**: Provides additional stack flexibility for complex operations
15
-
16
- Scripts consist of two parts:
17
-
18
- 1. **Unlocking Script**: Provided by the spender, supplies data to satisfy locking conditions
19
- 2. **Locking Script**: Defines the conditions that must be met to spend the output
20
-
21
- The execution begins with the unlocking script placing data on the stack, followed by the locking script operating on the same stack. A spend is valid if the top of the stack contains a "true" value (non-zero) after execution.
22
-
23
- ## SDK Integration
24
-
25
- The BSV TypeScript SDK provides comprehensive opcode support through the `OP` object and script execution engine:
26
-
27
- ```typescript
28
- import { OP, Script, LockingScript, UnlockingScript } from '@bsv/sdk'
29
-
30
- // Access opcodes by name
31
- console.log(OP.OP_DUP) // 0x76
32
- console.log(OP.OP_HASH160) // 0xa9
33
- console.log(OP.OP_CHECKSIG) // 0xac
34
-
35
- // Create scripts using opcodes
36
- const lockingScript = new LockingScript([
37
- { op: OP.OP_DUP },
38
- { op: OP.OP_HASH160 },
39
- { op: 20, data: publicKeyHash }, // Push 20 bytes
40
- { op: OP.OP_EQUALVERIFY },
41
- { op: OP.OP_CHECKSIG }
42
- ])
43
-
44
- // Create from ASM (Assembly) format
45
- const script = Script.fromASM('OP_DUP OP_HASH160 ' + publicKeyHash + ' OP_EQUALVERIFY OP_CHECKSIG')
46
- ```
47
-
48
- ## Opcode Categories
49
-
50
- ### Push Operations
51
-
52
- Push data onto the stack:
53
-
54
- ```typescript
55
- // Push small numbers (0-16)
56
- OP.OP_0 // Push empty array (false)
57
- OP.OP_1 // Push number 1 (true)
58
- OP.OP_2 // Push number 2
59
- // ... up to OP_16
60
-
61
- // Push data of various sizes
62
- OP.OP_PUSHDATA1 // Push up to 255 bytes
63
- OP.OP_PUSHDATA2 // Push up to 65,535 bytes
64
- OP.OP_PUSHDATA4 // Push up to 4,294,967,295 bytes
65
- ```
66
-
67
- ### Stack Operations
68
-
69
- Manipulate stack contents:
70
-
71
- ```typescript
72
- OP.OP_DUP // Duplicate top stack item
73
- OP.OP_DROP // Remove top stack item
74
- OP.OP_SWAP // Swap top two stack items
75
- OP.OP_ROT // Rotate top three stack items
76
- OP.OP_2DUP // Duplicate top two stack items
77
- OP.OP_TOALTSTACK // Move item to alt stack
78
- ```
79
-
80
- ### Arithmetic Operations
81
-
82
- Perform mathematical operations:
83
-
84
- ```typescript
85
- OP.OP_ADD // Add top two stack items
86
- OP.OP_SUB // Subtract second from top
87
- OP.OP_MUL // Multiply top two items
88
- OP.OP_DIV // Divide second by top
89
- OP.OP_MOD // Modulo operation
90
- OP.OP_MIN // Return minimum of two values
91
- OP.OP_MAX // Return maximum of two values
92
- ```
93
-
94
- ### Comparison Operations
95
-
96
- Compare values and return boolean results:
97
-
98
- ```typescript
99
- OP.OP_EQUAL // Check if top two items are equal
100
- OP.OP_EQUALVERIFY // Equal check + verify (fail if false)
101
- OP.OP_NUMEQUAL // Numeric equality check
102
- OP.OP_LESSTHAN // Check if second < top
103
- OP.OP_GREATERTHAN // Check if second > top
104
- OP.OP_WITHIN // Check if value is within range
105
- ```
106
-
107
- ### Cryptographic Operations
108
-
109
- Perform cryptographic functions:
110
-
111
- ```typescript
112
- OP.OP_SHA1 // SHA-1 hash
113
- OP.OP_SHA256 // SHA-256 hash
114
- OP.OP_HASH160 // RIPEMD160(SHA256(x))
115
- OP.OP_HASH256 // SHA256(SHA256(x))
116
- OP.OP_CHECKSIG // Verify signature
117
- OP.OP_CHECKMULTISIG // Verify multiple signatures
118
- ```
119
-
120
- ### Control Flow Operations
121
-
122
- Control script execution:
123
-
124
- ```typescript
125
- OP.OP_IF // Conditional execution
126
- OP.OP_ELSE // Alternative branch
127
- OP.OP_ENDIF // End conditional
128
- OP.OP_VERIFY // Fail if top stack item is false
129
- OP.OP_RETURN // Mark output as unspendable
130
- ```
131
-
132
- ## Common Script Patterns
133
-
134
- ### Pay-to-Public-Key-Hash (P2PKH)
135
-
136
- The most common Bitcoin script pattern:
137
-
138
- ```typescript
139
- // Locking script: OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
140
- const p2pkhLock = new LockingScript([
141
- { op: OP.OP_DUP },
142
- { op: OP.OP_HASH160 },
143
- { op: 20, data: publicKeyHash },
144
- { op: OP.OP_EQUALVERIFY },
145
- { op: OP.OP_CHECKSIG }
146
- ])
147
-
148
- // Unlocking script: <signature> <publicKey>
149
- const p2pkhUnlock = new UnlockingScript([
150
- { op: signature.length, data: signature },
151
- { op: publicKey.length, data: publicKey }
152
- ])
153
- ```
154
-
155
- ### Data Storage Script
156
-
157
- Store arbitrary data on the blockchain:
158
-
159
- ```typescript
160
- // OP_RETURN <data>
161
- const dataScript = new LockingScript([
162
- { op: OP.OP_RETURN },
163
- { op: data.length, data: data }
164
- ])
165
- ```
166
-
167
- ### Multi-Signature Script
168
-
169
- Require multiple signatures:
170
-
171
- ```typescript
172
- // 2-of-3 multisig: OP_2 <pubKey1> <pubKey2> <pubKey3> OP_3 OP_CHECKMULTISIG
173
- const multisigScript = new LockingScript([
174
- { op: OP.OP_2 },
175
- { op: pubKey1.length, data: pubKey1 },
176
- { op: pubKey2.length, data: pubKey2 },
177
- { op: pubKey3.length, data: pubKey3 },
178
- { op: OP.OP_3 },
179
- { op: OP.OP_CHECKMULTISIG }
180
- ])
181
- ```
182
-
183
- ## Security Considerations
184
-
185
- ### Disabled Opcodes
186
-
187
- Some opcodes are disabled for security reasons:
188
-
189
- ```typescript
190
- // These opcodes will cause script failure
191
- OP.OP_2MUL // Disabled: multiply by 2
192
- OP.OP_2DIV // Disabled: divide by 2
193
- OP.OP_VERIF // Disabled: conditional verification
194
- OP.OP_VERNOTIF // Disabled: inverse conditional verification
195
- ```
196
-
197
- ## Best Practices
198
-
199
- ### 1. Use Script Templates
200
-
201
- Leverage SDK script templates for common patterns:
202
-
203
- ```typescript
204
- import { P2PKH, MultiSig, RPuzzle } from '@bsv/sdk'
205
-
206
- // Use templates instead of manual opcode construction
207
- const p2pkh = new P2PKH()
208
- const lockingScript = p2pkh.lock(publicKeyHash)
209
- ```
210
-
211
- ### 2. Validate Scripts
212
-
213
- Always validate scripts before use:
214
-
215
- ```typescript
216
- try {
217
- const script = Script.fromASM(asmString)
218
- // Script is valid
219
- } catch (error) {
220
- console.error('Invalid script:', error.message)
221
- }
222
- ```
223
-
224
- ### 3. Handle Execution Errors
225
-
226
- Implement proper error handling:
227
-
228
- ```typescript
229
- const spend = new Spend(params)
230
- try {
231
- while (!spend.isFinished()) {
232
- if (!spend.step()) {
233
- throw new Error(`Script execution failed: ${spend.getDebugString()}`)
234
- }
235
- }
236
- } catch (error) {
237
- console.error('Script execution error:', error.message)
238
- }
239
- ```
240
-
241
- ## Common Use Cases
242
-
243
- ### 1. Payment Scripts
244
-
245
- Standard payment to public key hash:
246
-
247
- ```typescript
248
- const paymentScript = Script.fromASM(`OP_DUP OP_HASH160 ${pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG`)
249
- ```
250
-
251
- ### 2. Data Storage
252
-
253
- Store application data on-chain:
254
-
255
- ```typescript
256
- const dataScript = Script.fromASM(`OP_RETURN ${Buffer.from(jsonData).toString('hex')}`)
257
- ```
258
-
259
- ### 3. Smart Contracts
260
-
261
- Create conditional spending logic:
262
-
263
- ```typescript
264
- const contractScript = Script.fromASM(`
265
- OP_IF
266
- ${timelock} OP_CHECKLOCKTIMEVERIFY OP_DROP
267
- OP_DUP OP_HASH160 ${ownerHash} OP_EQUALVERIFY OP_CHECKSIG
268
- OP_ELSE
269
- OP_DUP OP_HASH160 ${beneficiaryHash} OP_EQUALVERIFY OP_CHECKSIG
270
- OP_ENDIF
271
- `)
272
- ```
273
-
274
- ### 4. Puzzle Scripts
275
-
276
- Create cryptographic puzzles:
277
-
278
- ```typescript
279
- // Hash puzzle: provide preimage to unlock
280
- const puzzleScript = Script.fromASM(`OP_HASH256 ${targetHash} OP_EQUAL`)
281
- ```
282
-
283
- ## Debugging Scripts
284
-
285
- ### Script Execution Tracing
286
-
287
- Monitor script execution step by step:
288
-
289
- ```typescript
290
- const spend = new Spend(params)
291
- console.log('Initial stack:', spend.getDebugString())
292
-
293
- while (!spend.isFinished()) {
294
- const success = spend.step()
295
- console.log('After step:', spend.getDebugString())
296
-
297
- if (!success) {
298
- console.log('Execution failed at:', spend.getCurrentOpcode())
299
- break
300
- }
301
- }
302
- ```
303
-
304
- ### Common Debugging Patterns
305
-
306
- Identify and fix common script issues:
307
-
308
- ```typescript
309
- // Check stack state
310
- if (spend.getStackSize() === 0) {
311
- console.log('Stack is empty - missing data?')
312
- }
313
-
314
- // Check for script errors
315
- if (spend.hasError()) {
316
- console.log('Script error:', spend.getErrorMessage())
317
- }
318
-
319
- // Verify final state
320
- if (spend.isSuccess() && spend.getStackTop() !== 1) {
321
- console.log('Script succeeded but top stack value is not true')
322
- }
323
- ```
324
-
325
- Understanding OP codes enables you to create sophisticated Bitcoin applications with custom spending conditions, smart contracts, and advanced cryptographic protocols using the BSV TypeScript SDK.
@@ -1,95 +0,0 @@
1
- # Transaction Signatures Reference
2
-
3
- ** SKELETON PLACEHOLDER - check <https://docs.bsvblockchain.org/guides/sdks/ts/low-level/tx_sig> ***
4
- *This is a technical reference document for transaction signatures in the BSV TypeScript SDK.*
5
-
6
- ## Introduction
7
-
8
- This reference document provides detailed technical information about transaction signatures in Bitcoin SV and how they are implemented in the TypeScript SDK. It complements the [Key Management and Cryptography tutorial](../tutorials/key-management.md) and the [Advanced Transaction Signing guide](../guides/advanced-transaction-signing.md).
9
-
10
- ## Signature Components
11
-
12
- ### ECDSA Signatures
13
-
14
- Bitcoin uses the Elliptic Curve Digital Signature Algorithm (ECDSA) with the secp256k1 curve. Each signature consists of two components:
15
-
16
- - `r`: A value derived from a random nonce used during signing
17
- - `s`: A value that combines the private key, message hash, and the nonce
18
-
19
- ```typescript
20
- // Structure of a Signature class instance
21
- class Signature {
22
- r: BigNumber; // The r component
23
- s: BigNumber; // The s component
24
-
25
- // Methods for serialization
26
- toDER(encoding?: 'hex'): number[] | string;
27
-
28
- // Static methods for deserialization
29
- static fromDER(bytes: number[]): Signature;
30
- // Additional methods...
31
- }
32
- ```
33
-
34
- ### DER Encoding
35
-
36
- Signatures in Bitcoin are encoded using the Distinguished Encoding Rules (DER) format, which provides a standardized way to represent the ECDSA signature components.
37
-
38
- ## Signature Hash (SIGHASH) Types
39
-
40
- Bitcoin transactions use SIGHASH flags to indicate which parts of a transaction are included in the signature hash:
41
-
42
- | Flag | Hex Value | Description |
43
- |------|-----------|-------------|
44
- | SIGHASH_ALL | 0x01 | Signs all inputs and outputs (default) |
45
- | SIGHASH_NONE | 0x02 | Signs inputs only, allows any outputs |
46
- | SIGHASH_SINGLE | 0x03 | Signs inputs and the output with the same index |
47
- | SIGHASH_ANYONECANPAY | 0x80 | Can be combined with above flags, allows additional inputs |
48
-
49
- ## Transaction Signature Verification Process
50
-
51
- 1. Extract signature and public key from the unlockingScript
52
- 2. Determine the SIGHASH flag from the signature
53
- 3. Recreate the signature hash based on the SIGHASH flag
54
- 4. Verify the signature against the hash using the public key
55
-
56
- ## API Reference
57
-
58
- ### PrivateKey Methods
59
-
60
- ```typescript
61
- // Sign a message or transaction with a private key
62
- sign(message: string | number[], sigHashType?: number): Promise<Signature>;
63
- ```
64
-
65
- ### PublicKey Methods
66
-
67
- ```typescript
68
- // Verify a signature with a public key
69
- verify(message: string | number[], signature: Signature): boolean;
70
- ```
71
-
72
- ### Transaction Methods
73
-
74
- ```typescript
75
- // Sign a transaction
76
- sign(sigHashType?: number): Promise<void>;
77
-
78
- // Verify all signatures in a transaction
79
- verify(): Promise<boolean>;
80
- ```
81
-
82
- ## Example Implementation Details
83
-
84
- ```typescript
85
- // Example of how signature hashing is implemented
86
- function calculateSignatureHash(tx: Transaction, inputIndex: number, sigHashType: number): number[] {
87
- // Implementation details...
88
- }
89
- ```
90
-
91
- ## Related Resources
92
-
93
- - [Advanced Transaction Signing guide](../guides/advanced-transaction-signing.md)
94
- - [Key Management and Cryptography tutorial](../tutorials/key-management.md)
95
- - [Bitcoin Transaction Format](https://reference.cash/protocol/blockchain/transaction)