file import base58 from "bs58" ; import { Buffer } from "buffer" ; import WIF from "wif" ; import { ADDR_VERSION } from "../consts" ; import { ab2hexstring , hash160 , hash256 , hexstring2ab , reverseHex } from "../u" ; import { generateRandomArray } from "../u/random" ; import { curve , sign } from "./signing" ; param unencodedKey return export function getPublicKeyEncoded ( unencodedKey ) { const publicKeyArray = new Uint8Array ( hexstring2ab ( unencodedKey ) ) ; if ( publicKeyArray [ 64 ] % 2 === 1 ) { return "03" + ab2hexstring ( publicKeyArray . slice ( 1 , 33 ) ) ; } else { return "02" + ab2hexstring ( publicKeyArray . slice ( 1 , 33 ) ) ; } } param publicKey return export function getPublicKeyUnencoded ( publicKey ) { const publicKeyBuffer = Buffer . from ( publicKey , "hex" ) ; const keyPair = curve . keyFromPublic ( publicKeyBuffer , "hex" ) ; return keyPair . getPublic ( ) . encode ( "hex" , false ) ; } /**
 * Converts a WIF to private key.
 */ export function getPrivateKeyFromWIF ( wif ) { return ab2hexstring ( WIF . decode ( wif , 128 ) . privateKey ) ; } /**
 * Converts a private key to WIF.
 */ export function getWIFFromPrivateKey ( privateKey ) { return WIF . encode ( 128 , Buffer . from ( privateKey , "hex" ) , true ) ; } param privateKey param encode export function getPublicKeyFromPrivateKey ( privateKey , encode = true ) { const privateKeyBuffer = Buffer . from ( privateKey , "hex" ) ; const keypair = curve . keyFromPrivate ( privateKeyBuffer , "hex" ) ; const unencodedPubKey = keypair . getPublic ( ) . encode ( "hex" , false ) ; if ( encode ) { const tail = parseInt ( unencodedPubKey . substr ( 64 * 2 , 2 ) , 16 ) ; if ( tail % 2 === 1 ) { return "03" + unencodedPubKey . substr ( 2 , 64 ) ; } else { return "02" + unencodedPubKey . substr ( 2 , 64 ) ; } } else { return unencodedPubKey ; } } /**
 * Converts a public key to verification script form.
 * VerificationScript serves a very niche purpose.
 * It is attached as part of the signature when signing a transaction.
 * Thus, the name 'scriptHash' instead of 'keyHash' is because we are hashing the verificationScript and not the PublicKey.
 */ export const getVerificationScriptFromPublicKey = ( publicKey ) : string => { return "21" + publicKey + "ac" ; } ; /**
 * Converts a public key to scripthash.
 */ export const getScriptHashFromPublicKey = ( publicKey ) : string => { if ( publicKey . substring ( 0 , 2 ) === "04" ) { publicKey = getPublicKeyEncoded ( publicKey ) ; } const verificationScript = getVerificationScriptFromPublicKey ( publicKey ) ; return reverseHex ( hash160 ( verificationScript ) ) ; } ; /**
 * Converts a scripthash to address.
 */ export const getAddressFromScriptHash = ( scriptHash ) : string => { scriptHash = reverseHex ( scriptHash ) ; const shaChecksum = hash256 ( ADDR_VERSION + scriptHash ) . substr ( 0 , 8 ) ; return base58 . encode ( Buffer . from ( ADDR_VERSION + scriptHash + shaChecksum , "hex" ) ) ; } ; /**
 * Converts an address to scripthash.
 */ export const getScriptHashFromAddress = ( address ) : string => { const hash = ab2hexstring ( base58 . decode ( address ) ) ; return reverseHex ( hash . substr ( 2 , 40 ) ) ; } ; param tx param privateKey return export const generateSignature = ( tx , privateKey ) : string => { return sign ( tx , privateKey ) ; } ; /**
 * Generates a random private key.
 */ export const generatePrivateKey = ( ) : string => { return ab2hexstring ( generateRandomArray ( 32 ) ) ; } ;