Skip to content

Commit

Permalink
US-NO-REF Added more tests for bitcoin to already existing tests (#111)
Browse files Browse the repository at this point in the history
* US-NO-REF Added more tests for bitcoin to already existing tests

* BIP.test.ts

* BIP.test.ts improved tests
  • Loading branch information
Freshenext authored Dec 4, 2023
1 parent 4db2769 commit 5a721ab
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 28 deletions.
60 changes: 35 additions & 25 deletions packages/bitcoin/test/BIP.test.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@
import { BitcoinNetwork } from '../src'
import { mnemonic } from './testSharedConfig'
import { mnemonic, mnemonicAddressesMainnet, mnemonicAddressesTestnet } from './testSharedConfig'
import * as constants from '../src/constants'

describe('BIP Class tests', () => {
const bitcoinNetworkInstance = new BitcoinNetwork(
constants.NETWORK_ID.BITCOIN_TESTNET,
[constants.BIP_ID.BIP84, constants.BIP_ID.BIP44],
mnemonic
)
/**
* Helper function to run many bip tests easily
* @param bipType
* @param bipField
* @param pathField
* @param networkInstance
* @param mnemonicAddresses
* @param NETWORK
*/
function runBipTests(bipType: 'BIP84' | 'BIP44', bipField: 'bip84_address' | 'bip44_address', pathField: 'path_84' | 'path_44', networkInstance: BitcoinNetwork, mnemonicAddresses: typeof mnemonicAddressesTestnet, NETWORK: string) {
describe(`NETWORK ${NETWORK} ${bipType} tests with mnemonic ${mnemonic}`, () => {
const bip = networkInstance.bipNames[bipType]

describe(`BIP84 tests with mnemonic ${mnemonic}`, () => {
const bip84 = bitcoinNetworkInstance.bipNames.BIP84

it('Should generate the address of index m/84\'/1\'/0\'/0/0 ', () => {
const address = bip84.getAddress(0)
expect(address).toBe('tb1qfymt85557xnjm04wtg839vrmq6jx8njh05vhmm')
mnemonicAddresses.forEach(mnemonicAddress => {
it(`Should generate the address ${mnemonicAddress[bipField]} of path ${mnemonicAddress[pathField]} correctly`, () => {
const address = bip.getAddress(mnemonicAddress.index, mnemonicAddress.change)
expect(address).toBe(mnemonicAddress[bipField])
})
})

it('Should try to get the xpub balance and set it to 0 (fetcher is mocked)', async () => {
const balance = await bip84.fetchBalance()
const balance = await bip.fetchBalance()
expect(balance).toBe(0)
})
})
}

describe(`BIP44 tests with mnemonic ${mnemonic}`, () => {
const bip44 = bitcoinNetworkInstance.bipNames.BIP44
it('Should generate the address of index m/44\'/1\'/0\'/0/0 ', () => {
const address = bip44.getAddress(0)
expect(address).toBe('mzo88FnZi9F1bSHnKZuyosaVbpYJWoZHJX')
})
describe('BIP Class tests', () => {
const bitcoinNetworkInstanceTestnet = new BitcoinNetwork(
constants.NETWORK_ID.BITCOIN_TESTNET,
[constants.BIP_ID.BIP84, constants.BIP_ID.BIP44],
mnemonic
)
const bitcoinNetworkInstanceMainnet = new BitcoinNetwork(
constants.NETWORK_ID.BITCOIN,
[constants.BIP_ID.BIP84, constants.BIP_ID.BIP44],
mnemonic
)

it('Should try to get the xpub balance and set it to 0 (fetcher is mocked)', async () => {
const balance = await bip44.fetchBalance()
expect(balance).toBe(0)
})
})
runBipTests('BIP84', 'bip84_address', 'path_84', bitcoinNetworkInstanceMainnet, mnemonicAddressesMainnet, constants.NETWORK_ID.BITCOIN)
runBipTests('BIP44', 'bip44_address', 'path_44', bitcoinNetworkInstanceMainnet, mnemonicAddressesMainnet, constants.NETWORK_ID.BITCOIN)
runBipTests('BIP84', 'bip84_address', 'path_84', bitcoinNetworkInstanceTestnet, mnemonicAddressesTestnet, constants.NETWORK_ID.BITCOIN_TESTNET)
runBipTests('BIP44', 'bip44_address', 'path_44', bitcoinNetworkInstanceTestnet, mnemonicAddressesTestnet, constants.NETWORK_ID.BITCOIN_TESTNET)
})
105 changes: 102 additions & 3 deletions packages/bitcoin/test/testSharedConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,103 @@
import { BIP39 } from '../src'

export const mnemonic = 'rely truly issue ghost elder intact kiss provide project hobby thumb thing blur slender true'
export const bip39Instance = new BIP39(mnemonic)

export const mnemonicAddressesTestnet = [
{
path_84: 'm/84\'/1\'/0\'/0/0',
path_44: 'm/44\'/1\'/0\'/0/0',
bip84_address: 'tb1qfymt85557xnjm04wtg839vrmq6jx8njh05vhmm',
bip44_address: 'mzo88FnZi9F1bSHnKZuyosaVbpYJWoZHJX',
change: 0,
index: 0,
},
{
path_84: 'm/84\'/1\'/0\'/0/5',
path_44: 'm/44\'/1\'/0\'/0/5',
bip84_address: 'tb1qz33avpr9jan0tqd7w5sd7kppyr7uc43npy8zjg',
bip44_address: 'mvk3MgtaDJ39hWigMfpEUPNmFiSW8mt4i9',
change: 0,
index: 5,
},
{
path_84: 'm/84\'/1\'/0\'/0/9',
path_44: 'm/44\'/1\'/0\'/0/9',
bip84_address: 'tb1qv6pjz0ysep3kxkj7hjqeyfy9s00kfnws7wrs8w',
bip44_address: 'n15BnwPsKngJU2ihRsS5BEHw4jDqdJGhqj',
change: 0,
index: 9,
},
{
path_84: 'm/84\'/1\'/0\'/1/0',
path_44: 'm/44\'/1\'/0\'/1/0',
bip84_address: 'tb1qty8nustsu48vqzq52378cehlg7xjg0mt08te6j',
bip44_address: 'n4MvgNUY4qjkGrE3uK2ioz9EBaXkSmNrZY',
change: 1,
index: 0
},
{
path_84: 'm/84\'/1\'/0\'/1/5',
path_44: 'm/44\'/1\'/0\'/1/5',
bip84_address: 'tb1qe9rhszcqhmlu3wfdlj6ew89h5xqfmqr4slu3vr',
bip44_address: 'mtGkjjY9qWUSV49bqc3angkxrWC1423WeX',
change: 1,
index: 5
},
{
path_84: 'm/84\'/1\'/0\'/1/9',
path_44: 'm/44\'/1\'/0\'/1/9',
bip84_address: 'tb1qlu5fr7exmn5nfhv4m8xj2znshvqfx2majhshd9',
bip44_address: 'mngVGGUbv8aSe1YTbET2WgcW44jjE2NCZu',
change: 1,
index: 9
},
]

export const mnemonicAddressesMainnet = [
{
path_84: 'm/84\'/0\'/0\'/0/0',
path_44: 'm/44\'/0\'/0\'/0/0',
bip84_address: 'bc1q9grrnz75r80wkqdc48qxm9tuc9w8ant92d3fey',
bip44_address: '15TkjxRFPvthP8deUyxgXsTiPznremJSJa',
change: 0,
index: 0,
},
{
path_84: 'm/84\'/0\'/0\'/0/5',
path_44: 'm/44\'/0\'/0\'/0/5',
bip84_address: 'bc1qec5gm4n7ewh0hy62et4fuuf9a98p6mxnxtgd4j',
bip44_address: '1Ex3pcrwDmBFa5WhjjnT6EVDUAqpq1DEyn',
change: 0,
index: 5,
},
{
path_84: 'm/84\'/0\'/0\'/0/9',
path_44: 'm/44\'/0\'/0\'/0/9',
bip84_address: 'bc1qndst98jc9jmz3gpx5yn9vf7sfnfcr7l07xcnr4',
bip44_address: '15RSVBHptfJCKRaULZyj6KAJMEgCH7h4Ws',
change: 0,
index: 9,
},
{
path_84: 'm/84\'/0\'/0\'/1/0',
path_44: 'm/44\'/0\'/0\'/1/0',
bip84_address: 'bc1qlc58uxfdzvlggwz3nt749uzfx4pf9qg4ue0j37',
bip44_address: '12b8ZpAJXeXU2Hv2RBTuaaacbdsxSrbfMW',
change: 1,
index: 0
},
{
path_84: 'm/84\'/0\'/0\'/1/5',
path_44: 'm/44\'/0\'/0\'/1/5',
bip84_address: 'bc1qnvnqccfwzajk3w7n29cmesdzkhe6eejkfvfsce',
bip44_address: '13H49p7yKMv3J7QfmCRDsv513MzbAPbvmc',
change: 1,
index: 5
},
{
path_84: 'm/84\'/0\'/0\'/1/9',
path_44: 'm/44\'/0\'/0\'/1/9',
bip84_address: 'bc1qhrvy3ur8nd5nxp9ykc64aw8r5ymr3g7ssez8wn',
bip44_address: '14h3nACmrbFxuTsJV2nzLyycw1jEBrT8qr',
change: 1,
index: 9
},
]

0 comments on commit 5a721ab

Please sign in to comment.