UNPKG

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