Last active
April 13, 2021 19:11
-
-
Save hoonsubin/ab4dea2a96c0c8c3675ccc930026ae06 to your computer and use it in GitHub Desktop.
generates a Plasm public address with the given ECDSA public key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* generates a Plasm public address with the given ethereum public key | |
* @param ethPubKey an compressed ECDSA public key. With or without the 0x prefix | |
*/ | |
export function generatePlmAddress(publicKey: string) { | |
// converts a given hex string into Uint8Array | |
const toByteArray = (hexString: string) => { | |
const result = []; | |
for (let i = 0; i < hexString.length; i += 2) { | |
result.push(parseInt(hexString.substr(i, 2), 16)); | |
} | |
return new Uint8Array(result); | |
}; | |
// hash to blake2 | |
const plasmPubKey = polkadotUtilCrypto.blake2AsU8a(toByteArray(publicKey.replace('0x', '')), 256); | |
// encode address | |
const plasmAddress = polkadotUtilCrypto.encodeAddress(plasmPubKey, 5); | |
return plasmAddress; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment