1 |
|
2 |
|
3 |
|
4 |
|
5 | import * as bip32 from 'bip32';
|
6 | import * as common from './common';
|
7 | import * as utxolib from '@bitgo/utxo-lib';
|
8 | import { V1Network } from './v2/types';
|
9 |
|
10 | export function getNetwork(network?: V1Network): utxolib.Network {
|
11 | network = network || common.getNetwork();
|
12 | return utxolib.networks[network];
|
13 | }
|
14 |
|
15 | export function makeRandomKey(): utxolib.ECPair.ECPairInterface {
|
16 | return utxolib.ECPair.makeRandom({ network: getNetwork() as utxolib.BitcoinJSNetwork });
|
17 | }
|
18 |
|
19 | interface LegacyECPair {
|
20 | network: utxolib.Network;
|
21 | getPublicKeyBuffer(): Buffer;
|
22 | }
|
23 |
|
24 | export 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 | }
|