input-output-hk / js-chain-libs

chain-libs javascript SDK
Apache License 2.0
18 stars 14 forks source link

Add from_bytes for PublicKey and PrivateKey #54

Closed SebastienGllmt closed 5 years ago

SebastienGllmt commented 5 years ago

Background: for Yoroi we need to convert our Icarus-style public & private keys to the new Shelley wrapper. To make this easy, I add a from_bytes function to both PublicKey and PrivateKey

Usage in Yoroi looks like this

export function v2SkKeyToV3Key(
  v2Key: RustModule.WalletV2.PrivateKey,
): RustModule.WalletV3.PrivateKey {
  return RustModule.WalletV3.PrivateKey.from_extended_bytes(
    // need to slice out the chain code from the private key
    Buffer.from(v2Key.to_hex().slice(0, 128), 'hex')
  );
}
export function v2PkKeyToV3Key(
  v2Key: RustModule.WalletV2.PublicKey,
): RustModule.WalletV3.PublicKey {
  return RustModule.WalletV3.PublicKey.from_bytes(
    // need to slice out the chain code from the public key
    Buffer.from(v2Key.to_hex().slice(0, 64), 'hex')
  );
}

Solves #52 #53