UNPKG

1.02 kBPlain TextView Raw
1/**
2 * @hidden
3 */
4
5import * as bip32 from 'bip32';
6import * as common from './common';
7import * as utxolib from '@bitgo/utxo-lib';
8import { V1Network } from './v2/types';
9
10export function getNetwork(network?: V1Network): utxolib.Network {
11 network = network || common.getNetwork();
12 return utxolib.networks[network];
13}
14
15export function makeRandomKey(): utxolib.ECPair.ECPairInterface {
16 return utxolib.ECPair.makeRandom({ network: getNetwork() as utxolib.BitcoinJSNetwork });
17}
18
19interface LegacyECPair {
20 network: utxolib.Network;
21 getPublicKeyBuffer(): Buffer;
22}
23
24export function getAddressP2PKH(key: utxolib.ECPair.ECPairInterface | bip32.BIP32Interface | LegacyECPair): string {
25 let pubkey;
26 if ('getPublicKeyBuffer' in key) {
27 pubkey = key.getPublicKeyBuffer();
28 } else {
29 pubkey = key.publicKey;
30 }
31 const { address } = utxolib.payments.p2pkh({ pubkey, network: key.network as utxolib.BitcoinJSNetwork });
32 if (!address) {
33 throw new Error('could not compute address');
34 }
35 return address;
36}