@bsv/sdk 1.9.2 → 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.
- package/dist/cjs/package.json +1 -1
- package/dist/cjs/src/kvstore/GlobalKVStore.js +116 -98
- package/dist/cjs/src/kvstore/GlobalKVStore.js.map +1 -1
- package/dist/cjs/src/kvstore/types.js.map +1 -1
- package/dist/cjs/src/overlay-tools/index.js +1 -0
- package/dist/cjs/src/overlay-tools/index.js.map +1 -1
- package/dist/cjs/src/overlay-tools/withDoubleSpendRetry.js +55 -0
- package/dist/cjs/src/overlay-tools/withDoubleSpendRetry.js.map +1 -0
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/kvstore/GlobalKVStore.js +117 -99
- package/dist/esm/src/kvstore/GlobalKVStore.js.map +1 -1
- package/dist/esm/src/kvstore/types.js.map +1 -1
- package/dist/esm/src/overlay-tools/index.js +1 -0
- package/dist/esm/src/overlay-tools/index.js.map +1 -1
- package/dist/esm/src/overlay-tools/withDoubleSpendRetry.js +48 -0
- package/dist/esm/src/overlay-tools/withDoubleSpendRetry.js.map +1 -0
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/types/src/kvstore/GlobalKVStore.d.ts.map +1 -1
- package/dist/types/src/kvstore/types.d.ts +2 -0
- package/dist/types/src/kvstore/types.d.ts.map +1 -1
- package/dist/types/src/overlay-tools/index.d.ts +1 -0
- package/dist/types/src/overlay-tools/index.d.ts.map +1 -1
- package/dist/types/src/overlay-tools/withDoubleSpendRetry.d.ts +14 -0
- package/dist/types/src/overlay-tools/withDoubleSpendRetry.d.ts.map +1 -0
- package/dist/types/tsconfig.types.tsbuildinfo +1 -1
- package/dist/umd/bundle.js +2 -2
- package/dist/umd/bundle.js.map +1 -1
- package/docs/fast-docs.png +0 -0
- package/docs/index.md +49 -44
- package/docs/reference/kvstore.md +9 -0
- package/docs/reference/overlay-tools.md +32 -0
- package/docs/swagger.png +0 -0
- package/package.json +1 -1
- package/src/kvstore/GlobalKVStore.ts +134 -114
- package/src/kvstore/__tests/GlobalKVStore.test.ts +11 -1
- package/src/kvstore/types.ts +2 -0
- package/src/overlay-tools/index.ts +1 -0
- package/src/overlay-tools/withDoubleSpendRetry.ts +71 -0
- package/docs/MARKDOWN_VALIDATION_GUIDE.md +0 -175
- package/docs/concepts/beef.md +0 -92
- package/docs/concepts/chain-tracking.md +0 -134
- package/docs/concepts/decentralized-identity.md +0 -221
- package/docs/concepts/fees.md +0 -249
- package/docs/concepts/identity-certificates.md +0 -307
- package/docs/concepts/index.md +0 -77
- package/docs/concepts/key-management.md +0 -185
- package/docs/concepts/script-templates.md +0 -176
- package/docs/concepts/sdk-philosophy.md +0 -80
- package/docs/concepts/signatures.md +0 -194
- package/docs/concepts/spv-verification.md +0 -118
- package/docs/concepts/transaction-encoding.md +0 -167
- package/docs/concepts/transaction-structure.md +0 -67
- package/docs/concepts/trust-model.md +0 -139
- package/docs/concepts/verification.md +0 -250
- package/docs/concepts/wallet-integration.md +0 -101
- package/docs/guides/development-wallet-setup.md +0 -374
- package/docs/guides/direct-transaction-creation.md +0 -147
- package/docs/guides/http-client-configuration.md +0 -488
- package/docs/guides/index.md +0 -138
- package/docs/guides/large-transactions.md +0 -448
- package/docs/guides/multisig-transactions.md +0 -792
- package/docs/guides/security-best-practices.md +0 -494
- package/docs/guides/transaction-batching.md +0 -132
- package/docs/guides/transaction-signing-methods.md +0 -419
- package/docs/reference/arc-config.md +0 -698
- package/docs/reference/brc-100.md +0 -33
- package/docs/reference/configuration.md +0 -835
- package/docs/reference/debugging.md +0 -705
- package/docs/reference/errors.md +0 -597
- package/docs/reference/index.md +0 -111
- package/docs/reference/network-config.md +0 -914
- package/docs/reference/op-codes.md +0 -325
- package/docs/reference/transaction-signatures.md +0 -95
- package/docs/tutorials/advanced-transaction.md +0 -572
- package/docs/tutorials/aes-encryption.md +0 -949
- package/docs/tutorials/authfetch-tutorial.md +0 -986
- package/docs/tutorials/ecdh-key-exchange.md +0 -549
- package/docs/tutorials/elliptic-curve-fundamentals.md +0 -606
- package/docs/tutorials/error-handling.md +0 -1216
- package/docs/tutorials/first-transaction-low-level.md +0 -205
- package/docs/tutorials/first-transaction.md +0 -275
- package/docs/tutorials/hashes-and-hmacs.md +0 -788
- package/docs/tutorials/identity-management.md +0 -729
- package/docs/tutorials/index.md +0 -219
- package/docs/tutorials/key-management.md +0 -538
- package/docs/tutorials/protowallet-development.md +0 -743
- package/docs/tutorials/script-construction.md +0 -690
- package/docs/tutorials/spv-merkle-proofs.md +0 -685
- package/docs/tutorials/testnet-transactions-low-level.md +0 -359
- package/docs/tutorials/transaction-broadcasting.md +0 -538
- package/docs/tutorials/transaction-types.md +0 -420
- package/docs/tutorials/type-42.md +0 -568
- package/docs/tutorials/uhrp-storage.md +0 -599
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
# Key Management
|
|
2
|
-
|
|
3
|
-
Understanding how private keys, public keys, and cryptographic operations work in the BSV TypeScript SDK.
|
|
4
|
-
|
|
5
|
-
## Cryptographic Keys
|
|
6
|
-
|
|
7
|
-
Bitcoin uses elliptic curve cryptography (secp256k1) for key management:
|
|
8
|
-
|
|
9
|
-
```typescript
|
|
10
|
-
import { PrivateKey, PublicKey } from '@bsv/sdk'
|
|
11
|
-
|
|
12
|
-
// Generate a new private key
|
|
13
|
-
const privateKey = PrivateKey.fromRandom()
|
|
14
|
-
|
|
15
|
-
// Derive the corresponding public key
|
|
16
|
-
const publicKey = privateKey.toPublicKey()
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
## Private Keys
|
|
20
|
-
|
|
21
|
-
Private keys are 256-bit numbers that control Bitcoin funds:
|
|
22
|
-
|
|
23
|
-
### Generation
|
|
24
|
-
|
|
25
|
-
```typescript
|
|
26
|
-
// Secure random generation
|
|
27
|
-
const privKey = PrivateKey.fromRandom()
|
|
28
|
-
|
|
29
|
-
// From existing data (use carefully)
|
|
30
|
-
const privKey2 = PrivateKey.fromString('hex_string')
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
### Formats
|
|
34
|
-
|
|
35
|
-
```typescript
|
|
36
|
-
// WIF (Wallet Import Format)
|
|
37
|
-
const wif = privateKey.toWif()
|
|
38
|
-
|
|
39
|
-
// Hex string
|
|
40
|
-
const hex = privateKey.toString()
|
|
41
|
-
|
|
42
|
-
// DER encoding
|
|
43
|
-
const der = privateKey.toDER()
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
## Public Keys
|
|
47
|
-
|
|
48
|
-
Public keys are derived from private keys and can be shared safely:
|
|
49
|
-
|
|
50
|
-
### Derivation
|
|
51
|
-
|
|
52
|
-
```typescript
|
|
53
|
-
// Always derive from private key
|
|
54
|
-
const publicKey = privateKey.toPublicKey()
|
|
55
|
-
|
|
56
|
-
// Cannot go backwards (public -> private)
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
### Formats
|
|
60
|
-
|
|
61
|
-
```typescript
|
|
62
|
-
// Compressed (33 bytes) - preferred
|
|
63
|
-
const compressed = publicKey.toString()
|
|
64
|
-
|
|
65
|
-
// Uncompressed (65 bytes) - legacy
|
|
66
|
-
const uncompressed = publicKey.toString(false)
|
|
67
|
-
|
|
68
|
-
// DER encoding
|
|
69
|
-
const der = publicKey.toDER()
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
## Digital Signatures
|
|
73
|
-
|
|
74
|
-
Private keys create signatures that prove ownership:
|
|
75
|
-
|
|
76
|
-
```typescript
|
|
77
|
-
// Sign a message
|
|
78
|
-
const message = 'Hello Bitcoin'
|
|
79
|
-
const signature = privateKey.sign(message)
|
|
80
|
-
|
|
81
|
-
// Verify with public key
|
|
82
|
-
const isValid = publicKey.verify(message, signature)
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
## Key Derivation
|
|
86
|
-
|
|
87
|
-
The SDK supports hierarchical key derivation:
|
|
88
|
-
|
|
89
|
-
```typescript
|
|
90
|
-
// Derive child keys (simplified example)
|
|
91
|
-
const childKey = privateKey.deriveChild(0)
|
|
92
|
-
const childPubKey = childKey.toPublicKey()
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
## Security Considerations
|
|
96
|
-
|
|
97
|
-
### Private Key Security
|
|
98
|
-
|
|
99
|
-
- **Never expose**: Private keys should never be logged or transmitted
|
|
100
|
-
- **Secure storage**: Use encrypted storage for private keys
|
|
101
|
-
- **Random generation**: Always use cryptographically secure randomness
|
|
102
|
-
- **Access control**: Limit who can access private key operations
|
|
103
|
-
|
|
104
|
-
### Best Practices
|
|
105
|
-
|
|
106
|
-
```typescript
|
|
107
|
-
// Good: Generate securely
|
|
108
|
-
const key = PrivateKey.fromRandom()
|
|
109
|
-
|
|
110
|
-
// Bad: Predictable generation
|
|
111
|
-
const badKey = PrivateKey.fromString('1234567890abcdef...')
|
|
112
|
-
|
|
113
|
-
// Good: Derive public key when needed
|
|
114
|
-
const pubKey = key.toPublicKey()
|
|
115
|
-
|
|
116
|
-
// Bad: Store private key unnecessarily
|
|
117
|
-
localStorage.setItem('privateKey', key.toString())
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
## Wallet Integration
|
|
121
|
-
|
|
122
|
-
In most applications, wallets handle key management:
|
|
123
|
-
|
|
124
|
-
The `WalletClient` provides high-level key management through wallet integration:
|
|
125
|
-
|
|
126
|
-
```typescript
|
|
127
|
-
// Wallet manages keys securely
|
|
128
|
-
const wallet = new WalletClient()
|
|
129
|
-
|
|
130
|
-
// Application doesn't see private keys
|
|
131
|
-
const action = await wallet.createAction({
|
|
132
|
-
outputs: [/* transaction outputs */]
|
|
133
|
-
})
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
When using the `WalletClient`, keys are managed by the connected wallet service:
|
|
137
|
-
|
|
138
|
-
The `WalletClient` approach is recommended for production applications as it provides:
|
|
139
|
-
|
|
140
|
-
## Key Recovery
|
|
141
|
-
|
|
142
|
-
Keys can be recovered from various formats:
|
|
143
|
-
|
|
144
|
-
```typescript
|
|
145
|
-
// From WIF format
|
|
146
|
-
const key1 = PrivateKey.fromWif(wifString)
|
|
147
|
-
|
|
148
|
-
// From hex string
|
|
149
|
-
const key2 = PrivateKey.fromString(hexString)
|
|
150
|
-
|
|
151
|
-
// From DER encoding
|
|
152
|
-
const key3 = PrivateKey.fromDER(derBytes)
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
## Cryptographic Operations
|
|
156
|
-
|
|
157
|
-
The SDK provides secure implementations of:
|
|
158
|
-
|
|
159
|
-
- **ECDSA**: Digital signature algorithm
|
|
160
|
-
- **ECDH**: Key exchange protocol
|
|
161
|
-
- **Hash Functions**: SHA-256, RIPEMD-160
|
|
162
|
-
- **AES**: Symmetric encryption
|
|
163
|
-
|
|
164
|
-
## Memory Management
|
|
165
|
-
|
|
166
|
-
Sensitive key data should be cleared when no longer needed:
|
|
167
|
-
|
|
168
|
-
- Use secure memory practices
|
|
169
|
-
- Clear variables containing key data
|
|
170
|
-
- Avoid keeping keys in memory longer than necessary
|
|
171
|
-
|
|
172
|
-
## Testing and Development
|
|
173
|
-
|
|
174
|
-
For development and testing:
|
|
175
|
-
|
|
176
|
-
- Use testnet for experiments
|
|
177
|
-
- Generate new keys for each test
|
|
178
|
-
- Never use mainnet keys in test code
|
|
179
|
-
- Implement proper key rotation
|
|
180
|
-
|
|
181
|
-
## Next Steps
|
|
182
|
-
|
|
183
|
-
- Understand [Digital Signatures](./signatures.md) in detail
|
|
184
|
-
- Learn about [Trust Model](./trust-model.md) considerations
|
|
185
|
-
- Explore [Wallet Integration](./wallet-integration.md) patterns
|
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
# Script Templates
|
|
2
|
-
|
|
3
|
-
Standard and custom Bitcoin script patterns available in the BSV TypeScript SDK.
|
|
4
|
-
|
|
5
|
-
## What are Script Templates?
|
|
6
|
-
|
|
7
|
-
Script templates provide reusable patterns for Bitcoin transaction locking and unlocking scripts:
|
|
8
|
-
|
|
9
|
-
```typescript
|
|
10
|
-
import { P2PKH } from '@bsv/sdk'
|
|
11
|
-
|
|
12
|
-
// Use a standard template
|
|
13
|
-
const template = new P2PKH()
|
|
14
|
-
|
|
15
|
-
// Create locking script
|
|
16
|
-
const lockingScript = template.lock(publicKeyHash)
|
|
17
|
-
|
|
18
|
-
// Create unlocking script
|
|
19
|
-
const unlockingScript = template.unlock(privateKey, signature)
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
## Standard Templates
|
|
23
|
-
|
|
24
|
-
### P2PKH (Pay to Public Key Hash)
|
|
25
|
-
|
|
26
|
-
The most common Bitcoin script pattern:
|
|
27
|
-
|
|
28
|
-
```typescript
|
|
29
|
-
const p2pkh = new P2PKH()
|
|
30
|
-
const lock = p2pkh.lock(publicKeyHash)
|
|
31
|
-
const unlock = p2pkh.unlock(privateKey, signature)
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
### P2PK (Pay to Public Key)
|
|
35
|
-
|
|
36
|
-
Direct payment to a public key:
|
|
37
|
-
|
|
38
|
-
```typescript
|
|
39
|
-
const p2pk = new P2PK()
|
|
40
|
-
const lock = p2pk.lock(publicKey)
|
|
41
|
-
const unlock = p2pk.unlock(signature)
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
### OP_RETURN (Data Storage)
|
|
45
|
-
|
|
46
|
-
Store arbitrary data on-chain:
|
|
47
|
-
|
|
48
|
-
```typescript
|
|
49
|
-
const opReturn = new OpReturn()
|
|
50
|
-
const lock = opReturn.lock(data)
|
|
51
|
-
// No unlock needed - unspendable output
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
## Custom Templates
|
|
55
|
-
|
|
56
|
-
Create your own script templates:
|
|
57
|
-
|
|
58
|
-
```typescript
|
|
59
|
-
class TimeLockTemplate implements ScriptTemplate {
|
|
60
|
-
lock(lockTime: number, publicKeyHash: string): Script {
|
|
61
|
-
return new Script()
|
|
62
|
-
.writeNumber(lockTime)
|
|
63
|
-
.writeOpCode(OpCode.OP_CHECKLOCKTIMEVERIFY)
|
|
64
|
-
.writeOpCode(OpCode.OP_DROP)
|
|
65
|
-
.writeOpCode(OpCode.OP_DUP)
|
|
66
|
-
.writeOpCode(OpCode.OP_HASH160)
|
|
67
|
-
.writeBin(publicKeyHash)
|
|
68
|
-
.writeOpCode(OpCode.OP_EQUALVERIFY)
|
|
69
|
-
.writeOpCode(OpCode.OP_CHECKSIG)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
unlock(signature: string, publicKey: string): Script {
|
|
73
|
-
return new Script()
|
|
74
|
-
.writeBin(signature)
|
|
75
|
-
.writeBin(publicKey)
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
## Template Interface
|
|
81
|
-
|
|
82
|
-
All templates implement the ScriptTemplate interface:
|
|
83
|
-
|
|
84
|
-
```typescript
|
|
85
|
-
interface ScriptTemplate {
|
|
86
|
-
lock(...args: any[]): Script
|
|
87
|
-
unlock(...args: any[]): Script
|
|
88
|
-
estimateLength?(args: any[]): number
|
|
89
|
-
}
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
## Benefits
|
|
93
|
-
|
|
94
|
-
### Reusability
|
|
95
|
-
|
|
96
|
-
- Standard patterns for common use cases
|
|
97
|
-
- Consistent implementation across applications
|
|
98
|
-
- Reduced development time
|
|
99
|
-
|
|
100
|
-
### Security
|
|
101
|
-
|
|
102
|
-
- Well-tested script patterns
|
|
103
|
-
- Reduced risk of script errors
|
|
104
|
-
- Best practice implementations
|
|
105
|
-
|
|
106
|
-
### Maintainability
|
|
107
|
-
|
|
108
|
-
- Clear separation of script logic
|
|
109
|
-
- Easy to update and modify
|
|
110
|
-
- Testable components
|
|
111
|
-
|
|
112
|
-
## Working with Templates
|
|
113
|
-
|
|
114
|
-
### Transaction Integration
|
|
115
|
-
|
|
116
|
-
```typescript
|
|
117
|
-
// Use template in transaction
|
|
118
|
-
const output = {
|
|
119
|
-
satoshis: 1000,
|
|
120
|
-
lockingScript: template.lock(publicKeyHash)
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const input = {
|
|
124
|
-
sourceTransaction: prevTx,
|
|
125
|
-
sourceOutputIndex: 0,
|
|
126
|
-
unlockingScript: template.unlock(privateKey, signature)
|
|
127
|
-
}
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
### Fee Estimation
|
|
131
|
-
|
|
132
|
-
```typescript
|
|
133
|
-
// Estimate script size for fee calculation
|
|
134
|
-
const estimatedSize = template.estimateLength([publicKeyHash])
|
|
135
|
-
const fee = estimatedSize * feePerByte
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
## Advanced Patterns
|
|
139
|
-
|
|
140
|
-
### Multi-Signature
|
|
141
|
-
|
|
142
|
-
```typescript
|
|
143
|
-
class MultiSigTemplate implements ScriptTemplate {
|
|
144
|
-
lock(threshold: number, publicKeys: string[]): Script {
|
|
145
|
-
// Implementation for m-of-n multisig
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
unlock(signatures: string[]): Script {
|
|
149
|
-
// Implementation for signature collection
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
### Conditional Scripts
|
|
155
|
-
|
|
156
|
-
```typescript
|
|
157
|
-
class ConditionalTemplate implements ScriptTemplate {
|
|
158
|
-
lock(condition: Script, trueScript: Script, falseScript: Script): Script {
|
|
159
|
-
// Implementation for IF/ELSE logic
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
## Best Practices
|
|
165
|
-
|
|
166
|
-
- Use standard templates when possible
|
|
167
|
-
- Test custom templates thoroughly
|
|
168
|
-
- Document template parameters clearly
|
|
169
|
-
- Consider script size and complexity
|
|
170
|
-
- Validate inputs before script creation
|
|
171
|
-
|
|
172
|
-
## Next Steps
|
|
173
|
-
|
|
174
|
-
- Learn about [Digital Signatures](./signatures.md) for script authorization
|
|
175
|
-
- Understand [Transaction Structure](./transaction-structure.md) integration
|
|
176
|
-
- Explore [Key Management](./key-management.md) for script security
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
# SDK Design Philosophy
|
|
2
|
-
|
|
3
|
-
This document details the core principles behind the BSV TypeScript SDK architecture and design decisions.
|
|
4
|
-
|
|
5
|
-
## Zero Dependencies
|
|
6
|
-
|
|
7
|
-
The SDK is built without external dependencies to:
|
|
8
|
-
|
|
9
|
-
- Minimize security attack surface
|
|
10
|
-
- Reduce bundle size and complexity
|
|
11
|
-
- Ensure long-term stability
|
|
12
|
-
- Provide predictable behavior
|
|
13
|
-
|
|
14
|
-
## SPV-First Approach
|
|
15
|
-
|
|
16
|
-
The SDK prioritizes Simplified Payment Verification:
|
|
17
|
-
|
|
18
|
-
- **Lightweight**: No need to download the full blockchain
|
|
19
|
-
- **Efficient**: Verify transactions using merkle proofs
|
|
20
|
-
- **Scalable**: Works with millions of transactions
|
|
21
|
-
- **Secure**: Cryptographically verifiable without full nodes
|
|
22
|
-
|
|
23
|
-
## Vendor Neutrality
|
|
24
|
-
|
|
25
|
-
The SDK works with any compliant Bitcoin infrastructure:
|
|
26
|
-
|
|
27
|
-
- **Wallet Agnostic**: Supports any BRC-100 compliant wallet
|
|
28
|
-
- **Network Flexible**: Works with different chain tracking services
|
|
29
|
-
- **Service Independent**: No lock-in to specific providers
|
|
30
|
-
|
|
31
|
-
## Modular Design
|
|
32
|
-
|
|
33
|
-
Components are designed to work independently:
|
|
34
|
-
|
|
35
|
-
- **Composable**: Mix and match functionality as needed
|
|
36
|
-
- **Extensible**: Easy to add custom implementations
|
|
37
|
-
- **Testable**: Each component can be tested in isolation
|
|
38
|
-
- **Maintainable**: Clear separation of concerns
|
|
39
|
-
|
|
40
|
-
## TypeScript-First
|
|
41
|
-
|
|
42
|
-
Built specifically for TypeScript to provide:
|
|
43
|
-
|
|
44
|
-
- **Type Safety**: Catch errors at compile time
|
|
45
|
-
- **Developer Experience**: Rich IDE support and autocomplete
|
|
46
|
-
- **Documentation**: Types serve as living documentation
|
|
47
|
-
- **Reliability**: Reduced runtime errors
|
|
48
|
-
|
|
49
|
-
## Security by Design
|
|
50
|
-
|
|
51
|
-
Security considerations are built into every component:
|
|
52
|
-
|
|
53
|
-
- **Cryptographic Primitives**: Secure implementations of Bitcoin cryptography
|
|
54
|
-
- **Input Validation**: All inputs are validated and sanitized
|
|
55
|
-
- **Error Handling**: Comprehensive error handling prevents information leakage
|
|
56
|
-
- **Best Practices**: Follows established security patterns
|
|
57
|
-
|
|
58
|
-
## Performance Focused
|
|
59
|
-
|
|
60
|
-
Optimized for real-world application needs:
|
|
61
|
-
|
|
62
|
-
- **Memory Efficient**: Minimal memory footprint
|
|
63
|
-
- **Fast Execution**: Optimized critical paths
|
|
64
|
-
- **Batch Processing**: Support for high-throughput scenarios
|
|
65
|
-
- **Caching**: Intelligent caching where appropriate
|
|
66
|
-
|
|
67
|
-
## Developer-Friendly
|
|
68
|
-
|
|
69
|
-
Designed to make Bitcoin development accessible:
|
|
70
|
-
|
|
71
|
-
- **Clear APIs**: Intuitive method names and parameters
|
|
72
|
-
- **Comprehensive Documentation**: Tutorials, guides, and references
|
|
73
|
-
- **Working Examples**: Real code that developers can use immediately
|
|
74
|
-
- **Progressive Complexity**: Start simple, add complexity as needed
|
|
75
|
-
|
|
76
|
-
## Next Steps
|
|
77
|
-
|
|
78
|
-
- Understand [Wallet Integration](./wallet-integration.md) patterns
|
|
79
|
-
- Learn about [SPV Verification](./spv-verification.md) concepts
|
|
80
|
-
- Explore [Key Management](./key-management.md) approaches
|
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
# Digital Signatures
|
|
2
|
-
|
|
3
|
-
How digital signatures work in Bitcoin and their implementation in the BSV TypeScript SDK.
|
|
4
|
-
|
|
5
|
-
## What are Digital Signatures?
|
|
6
|
-
|
|
7
|
-
Digital signatures prove ownership and authorize Bitcoin transactions:
|
|
8
|
-
|
|
9
|
-
```typescript
|
|
10
|
-
import { PrivateKey, Transaction } from '@bsv/sdk'
|
|
11
|
-
|
|
12
|
-
// Create a signature
|
|
13
|
-
const privateKey = PrivateKey.fromRandom()
|
|
14
|
-
const message = 'transaction data'
|
|
15
|
-
const signature = privateKey.sign(message)
|
|
16
|
-
|
|
17
|
-
// Verify the signature
|
|
18
|
-
const publicKey = privateKey.toPublicKey()
|
|
19
|
-
const isValid = publicKey.verify(message, signature)
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
## Bitcoin Signatures
|
|
23
|
-
|
|
24
|
-
Bitcoin uses ECDSA (Elliptic Curve Digital Signature Algorithm):
|
|
25
|
-
|
|
26
|
-
- **secp256k1**: The elliptic curve used by Bitcoin
|
|
27
|
-
- **SHA-256**: Hash function for message digests
|
|
28
|
-
- **DER Encoding**: Standard format for signature serialization
|
|
29
|
-
|
|
30
|
-
## SIGHASH Types
|
|
31
|
-
|
|
32
|
-
SIGHASH flags determine what parts of a transaction are signed:
|
|
33
|
-
|
|
34
|
-
### SIGHASH_ALL (Default)
|
|
35
|
-
|
|
36
|
-
Signs all inputs and outputs:
|
|
37
|
-
|
|
38
|
-
```typescript
|
|
39
|
-
const signature = privateKey.sign(txHash, 'all')
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
### SIGHASH_NONE
|
|
43
|
-
|
|
44
|
-
Signs all inputs but no outputs:
|
|
45
|
-
|
|
46
|
-
```typescript
|
|
47
|
-
const signature = privateKey.sign(txHash, 'none')
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
### SIGHASH_SINGLE
|
|
51
|
-
|
|
52
|
-
Signs all inputs and one corresponding output:
|
|
53
|
-
|
|
54
|
-
```typescript
|
|
55
|
-
const signature = privateKey.sign(txHash, 'single')
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
### SIGHASH_ANYONECANPAY
|
|
59
|
-
|
|
60
|
-
Can be combined with other flags to sign only one input:
|
|
61
|
-
|
|
62
|
-
```typescript
|
|
63
|
-
const signature = privateKey.sign(txHash, 'all|anyonecanpay')
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
## Transaction Signing
|
|
67
|
-
|
|
68
|
-
The SDK handles transaction signing automatically:
|
|
69
|
-
|
|
70
|
-
```typescript
|
|
71
|
-
// Manual signing (low-level)
|
|
72
|
-
const tx = new Transaction()
|
|
73
|
-
const signature = tx.sign(privateKey, inputIndex, sighashType)
|
|
74
|
-
|
|
75
|
-
// Wallet signing (recommended)
|
|
76
|
-
const wallet = new WalletClient()
|
|
77
|
-
const action = await wallet.createAction({
|
|
78
|
-
outputs: [/* outputs */]
|
|
79
|
-
})
|
|
80
|
-
// Wallet handles signing internally
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
## Signature Verification
|
|
84
|
-
|
|
85
|
-
Verify signatures to ensure transaction validity:
|
|
86
|
-
|
|
87
|
-
```typescript
|
|
88
|
-
// Verify a specific signature
|
|
89
|
-
const isValid = publicKey.verify(messageHash, signature)
|
|
90
|
-
|
|
91
|
-
// Verify entire transaction
|
|
92
|
-
const txValid = await transaction.verify(chainTracker)
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
## DER Encoding
|
|
96
|
-
|
|
97
|
-
Signatures are encoded in DER format:
|
|
98
|
-
|
|
99
|
-
```typescript
|
|
100
|
-
// Get DER-encoded signature
|
|
101
|
-
const derSignature = signature.toDER()
|
|
102
|
-
|
|
103
|
-
// Parse DER signature
|
|
104
|
-
const sig = Signature.fromDER(derBytes)
|
|
105
|
-
|
|
106
|
-
// Get r and s components
|
|
107
|
-
const r = signature.r
|
|
108
|
-
const s = signature.s
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
## Security Considerations
|
|
112
|
-
|
|
113
|
-
### Nonce Security
|
|
114
|
-
|
|
115
|
-
- Each signature must use a unique, random nonce
|
|
116
|
-
- Reusing nonces can leak private keys
|
|
117
|
-
- The SDK handles nonce generation securely
|
|
118
|
-
|
|
119
|
-
### Signature Malleability
|
|
120
|
-
|
|
121
|
-
- Bitcoin signatures can be modified without invalidating them
|
|
122
|
-
- Use canonical signatures to prevent malleability
|
|
123
|
-
- The SDK produces canonical signatures by default
|
|
124
|
-
|
|
125
|
-
### Hash Types
|
|
126
|
-
|
|
127
|
-
- Choose appropriate SIGHASH types for your use case
|
|
128
|
-
- SIGHASH_ALL is safest for most applications
|
|
129
|
-
- Other types enable advanced transaction patterns
|
|
130
|
-
|
|
131
|
-
## Common Patterns
|
|
132
|
-
|
|
133
|
-
### Multi-Input Signing
|
|
134
|
-
|
|
135
|
-
```typescript
|
|
136
|
-
// Sign multiple inputs in a transaction
|
|
137
|
-
for (let i = 0; i < transaction.inputs.length; i++) {
|
|
138
|
-
const signature = privateKey.sign(transaction.getSignatureHash(i))
|
|
139
|
-
transaction.inputs[i].unlockingScript = createUnlockingScript(signature)
|
|
140
|
-
}
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
### Conditional Signatures
|
|
144
|
-
|
|
145
|
-
```typescript
|
|
146
|
-
// Different signatures for different conditions
|
|
147
|
-
const signature1 = privateKey1.sign(txHash, 'all')
|
|
148
|
-
const signature2 = privateKey2.sign(txHash, 'single')
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
## Error Handling
|
|
152
|
-
|
|
153
|
-
Common signature issues:
|
|
154
|
-
|
|
155
|
-
- Invalid private key format
|
|
156
|
-
- Incorrect message hash
|
|
157
|
-
- Malformed signature data
|
|
158
|
-
- Verification failures
|
|
159
|
-
|
|
160
|
-
```typescript
|
|
161
|
-
try {
|
|
162
|
-
const signature = privateKey.sign(message)
|
|
163
|
-
} catch (error) {
|
|
164
|
-
console.error('Signing failed:', error.message)
|
|
165
|
-
}
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
## Best Practices
|
|
169
|
-
|
|
170
|
-
- Always use secure random number generation
|
|
171
|
-
- Verify signatures before trusting them
|
|
172
|
-
- Use appropriate SIGHASH types for your use case
|
|
173
|
-
- Store signatures in DER format for interoperability
|
|
174
|
-
- Never reuse nonces across signatures
|
|
175
|
-
|
|
176
|
-
## Wallet Integration
|
|
177
|
-
|
|
178
|
-
Most applications use wallets for signing:
|
|
179
|
-
|
|
180
|
-
```typescript
|
|
181
|
-
// Wallet handles signature creation
|
|
182
|
-
const wallet = new WalletClient()
|
|
183
|
-
const result = await wallet.createAction({
|
|
184
|
-
description: 'Payment transaction',
|
|
185
|
-
outputs: [/* outputs */]
|
|
186
|
-
})
|
|
187
|
-
// Signatures created automatically
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
## Next Steps
|
|
191
|
-
|
|
192
|
-
- Understand [Key Management](./key-management.md) for signature security
|
|
193
|
-
- Learn about [Script Templates](./script-templates.md) for signature usage
|
|
194
|
-
- Explore [Transaction Structure](./transaction-structure.md) for signature placement
|