@@ -10,13 +10,25 @@ import * as utxolib from '@bitgo/utxo-lib';
1010
1111import { _apiFallbacks } from '../fallbacks/btc' ;
1212import { fallback , retryNTimes } from '../utils/retry' ;
13- import { GetTransactionPayload , TransferPayload } from '../utils/types' ;
13+ import {
14+ BalancePayload ,
15+ CreateWalletPayload ,
16+ GenerateWalletFromMnemonicPayload ,
17+ GetAddressFromPrivateKeyPayload ,
18+ GetTransactionPayload ,
19+ IResponse ,
20+ Network ,
21+ TransferPayload ,
22+ } from '../utils/types' ;
1423import { BitgoUTXOLib } from '../libs/bitgoUtxoLib' ;
1524
1625const bip32 = BIP32Factory ( ecc ) ;
1726const ECPair = ECPairFactory ( ecc ) ;
1827
19- const createWallet = ( network : string , derivationPath ?: string ) => {
28+ const createWallet = ( {
29+ network,
30+ derivationPath,
31+ } : CreateWalletPayload ) : IResponse => {
2032 if ( derivationPath ) {
2133 const purpose = derivationPath ?. split ( '/' ) [ 1 ] ;
2234 if ( purpose !== "44'" ) {
@@ -30,12 +42,7 @@ const createWallet = (network: string, derivationPath?: string) => {
3042
3143 const node = bip32 . fromSeed ( seed ) ;
3244 const child = node . derivePath ( path ) ;
33- const actualNetwork =
34- network === 'bitcoin'
35- ? bitcoin . networks . bitcoin
36- : network === 'bitcoin-testnet'
37- ? bitcoin . networks . testnet
38- : bitcoin . networks . bitcoin ;
45+ const actualNetwork = getNetwork ( network ) ;
3946
4047 const { address } = bitcoin . payments . p2pkh ( {
4148 pubkey : child . publicKey ,
@@ -51,11 +58,11 @@ const createWallet = (network: string, derivationPath?: string) => {
5158 } ) ;
5259} ;
5360
54- const generateWalletFromMnemonic = (
55- network : string ,
56- mnemonic : string ,
57- derivationPath ?: string
58- ) => {
61+ const generateWalletFromMnemonic = ( {
62+ network,
63+ mnemonic,
64+ derivationPath,
65+ } : GenerateWalletFromMnemonicPayload ) : IResponse => {
5966 if ( derivationPath ) {
6067 const purpose = derivationPath ?. split ( '/' ) [ 1 ] ;
6168 if ( purpose !== "44'" ) {
@@ -68,12 +75,7 @@ const generateWalletFromMnemonic = (
6875
6976 const node = bip32 . fromSeed ( seed ) ;
7077 const child = node . derivePath ( path ) ;
71- const actualNetwork =
72- network === 'bitcoin'
73- ? bitcoin . networks . bitcoin
74- : network === 'bitcoin-testnet'
75- ? bitcoin . networks . testnet
76- : bitcoin . networks . bitcoin ;
78+ const actualNetwork = getNetwork ( network ) ;
7779
7880 const { address } = bitcoin . payments . p2pkh ( {
7981 pubkey : child . publicKey ,
@@ -89,13 +91,11 @@ const generateWalletFromMnemonic = (
8991 } ) ;
9092} ;
9193
92- const getAddressFromPrivateKey = ( privateKey : string , network : string ) => {
93- const actualNetwork =
94- network === 'bitcoin'
95- ? bitcoin . networks . bitcoin
96- : network === 'bitcoin-testnet'
97- ? bitcoin . networks . testnet
98- : bitcoin . networks . bitcoin ;
94+ const getAddressFromPrivateKey = ( {
95+ privateKey,
96+ network,
97+ } : GetAddressFromPrivateKeyPayload ) : IResponse => {
98+ const actualNetwork = getNetwork ( network ) ;
9999
100100 const keyPair = ECPair . fromWIF ( privateKey ) ;
101101
@@ -109,9 +109,11 @@ const getAddressFromPrivateKey = (privateKey: string, network: string) => {
109109 } ) ;
110110} ;
111111
112- const getBalance = async ( address : string , network : string ) => {
113- const testnet =
114- network === 'bitcoin' ? false : network === 'bitcoin-testnet' ? true : true ;
112+ const getBalance = async ( {
113+ address,
114+ network,
115+ } : BalancePayload ) : Promise < IResponse > => {
116+ const testnet = isTestnet ( network ) ;
115117
116118 const endpoints = _apiFallbacks . fetchUTXOs ( testnet , address , 0 ) ;
117119 const utxos = await fallback ( endpoints ) ;
@@ -125,13 +127,8 @@ const getBalance = async (address: string, network: string) => {
125127 } ) ;
126128} ;
127129
128- const transfer = async ( args : TransferPayload ) => {
129- const testnet =
130- args . network === 'bitcoin'
131- ? false
132- : args . network === 'bitcoin-testnet'
133- ? true
134- : true ;
130+ const transfer = async ( args : TransferPayload ) : Promise < IResponse > => {
131+ const testnet = isTestnet ( args . network ) ;
135132
136133 const keyPair = ECPair . fromWIF ( args . privateKey ) ;
137134
@@ -142,8 +139,10 @@ const transfer = async (args: TransferPayload) => {
142139 : utxolib . networks . testnet
143140 ) ;
144141
145- const fromAddress = getAddressFromPrivateKey ( args . privateKey , args . network )
146- . address ;
142+ const fromAddress = getAddressFromPrivateKey ( {
143+ privateKey : args . privateKey ,
144+ network : args . network ,
145+ } ) . address ;
147146
148147 const changeAddress = fromAddress ;
149148 const endpoints = _apiFallbacks . fetchUTXOs ( testnet , fromAddress , 0 ) ;
@@ -196,14 +195,15 @@ const transfer = async (args: TransferPayload) => {
196195 }
197196} ;
198197
199- const getTransaction = async ( { hash, network } : GetTransactionPayload ) => {
200- const testnet =
201- network === 'bitcoin' ? false : network === 'bitcoin-testnet' ? true : true ;
198+ const getTransaction = async ( {
199+ hash,
200+ network,
201+ } : GetTransactionPayload ) : Promise < IResponse > => {
202+ const testnet = isTestnet ( network ) ;
202203
203204 const transaction = await fallback ( _apiFallbacks . fetchUTXO ( testnet , hash , 0 ) ) ;
204205 const bigAmount = new BigNumber ( transaction . amount ) ;
205206
206- // Convert amount from Satoshi to Bitcoin
207207 const amount = bigAmount . dividedBy ( new BigNumber ( 10 ) . exponentiatedBy ( 8 ) ) ;
208208
209209 return successResponse ( {
@@ -212,6 +212,18 @@ const getTransaction = async ({ hash, network }: GetTransactionPayload) => {
212212 } ) ;
213213} ;
214214
215+ function getNetwork ( network : Network ) {
216+ return network === 'bitcoin'
217+ ? bitcoin . networks . bitcoin
218+ : network === 'bitcoin-testnet'
219+ ? bitcoin . networks . testnet
220+ : bitcoin . networks . bitcoin ;
221+ }
222+
223+ function isTestnet ( network : Network ) {
224+ return network === 'bitcoin-testnet' ;
225+ }
226+
215227export default {
216228 createWallet,
217229 generateWalletFromMnemonic,
0 commit comments