import { Base58AddressFormatVersion , decodeBase58AddressFormat , encodeBase58AddressFormat , } from '../address/address' ; import { Sha256 } from '../crypto/crypto' ; export enum WalletImportFormatError { incorrectLength = 'The WIF private key payload is not the correct length.' } /**
 * The network and address format in which a WIF-encoded private key is expected
 * to be used.
 *
 * WIF-encoding is generally used to serialize private keys for Pay to Public
 * Key (P2PKH) addresses – each WIF-encoded private key specifies the
 * compression of the public key to use in the P2PKH address:
 *
 * - The values `mainnet` and `testnet` indicate that the address should use the
 * compressed form of the derived public key (33 bytes, beginning with `0x02` or
 * `0x03`) on the respective network.
 * - The less common `mainnet-uncompressed` and `testnet-uncompressed` values
 * indicate that the address should use the uncompressed form of the public key
 * (65 bytes beginning with `0x04`) on the specified network.
 */ export type WalletImportFormatType = 'mainnet' | 'testnet' | 'mainnet-uncompressed' | 'testnet-uncompressed' ; remarks param sha256 param privateKey param type export const encodePrivateKeyWif = ( sha256 , privateKey , type ) => { const compressedByte = 0x01 ; const mainnet = type === 'mainnet' || type === 'mainnet-uncompressed' ; const compressed = type === 'mainnet' || type === 'testnet' ; const payload = compressed ? Uint8Array . from ( [ ... privateKey , compressedByte ] ) : privateKey ; return encodeBase58AddressFormat ( sha256 , mainnet ? Base58AddressFormatVersion . wif : Base58AddressFormatVersion . wifTestnet , payload ) ; } ; param sha256 param wifKey export const decodePrivateKeyWif = ( sha256 , wifKey ) => { const compressedPayloadLength = 33 ; const decoded = decodeBase58AddressFormat ( sha256 , wifKey ) ; if ( typeof decoded === 'string' ) return decoded ; const mainnet = decoded . version === Base58AddressFormatVersion . wif ; const compressed = decoded . payload . length === compressedPayloadLength ; const privateKey = compressed ? decoded . payload . slice ( 0 , - 1 ) : decoded . payload ; const type = mainnet ? compressed ? 'mainnet' : 'mainnet-uncompressed' : compressed ? 'testnet' : 'testnet-uncompressed' ; return { privateKey , type } ; } ;