-
Notifications
You must be signed in to change notification settings - Fork 1
/
p2pkh.js
16 lines (13 loc) · 923 Bytes
/
p2pkh.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//This function will generate Legacy bitcoin address (public key hash) using public key
const generateLegacyAddress = (publicKey) => {
const keyHex = CryptoJS.enc.Hex.parse(publicKey);
const ripedHashedKey = CryptoJS.RIPEMD160(CryptoJS.SHA256(keyHex)).toString();
const mainRipeKeyString = '00'+ripedHashedKey;
const mainRipeKey = CryptoJS.enc.Hex.parse(mainRipeKeyString);
const doubleHashedKey = CryptoJS.SHA256(CryptoJS.SHA256(mainRipeKey)).toString();
const checkSum = doubleHashedKey.substr(0, 8);
const binaryAddress = mainRipeKeyString+checkSum;
const arrayBinary = binaryAddress.match(/.{1,2}/g); //Converting serialization into array of every 2nd character
const binaryUint = new Uint8Array(arrayBinary.map(hex => parseInt(hex,16))); //Converting hex array into uint8array to be used as input in base58 function
return to_b58(binaryUint,bs58Chars);
}