@blockchain-api/bitcoin-js
Version:
Blockchain Api Bitcoin JS
123 lines (104 loc) • 6.13 kB
text/typescript
import { generateAddressFromMnemonic, generateAddressFromPrivatekey, generateAddressFromXPub, generatePrivateKeyFromMnemonic, generateWallet, isValidAddress } from './doge'
const mnemonic = 'coyote nephew stable ghost enroll tunnel dizzy endorse garden know device funny swamp rural engine'
const accountExtPubkey = 'dgub8sQaZBJCDJREHoikKeArE5E1HVf7HTaRWGFYmt2ARMLY2LcTgWwvQkfrGTgmAF83bDU2CFhdrL5a9NcJ489mbyGZcr4sfimEuC6qTtTTUm1'
const accountExtPrivatekey = 'dgpv58XZDVNJf9ktzx4bHQYt7DgQpwHUTLYiDfULcmdBdEHXUHVmGkQ9mTGChESq92UfYvbn5dMGjCBmcvybGtd4FDN5iJZ5E9guFgisRhutgqy'
const bip32ExtPubkey = 'dgub8u5Uba7CXrXcVoBuYnQA9BzjkqZGnox94c6hCQUjD3NSFghfceccbxLbnEiXMwmygLJbrxQpsbsK7G1q6u3pcfS24NoUeNhpveumHxiixGW'
const bip32ExtPrivatekey = 'dgpv5ACTFtBJyhsHCwXkWYnC2LT9JHBdxgvRn1KV3J5kQvKRhdayCt4qxevxCzcSNPjbi7Vw8Ur4uqCegmCNpDjPAM4Ka9Acob6fXGo54K9CB32'
const privateKeyBip44AddressIndex0 = 'QUUaS3be4Sch5g41MkqoWTogyzHSkSvG1fPRqdfTdZ5ig2dTAkC5'
const bip44AddressIndex0 = 'DMiRzhXcVqAmHjDaVXxJXFVjbZn537Tynx'
const testnetAccountExtPubkey = 'tpubDCVKQML6TxHkPdWmtBWEHitrf9Gy24w7Ws8u4WoMfRG8mPT5ByjYfwhWgCMPJFqKgbPFVFmeBWiuZbt7y9tbUaR7ZzZca9dw8wbxg5yk2E1'
const testnetAccountExtPrivatekey = 'tprv8foHFwHrKac5WAUyzXqdtKEk67m2rjkCwZY7mzm4F9TjvuCJZauxVT5eW3VXje9PxNyee7CQjXqDgvzARLUzDgECWUfHzeKEXsKdWyHpixx'
const testnetBip32ExtPubkey = 'tpubDEh1QwGhPMp5PNWMzFF3shrWYN5saSmCJbKS1unFoQZ3QoAX1mxPB9243SjSEq81s9MB5p8hLbUp4EzkomV8GE6yfvPXtWse2QccAz1nG3B'
const testnetBip32ExtPrivatekey = 'tprv8hzyGXETEz8QVuUa6baTUJCPyLZwR7aHjHiejPjxP8keaJukPP8nzeQBsLSDLyqrGfb476xou2RNjwt2oHrTUQMYGpBhVVEPMpVYBqR3x5a'
const testnetPrivateKeyBip44AddressIndex0 = 'cgn6A9Pp55rJBLgNUfh3D3vu2uLQXrfN39rES4hzzJXD43JGGgJk'
const testnetBip44AddressIndex0 = 'nm8b7NVKZ2dQTCoWHo5JWVPqQSRaK53Q7p'
describe('mainnet', () => {
const network = "mainnet"
test('generateWallet', async () => {
const wallet = await generateWallet(network, mnemonic)
expect(wallet.mnemonic).toBe(mnemonic)
expect(wallet.accountExtPubkey).toBe(accountExtPubkey)
expect(wallet.accountExtPrivatekey).toBe(accountExtPrivatekey)
expect(wallet.bip32ExtPubkey).toBe(bip32ExtPubkey)
expect(wallet.bip32ExtPrivatekey).toBe(bip32ExtPrivatekey)
});
test('generateAddressFromMnemonic', async () => {
const address = await generateAddressFromMnemonic(network, mnemonic, 0)
expect(address).toBe(bip44AddressIndex0)
});
test('generateAddressFromPrivatekey', async () => {
const address = generateAddressFromPrivatekey(network, privateKeyBip44AddressIndex0)
expect(address).toBe(bip44AddressIndex0)
});
test('generatePrivateKeyFromMnemonic', async () => {
const privateKey = await generatePrivateKeyFromMnemonic(network, mnemonic, 0)
expect(privateKey).toBe(privateKeyBip44AddressIndex0)
});
test('generateAddressFromXPub', async () => {
const address = generateAddressFromXPub(network, bip32ExtPubkey, 0)
expect(address).toBe(bip44AddressIndex0)
});
test('isValidAddress', async () => {
const address1 = bip44AddressIndex0
const checkValidAddress1 = isValidAddress(network, address1)
const address5 = testnetBip44AddressIndex0
const checkValidAddress5 = isValidAddress(network, address5)
expect(checkValidAddress1).toBe(true)
expect(checkValidAddress5).toBe(false)
});
})
describe('testnet', () => {
const network = "testnet"
test('generateWallet', async () => {
const wallet = await generateWallet(network, mnemonic)
expect(wallet.mnemonic).toBe(mnemonic)
expect(wallet.accountExtPubkey).toBe(testnetAccountExtPubkey)
expect(wallet.accountExtPrivatekey).toBe(testnetAccountExtPrivatekey)
expect(wallet.bip32ExtPubkey).toBe(testnetBip32ExtPubkey)
expect(wallet.bip32ExtPrivatekey).toBe(testnetBip32ExtPrivatekey)
});
test('generateAddressFromMnemonic', async () => {
const address = await generateAddressFromMnemonic(network, mnemonic, 0)
expect(address).toBe(testnetBip44AddressIndex0)
});
test('generateAddressFromPrivatekey', async () => {
const address = generateAddressFromPrivatekey(network, testnetPrivateKeyBip44AddressIndex0)
expect(address).toBe(testnetBip44AddressIndex0)
});
test('generatePrivateKeyFromMnemonic', async () => {
const privateKey = await generatePrivateKeyFromMnemonic(network, mnemonic, 0)
expect(privateKey).toBe(testnetPrivateKeyBip44AddressIndex0)
});
test('generateAddressFromXPub', async () => {
const address = generateAddressFromXPub(network, testnetBip32ExtPubkey, 0)
expect(address).toBe(testnetBip44AddressIndex0)
});
test('isValidAddress', async () => {
const address1 = testnetBip44AddressIndex0
const checkValidAddress1 = isValidAddress(network, address1)
const address5 = bip44AddressIndex0
const checkValidAddress5 = isValidAddress(network, address5)
expect(checkValidAddress1).toBe(true)
expect(checkValidAddress5).toBe(false)
});
})
// describe('transaction', () => {
// test('signTxOffline by address', async () => {
// const outputs = [{
// txHash: '0c6b596c8d77ef84b9d75f0978e0a8add3464712c356f06f234564498501131e',
// index: 0,
// script: 'a9143e410755b5375e8fdf9bd0f4ee944f22c7c2060587',
// value: 5000661330
// } as UTXO]
// signTxOffline("mainnet", outputs, privateKeyBip44AddressIndex0, bip44AddressIndex0, 0.001)
// });
// test('signTxOffline by address testnet', async () => {
// const outputs = [{
// txHash: 'e1433822a5df48c21bc68de00efbd9c7e5c3d0857ccf9d045c1baf7c50d515a3',
// index: 0,
// script: 'a9143e410755b5375e8fdf9bd0f4ee944f22c7c2060587',
// value: 3796821615
// } as UTXO]
// signTxOffline("testnet", outputs, testnetPrivateKeyBip44AddressIndex0, testnetBip44AddressIndex0, 0.001)
// });
// })