Table of Contents
Create your own wallet
Prerequisites
Setup your block source or have a URL to a remote node
Electrum and Bitcoin Core
Latest version of Node.js
Guide
1. Wallet seed/mnemonic
In WDK, we use 1 seed phrase for all assets. To learn more
Copy const BIP39Seed = require('wallet-seed-bip39')
const seed = await BIP39Seed.generate()
To reuse an existing seed phrase from a different wallet:
Copy const seed = await BIP39Seed.generate(<seed phrase here>)
The seed
is now ready to be used to secure your assets.
2. Database
In WDK we use a database to keep track of the overall state of the wallet. This includes things like balances, past transactions, addresses etc.
Copy const store = new WalletStoreHyperbee({
store_path: './path-to-data-dir'
})
await store.init()
If you do not want persistance storage and want to use in memory storage leave the store_path
field as empty.
3. Setup assets
Each blockchain/asset has its own module that encapsulates all of the logic. Each asset can have its own configuration.
Lets setup Bitcoin:
Copy const { BitcoinPay } = require('lib-wallet-pay-btc')
const btcPay = new BitcoinPay({
// Asset name space
asset_name: 'btc',
// Asset's network
network: 'regtest',
electrum: {
// optional TCP to Websocket adaptor. This will allow you to connect to a websocket electrum node
net: require('./modules/ws-net.js'),
host: "electrum-websocket host"
port: "electrum-websocket port"
}
})
Let's set-up Ethereum and USDt
Copy const { EthPay, Provider } = require('lib-wallet-pay-eth')
const { TetherCurrency } = require('lib-wallet')
// Ethereum data provider setup
const provider = new Provider({
web3: "web3 endpoint"
indexer: "web3 indexer rpc endpoint"
indexerWs: "web3 indexer websocket endpoint"
})
await provider.init()
// ERC20 class: encapsulates logic for any ERC20 token
// erc20CurrencyFacade: Generate Erc20 Currency class, that encapsulates params like contract address for an ERC 20
// TetherCurrency.ERC20(): Erc20 USDT mainnet configuration:
const USDT = new ERC20({
currency: erc20CurrencyFac(TetherCurrency.ERC20())
})
const ethPay = new EthPay({
asset_name: 'eth',
provider,
network : 'sepolia'
token: [
USDT
]
})
You've now setup Ethereum and USDt. You can now generate addresses and send and receive funds.
Putting it all together
We configure the main Wallet class with the assets we want to use:
Copy const wallet = new Wallet({
store,
seed,
assets: [btcPay, ethPay]
})
await wallet.initialize()
The wallet is now setup and ready to be used.
Use your wallet.
Generate addresses:
Copy // Generate Bitcoin address
await wallet.pay.btc.getNewAddress()
// Generate ETH / USDt address
await wallet.pay.eth.getNewAddress()
Sync your wallet
WDK automatically listens to new incoming transactions for your latest transactions if you are online. When you close and reopen the wallet, you need to resync with the blockchain.
Copy await wallet.syncHistory()
Check out some of the simple APIs available for building a wallet.
Copy await wallet.pay.btc({}, {
address: <recipient>,
amount: <quantity of bitcoin>,
unit: <main or base> // main = bitcoin base = satoshis.
fee: <in sats per bytes>
})
// Example: send 0,1 Bitcoin with 10 satsVbyte in fees
await wallet.pay.btc({}, {
address: <recipient>,
amount: 0.1,
unit: 'base',
fee: 10
})
Sending USDt on Ethereum
Copy await wallet.pay.eth({
token : 'USDT'
}, {
address: <recipient>,
amount: <quanity of USDT>,
unit: 'main',
})
Wallet history
Transaction history now works via an iterator.
Copy // Get USDT transactions history.
await wallet.pay.eth.getTransactions({
token : 'USDT',
}, (tx) => {
// iterate through tx history
})