Skip to content

Instantly share code, notes, and snippets.

@hoonsubin
Last active April 13, 2021 19:11
Show Gist options
  • Save hoonsubin/ab4dea2a96c0c8c3675ccc930026ae06 to your computer and use it in GitHub Desktop.
Save hoonsubin/ab4dea2a96c0c8c3675ccc930026ae06 to your computer and use it in GitHub Desktop.
generates a Plasm public address with the given ECDSA public key
/**
* 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