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