@cryptforge/blockchain-btc 0.1.0 → 0.2.0
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/README.md +338 -19
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,42 +1,361 @@
|
|
|
1
1
|
# @cryptforge/blockchain-btc
|
|
2
2
|
|
|
3
|
-
Bitcoin blockchain adapter for the CryptForge SDK.
|
|
3
|
+
Bitcoin blockchain adapter for the CryptForge SDK. Provides BIP44-compliant key derivation, address generation, and transaction signing for Bitcoin.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm install @cryptforge/blockchain-btc
|
|
8
|
+
npm install @cryptforge/blockchain-btc @cryptforge/auth @cryptforge/core
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @cryptforge/blockchain-btc @cryptforge/auth @cryptforge/core
|
|
11
|
+
# or
|
|
12
|
+
yarn add @cryptforge/blockchain-btc @cryptforge/auth @cryptforge/core
|
|
9
13
|
```
|
|
10
14
|
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { createAuthClient } from '@cryptforge/auth';
|
|
19
|
+
import { BitcoinAdapter } from '@cryptforge/blockchain-btc';
|
|
20
|
+
|
|
21
|
+
// Create auth client and register Bitcoin adapter
|
|
22
|
+
const auth = createAuthClient();
|
|
23
|
+
auth.registerAdapter('bitcoin', new BitcoinAdapter());
|
|
24
|
+
|
|
25
|
+
// Generate mnemonic
|
|
26
|
+
const mnemonic = auth.generateMnemonic({ wordCount: 12 });
|
|
27
|
+
|
|
28
|
+
// Create identity with Bitcoin
|
|
29
|
+
const { identity, keys } = await auth.createIdentity({
|
|
30
|
+
mnemonic,
|
|
31
|
+
password: 'secure-password',
|
|
32
|
+
label: 'My Bitcoin Wallet',
|
|
33
|
+
chainId: 'bitcoin',
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
console.log('Bitcoin Address:', keys.address);
|
|
37
|
+
// Example: 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Features
|
|
41
|
+
|
|
42
|
+
- ✅ **BIP44 Derivation** - Standard Bitcoin derivation path: `m/44'/0'/0'/0/0`
|
|
43
|
+
- ✅ **P2PKH Addresses** - Pay-to-Public-Key-Hash address generation
|
|
44
|
+
- ✅ **Message Signing** - Sign messages with Bitcoin keys
|
|
45
|
+
- ✅ **Transaction Signing** - Sign Bitcoin transactions (UTXO-based)
|
|
46
|
+
- ✅ **HD Wallet Support** - Hierarchical Deterministic wallet key derivation
|
|
47
|
+
- ✅ **Multiple Addresses** - Generate multiple addresses from a single seed
|
|
48
|
+
- ✅ **Type-Safe** - Full TypeScript support
|
|
49
|
+
|
|
11
50
|
## Usage
|
|
12
51
|
|
|
52
|
+
### With @cryptforge/auth (Recommended)
|
|
53
|
+
|
|
54
|
+
The recommended way to use this adapter is through `@cryptforge/auth`:
|
|
55
|
+
|
|
13
56
|
```typescript
|
|
57
|
+
import { createAuthClient } from '@cryptforge/auth';
|
|
14
58
|
import { BitcoinAdapter } from '@cryptforge/blockchain-btc';
|
|
15
|
-
import { setupCryptForgeHandlers } from '@cryptforge/transport-electron/main';
|
|
16
59
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
60
|
+
const auth = createAuthClient();
|
|
61
|
+
auth.registerAdapter('bitcoin', new BitcoinAdapter());
|
|
62
|
+
|
|
63
|
+
// Unlock wallet
|
|
64
|
+
await auth.unlock({
|
|
65
|
+
password: 'your-password',
|
|
66
|
+
chainId: 'bitcoin',
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Get current Bitcoin address
|
|
70
|
+
const address = auth.currentAddress;
|
|
71
|
+
console.log('Address:', address);
|
|
72
|
+
|
|
73
|
+
// Get multiple addresses
|
|
74
|
+
const addresses = await auth.getAddresses('bitcoin', 0, 5);
|
|
75
|
+
console.log('First 5 addresses:', addresses);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Sign Message
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
// Unlock wallet first
|
|
82
|
+
await auth.unlock({
|
|
83
|
+
password: 'your-password',
|
|
84
|
+
chainId: 'bitcoin',
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// Sign a message
|
|
88
|
+
const { signature, address, publicKey } = await auth.signMessage({
|
|
89
|
+
message: 'Hello Bitcoin!',
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
console.log('Signature:', signature);
|
|
93
|
+
console.log('Signed by:', address);
|
|
94
|
+
|
|
95
|
+
// Verify signature
|
|
96
|
+
const isValid = await auth.verifySignature(
|
|
97
|
+
'Hello Bitcoin!',
|
|
98
|
+
signature,
|
|
99
|
+
publicKey
|
|
100
|
+
);
|
|
101
|
+
console.log('Signature valid:', isValid);
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Sign Transaction
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
// Unlock wallet first
|
|
108
|
+
await auth.unlock({
|
|
109
|
+
password: 'your-password',
|
|
110
|
+
chainId: 'bitcoin',
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// Sign a Bitcoin transaction
|
|
114
|
+
const { signedTransaction, signature } = await auth.signTransaction({
|
|
115
|
+
transaction: {
|
|
116
|
+
inputs: [
|
|
117
|
+
{
|
|
118
|
+
txid: 'previous_transaction_id',
|
|
119
|
+
index: 0,
|
|
120
|
+
// ... other input fields
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
outputs: [
|
|
124
|
+
{
|
|
125
|
+
address: '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
|
|
126
|
+
amount: '100000', // satoshis
|
|
127
|
+
},
|
|
128
|
+
],
|
|
21
129
|
},
|
|
22
|
-
// ... other options
|
|
23
130
|
});
|
|
131
|
+
|
|
132
|
+
console.log('Signed Transaction:', signedTransaction);
|
|
24
133
|
```
|
|
25
134
|
|
|
26
|
-
|
|
135
|
+
### Generate Multiple Addresses
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
// Get addresses at specific indices
|
|
139
|
+
const addresses = await auth.getAddresses('bitcoin', 0, 10);
|
|
140
|
+
|
|
141
|
+
addresses.forEach((addr) => {
|
|
142
|
+
console.log(`Index ${addr.index}: ${addr.address}`);
|
|
143
|
+
console.log(`Path: ${addr.path}`);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// Output:
|
|
147
|
+
// Index 0: 1A1zP1eP5... Path: m/44'/0'/0'/0/0
|
|
148
|
+
// Index 1: 1BvBMSEYs... Path: m/44'/0'/0'/0/1
|
|
149
|
+
// ...
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Direct Adapter Usage (Advanced)
|
|
153
|
+
|
|
154
|
+
For advanced use cases, you can use the adapter directly:
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
import { BitcoinAdapter } from '@cryptforge/blockchain-btc';
|
|
158
|
+
|
|
159
|
+
const adapter = new BitcoinAdapter();
|
|
160
|
+
|
|
161
|
+
// Derive keys from mnemonic
|
|
162
|
+
const keys = await adapter.deriveKeys(
|
|
163
|
+
'your twelve word mnemonic phrase here that you backed up safely'
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
console.log('Address:', keys.address);
|
|
167
|
+
console.log('Public Key:', keys.publicKeyHex);
|
|
168
|
+
console.log('Derivation Path:', keys.path);
|
|
169
|
+
|
|
170
|
+
// Get address at specific index
|
|
171
|
+
const { address, publicKey, path } = await adapter.getAddressAtIndex(
|
|
172
|
+
'your twelve word mnemonic phrase here that you backed up safely',
|
|
173
|
+
5
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
// Sign message
|
|
177
|
+
const { signature } = await adapter.signMessage(
|
|
178
|
+
keys.privateKey,
|
|
179
|
+
'Hello Bitcoin!'
|
|
180
|
+
);
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## API Reference
|
|
184
|
+
|
|
185
|
+
### BitcoinAdapter
|
|
186
|
+
|
|
187
|
+
#### Properties
|
|
188
|
+
|
|
189
|
+
- `chainData: ChainData` - Chain metadata
|
|
190
|
+
- `name: "Bitcoin"`
|
|
191
|
+
- `symbol: "BTC"`
|
|
192
|
+
- `cmc_id: 1` (CoinMarketCap ID)
|
|
193
|
+
- `decimals: 8`
|
|
194
|
+
|
|
195
|
+
#### Methods
|
|
196
|
+
|
|
197
|
+
##### `deriveKeys(mnemonic: string): Promise<KeyData>`
|
|
198
|
+
|
|
199
|
+
Derives Bitcoin keys from a BIP39 mnemonic using path `m/44'/0'/0'/0/0`.
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
const keys = await adapter.deriveKeys(mnemonic);
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
##### `deriveKeysAtIndex(mnemonic: string, index: number): Promise<KeyData>`
|
|
206
|
+
|
|
207
|
+
Derives keys at a specific address index.
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
const keys = await adapter.deriveKeysAtIndex(mnemonic, 5);
|
|
211
|
+
// Path: m/44'/0'/0'/0/5
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
##### `deriveKeysAtPath(mnemonic: string, path: string): Promise<KeyData>`
|
|
27
215
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
216
|
+
Derives keys at a custom BIP44 path.
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
219
|
+
const keys = await adapter.deriveKeysAtPath(mnemonic, "m/44'/0'/1'/0/0");
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
##### `getAddressAtIndex(mnemonic: string, index: number): Promise<{ address, publicKey, path }>`
|
|
223
|
+
|
|
224
|
+
Gets address information at a specific index without returning private keys.
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
const { address, publicKey, path } = await adapter.getAddressAtIndex(
|
|
228
|
+
mnemonic,
|
|
229
|
+
0
|
|
230
|
+
);
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
##### `getAddresses(mnemonic: string, startIndex: number, count: number): Promise<Array<{ address, path, index }>>`
|
|
234
|
+
|
|
235
|
+
Gets multiple addresses starting from an index.
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
const addresses = await adapter.getAddresses(mnemonic, 0, 10);
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
##### `signMessage(privateKey: Uint8Array, message: string | Uint8Array): Promise<{ signature }>`
|
|
242
|
+
|
|
243
|
+
Signs a message using Bitcoin's double SHA-256 hash.
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
const { signature } = await adapter.signMessage(
|
|
247
|
+
keys.privateKey,
|
|
248
|
+
'Hello Bitcoin!'
|
|
249
|
+
);
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
##### `signTransaction(privateKey: Uint8Array, transaction: any): Promise<{ signedTransaction, signature }>`
|
|
253
|
+
|
|
254
|
+
Signs a Bitcoin transaction. Requires UTXO-based transaction structure.
|
|
255
|
+
|
|
256
|
+
```typescript
|
|
257
|
+
const { signedTransaction, signature } = await adapter.signTransaction(
|
|
258
|
+
keys.privateKey,
|
|
259
|
+
{
|
|
260
|
+
inputs: [{ txid: '...', index: 0 }],
|
|
261
|
+
outputs: [{ address: '...', amount: '100000' }],
|
|
262
|
+
}
|
|
263
|
+
);
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
##### `verifySignature(message: string | Uint8Array, signature: string, publicKey: string): Promise<boolean>`
|
|
267
|
+
|
|
268
|
+
Verifies a message signature.
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
const isValid = await adapter.verifySignature(message, signature, publicKey);
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
## KeyData Type
|
|
275
|
+
|
|
276
|
+
```typescript
|
|
277
|
+
interface KeyData {
|
|
278
|
+
mnemonic: string; // BIP39 mnemonic phrase
|
|
279
|
+
seed: Uint8Array; // BIP39 seed (512 bits)
|
|
280
|
+
privateKey: Uint8Array; // Private key bytes
|
|
281
|
+
privateKeyHex: string; // Private key as hex string
|
|
282
|
+
publicKey: Uint8Array; // Compressed public key (33 bytes)
|
|
283
|
+
publicKeyHex: string; // Public key as hex string
|
|
284
|
+
address: string; // Bitcoin P2PKH address
|
|
285
|
+
path: string; // BIP44 derivation path
|
|
286
|
+
}
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
## Address Types
|
|
290
|
+
|
|
291
|
+
This adapter currently supports **P2PKH (Pay-to-Public-Key-Hash)** addresses only. P2PKH addresses:
|
|
292
|
+
- Start with `1` (mainnet) or `m`/`n` (testnet)
|
|
293
|
+
- Are the most common legacy Bitcoin address type
|
|
294
|
+
- Use base58check encoding
|
|
295
|
+
- Example: `1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa`
|
|
296
|
+
|
|
297
|
+
Future versions may support:
|
|
298
|
+
- P2SH (Pay-to-Script-Hash) - addresses starting with `3`
|
|
299
|
+
- P2WPKH (SegWit) - addresses starting with `bc1q`
|
|
300
|
+
- P2TR (Taproot) - addresses starting with `bc1p`
|
|
32
301
|
|
|
33
302
|
## Dependencies
|
|
34
303
|
|
|
35
|
-
This package
|
|
36
|
-
|
|
37
|
-
-
|
|
38
|
-
-
|
|
39
|
-
-
|
|
304
|
+
This package uses modern, audited Bitcoin libraries:
|
|
305
|
+
|
|
306
|
+
- **`@scure/bip32`** - BIP32 HD wallet key derivation
|
|
307
|
+
- **`@scure/bip39`** - BIP39 mnemonic generation and validation
|
|
308
|
+
- **`@scure/btc-signer`** - Bitcoin transaction signing
|
|
309
|
+
- **`@noble/hashes`** - Cryptographic hash functions (SHA-256)
|
|
310
|
+
- **`@noble/secp256k1`** - secp256k1 elliptic curve cryptography
|
|
311
|
+
|
|
312
|
+
All dependencies are minimal, audited, and widely trusted in the Bitcoin ecosystem.
|
|
313
|
+
|
|
314
|
+
## Limitations
|
|
315
|
+
|
|
316
|
+
### Not Yet Implemented
|
|
317
|
+
|
|
318
|
+
The following features are not yet implemented but are planned for future releases:
|
|
319
|
+
|
|
320
|
+
- **Balance Queries** - `getNativeBalance()`, `getTokenBalances()`
|
|
321
|
+
- **Transaction History** - `getTransactions()`, `getTokenTransfers()`
|
|
322
|
+
- **Transaction Broadcasting** - `sendNativeToken()`, `sendToken()`
|
|
323
|
+
- **Transaction Status** - `getTransactionStatus()`
|
|
324
|
+
|
|
325
|
+
For these features, please use:
|
|
326
|
+
- Block explorer APIs (Blockstream, Blockchain.com, etc.)
|
|
327
|
+
- Bitcoin RPC providers
|
|
328
|
+
- Bitcoin wallet SDKs
|
|
329
|
+
|
|
330
|
+
### UTXO Management
|
|
331
|
+
|
|
332
|
+
Bitcoin uses a UTXO (Unspent Transaction Output) model, which is more complex than account-based models (like Ethereum). When building a transaction, you need to:
|
|
333
|
+
|
|
334
|
+
1. Select appropriate UTXOs (inputs)
|
|
335
|
+
2. Calculate transaction fees
|
|
336
|
+
3. Handle change outputs
|
|
337
|
+
4. Manage different address types
|
|
338
|
+
|
|
339
|
+
Consider using specialized Bitcoin libraries or services for production applications.
|
|
340
|
+
|
|
341
|
+
## Browser Compatibility
|
|
342
|
+
|
|
343
|
+
This package is **100% browser-compatible**:
|
|
344
|
+
|
|
345
|
+
- ✅ Chrome, Firefox, Safari, Edge
|
|
346
|
+
- ✅ Node.js (v16+)
|
|
347
|
+
- ✅ Electron (main and renderer)
|
|
348
|
+
- ✅ React Native
|
|
349
|
+
- ✅ Web Workers
|
|
350
|
+
|
|
351
|
+
Uses only browser-safe cryptography with no Node.js dependencies.
|
|
352
|
+
|
|
353
|
+
## Examples
|
|
354
|
+
|
|
355
|
+
See working examples in:
|
|
356
|
+
- `examples/vue-electron-example/src/AuthTest.vue` - Complete integration with @cryptforge/auth
|
|
357
|
+
|
|
358
|
+
## License
|
|
40
359
|
|
|
41
|
-
|
|
360
|
+
MIT
|
|
42
361
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cryptforge/blockchain-btc",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Bitcoin blockchain adapter for CryptForge SDK",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -49,10 +49,10 @@
|
|
|
49
49
|
"@noble/secp256k1": "^2.0.0"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
|
-
"@cryptforge/core": "
|
|
52
|
+
"@cryptforge/core": "workspace:*"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
|
-
"@cryptforge/core": "
|
|
55
|
+
"@cryptforge/core": "workspace:*",
|
|
56
56
|
"tsup": "^8.0.1",
|
|
57
57
|
"typescript": "^5.3.3"
|
|
58
58
|
}
|