-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupWallet.ts
More file actions
115 lines (97 loc) · 4 KB
/
setupWallet.ts
File metadata and controls
115 lines (97 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { writeFileSync, existsSync } from 'fs'
import { PrivateKey, WalletClient, WalletProtocol, PublicKey, P2PKH, KeyDeriver, Utils } from '@bsv/sdk'
import { Wallet, WalletStorageManager, WalletSigner, Services, StorageClient } from '@bsv/wallet-toolbox'
import * as dotenv from 'dotenv'
const AMOUNT = 1000
const NETWORK = 'main'
const STORAGE_URL = 'https://storage.babbage.systems'
const brc29ProtocolID: WalletProtocol = [2, '3241645161d8']
async function getOrCreateBackendWallet(): Promise<{ wallet: Wallet, publicKey: PublicKey, address: string, isNew: boolean }> {
let privateKey: PrivateKey
let isNew = false
// Check if .env file exists and has a private key
if (existsSync('.env')) {
dotenv.config()
const existingKey = process.env.PRIVATE_KEY
if (existingKey) {
console.log('Found existing wallet in .env')
privateKey = PrivateKey.fromHex(existingKey)
} else {
console.log('No private key in .env, creating new wallet')
privateKey = PrivateKey.fromRandom()
writeFileSync('.env', `PRIVATE_KEY=${privateKey.toHex()}\nSTORAGE_URL=${STORAGE_URL}\nNETWORK=${NETWORK}\n`)
isNew = true
}
} else {
console.log('No .env file found, creating new wallet')
privateKey = PrivateKey.fromRandom()
writeFileSync('.env', `PRIVATE_KEY=${privateKey.toHex()}\nSTORAGE_URL=${STORAGE_URL}\nNETWORK=${NETWORK}\n`)
isNew = true
}
const publicKey = privateKey.toPublicKey()
const address = publicKey.toAddress()
const keyDeriver = new KeyDeriver(privateKey)
const storageManager = new WalletStorageManager(keyDeriver.identityKey)
const signer = new WalletSigner(NETWORK, keyDeriver, storageManager)
const services = new Services(NETWORK)
const wallet = new Wallet(signer, services)
const client = new StorageClient(wallet, STORAGE_URL)
await client.makeAvailable()
await storageManager.addWalletStorageProvider(client)
return { wallet, publicKey, address, isNew }
}
async function fundWallet() {
const { wallet, publicKey, address, isNew } = await getOrCreateBackendWallet()
if (isNew) {
console.log(`Created new backend wallet with address: ${address}`)
} else {
console.log(`Using existing backend wallet with address: ${address}`)
}
const localWallet = new WalletClient('json-api', 'localhost')
await localWallet.connectToSubstrate()
const derivationPrefix = Utils.toBase64(Utils.toArray('keyID', 'utf8'))
const derivationSuffix = Utils.toBase64(Utils.toArray('somethingIwontforget', 'utf8'))
const { publicKey: payer } = await localWallet.getPublicKey({ identityKey: true })
console.log('Payer identity:', payer)
const payee = publicKey.toString()
console.log('Payee identity:', payee)
const { publicKey: derivedPublicKey } = await localWallet.getPublicKey({
counterparty: payee,
protocolID: brc29ProtocolID,
keyID: `${derivationPrefix} ${derivationSuffix}`
})
const lockingScript = new P2PKH().lock(PublicKey.fromString(derivedPublicKey).toAddress()).toHex()
console.log(`Funding backend wallet with ${AMOUNT} satoshis...`)
const transaction = await localWallet.createAction({
outputs: [{
lockingScript,
satoshis: AMOUNT,
outputDescription: 'Fund backend wallet'
}],
description: 'Funding backend wallet with 10000 sats',
options: {
randomizeOutputs: false
}
})
console.log('Transaction created:', transaction.txid)
if (!transaction.tx) throw new Error('No transaction created')
const atomicBEEF = transaction.tx
await wallet.internalizeAction({
tx: atomicBEEF,
outputs: [{
outputIndex: 0,
protocol: 'wallet payment',
paymentRemittance: {
derivationPrefix,
derivationSuffix,
senderIdentityKey: payer
}
}],
description: 'Incoming wallet funding'
})
console.log(`\nSuccess! Backend wallet funded with ${AMOUNT} satoshis`)
console.log(`Address: ${address}`)
console.log(`TXID: ${transaction.txid}`)
console.log(`https://whatsonchain.com/tx/${transaction.txid}`)
}
fundWallet().catch(console.error)