72 lines
3.3 KiB
JavaScript
72 lines
3.3 KiB
JavaScript
|
|
import { ECPairFactory } from 'ecpair';
|
||
|
|
import * as ecc from '@bitcoinerlab/secp256k1';
|
||
|
|
import { payments, initEccLib } from 'bitcoinjs-lib';
|
||
|
|
import { digibyte, digibyteLegacyP2SH, digibyteLegacyWIF } from './network.js';
|
||
|
|
const ECPair = ECPairFactory(ecc);
|
||
|
|
initEccLib(ecc);
|
||
|
|
// Import a DigiByte private key in WIF (Wallet Import Format).
|
||
|
|
// Tries the modern 0x80 prefix first, falls back to the legacy 0x9e
|
||
|
|
// prefix that older DGB tools produced. Rejects anything else with a
|
||
|
|
// clear message that identifies the network mismatch.
|
||
|
|
export function importWif(wif) {
|
||
|
|
const trimmed = wif.trim();
|
||
|
|
try {
|
||
|
|
return { keyPair: ECPair.fromWIF(trimmed, digibyte), variant: 'modern' };
|
||
|
|
}
|
||
|
|
catch {
|
||
|
|
// fall through to legacy attempt
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
return { keyPair: ECPair.fromWIF(trimmed, digibyteLegacyWIF), variant: 'legacy' };
|
||
|
|
}
|
||
|
|
catch (e) {
|
||
|
|
throw new Error(`Not a valid DigiByte WIF (tried both current 0x80 and legacy 0x9e prefixes). ` +
|
||
|
|
`Underlying error: ${e.message}. Check the key is for DGB mainnet, not testnet or another chain.`);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
// Import against a specific network only (advanced / testing).
|
||
|
|
export function importWifStrict(wif, network = digibyte) {
|
||
|
|
return ECPair.fromWIF(wif.trim(), network);
|
||
|
|
}
|
||
|
|
export function exportWif(keyPair) {
|
||
|
|
return keyPair.toWIF();
|
||
|
|
}
|
||
|
|
export function addressesForPubkey(pubkey, network = digibyte) {
|
||
|
|
const p2pkh = payments.p2pkh({ pubkey, network });
|
||
|
|
const wpkhRedeem = payments.p2wpkh({ pubkey, network });
|
||
|
|
// bitcoinjs-lib enforces `redeem.network === outerNetwork` (identity
|
||
|
|
// comparison, not shape). To render the legacy 3-prefix P2SH address
|
||
|
|
// we need a fresh redeem whose .network property is the legacy variant
|
||
|
|
// — same bytes on the wire, different object identity.
|
||
|
|
const wpkhRedeemLegacy = payments.p2wpkh({ pubkey, network: digibyteLegacyP2SH });
|
||
|
|
const p2shModern = payments.p2sh({ redeem: wpkhRedeem, network });
|
||
|
|
const p2shLegacy = payments.p2sh({ redeem: wpkhRedeemLegacy, network: digibyteLegacyP2SH });
|
||
|
|
const p2wpkh = payments.p2wpkh({ pubkey, network });
|
||
|
|
const p2tr = payments.p2tr({ internalPubkey: pubkey.subarray(1, 33), network });
|
||
|
|
if (!p2pkh.address || !p2shModern.address || !p2shLegacy.address || !p2wpkh.address || !p2tr.address) {
|
||
|
|
throw new Error('bitcoinjs-lib returned an empty address for one of the payment types');
|
||
|
|
}
|
||
|
|
if (!p2pkh.output || !p2shModern.output || !p2wpkh.output || !p2tr.output) {
|
||
|
|
throw new Error('bitcoinjs-lib returned an empty scriptPubKey for one of the payment types');
|
||
|
|
}
|
||
|
|
return {
|
||
|
|
p2pkh: p2pkh.address,
|
||
|
|
p2shP2wpkhModern: p2shModern.address,
|
||
|
|
p2shP2wpkhLegacy: p2shLegacy.address,
|
||
|
|
p2wpkh: p2wpkh.address,
|
||
|
|
p2tr: p2tr.address,
|
||
|
|
scripts: {
|
||
|
|
p2pkh: Buffer.from(p2pkh.output).toString('hex'),
|
||
|
|
p2shP2wpkh: Buffer.from(p2shModern.output).toString('hex'),
|
||
|
|
p2wpkh: Buffer.from(p2wpkh.output).toString('hex'),
|
||
|
|
p2tr: Buffer.from(p2tr.output).toString('hex'),
|
||
|
|
},
|
||
|
|
};
|
||
|
|
}
|
||
|
|
export function importWifWithAddresses(wif) {
|
||
|
|
const imported = importWif(wif);
|
||
|
|
const pubkey = Buffer.from(imported.keyPair.publicKey);
|
||
|
|
const addrs = addressesForPubkey(pubkey);
|
||
|
|
return { ...imported, ...addrs };
|
||
|
|
}
|
||
|
|
//# sourceMappingURL=wif.js.map
|