import { BitcoinError , BitcoinErrorCode , Network } from "." ; import { Base58Check } from "./Base58Check" ; export type WifDecodeResult = { privateKey : Buffer ; compressed : boolean ; prefix : number ; } ; export class Wif { param prefix returns public static decodePrefix ( prefix ) { for ( const network of Network . all ) { if ( network . wifPrefix === prefix ) return network ; } throw new BitcoinError ( BitcoinErrorCode . UnknownWifPrefix , { prefix } ) ; } param prefix param privateKey param compressed public static encode ( prefix , privateKey , compressed = true ) { const prefixBuf = Buffer . from ( [ prefix ] ) ; const suffix = compressed ? Buffer . from ( [ 0x01 ] ) : Buffer . alloc ( 0 ) ; return Base58Check . encode ( Buffer . concat ( [ prefixBuf , privateKey , suffix ] ) ) ; } param buf public static decode ( input ) { const raw = Base58Check . decode ( input ) ; if ( raw . length !== 33 && raw . length !== 34 ) { throw new BitcoinError ( BitcoinErrorCode . InvalidWifEncoding , { input } ) ; } const prefix = raw [ 0 ] ; const privateKey = raw . slice ( 1 , 33 ) ; const compressed = raw . length === 34 && raw [ 33 ] === 0x01 ; return { privateKey , compressed , prefix } ; } }