input-output-hk / js-chain-libs

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

Add sign&verify and external witnesses #57

Closed SebastienGllmt closed 4 years ago

SebastienGllmt commented 5 years ago

I add a sign function to PrivateKey and verify to PublicKey (this requires https://github.com/input-output-hk/chain-libs/pull/118)

I designed this so we can easily add more signature key in the future if needed.

Usage


// signing test

const buff1 = Buffer.from('hello', 'utf-8');
const signature = key.sign(buff1);

// ed25519_sig1eu603zrj68h55dg9pwvmvc4wc48zy7l8m83m0fgdxekn7mx8jmvhyd3anncufde39l4vv2drpx409t2g0r7nc9m303qu06nasj3kkqqvv2xhz
console.log(signature.to_bech32());

// cf34f88872d1ef4a35050b99b662aec54e227be7d9e3b7a50d366d3f6cc796d972363d9cf1c4b7312feac629a309aaf2ad4878fd3c17717c41c7ea7d84a36b00
console.log(Buffer.from(signature.to_bytes()).toString('hex'));

// cf34f88872d1ef4a35050b99b662aec54e227be7d9e3b7a50d366d3f6cc796d972363d9cf1c4b7312feac629a309aaf2ad4878fd3c17717c41c7ea7d84a36b00
console.log(signature.to_hex());

// verify test

const pubKey = key.to_public();
const success = pubKey.verify(buff1, signature); // true as expected

const buff2 = Buffer.from('world', 'utf-8');
const failed = pubKey.verify(buff2, signature); // failed as expected

Other change

I also added AccountWitness and UtxoWitness so that the WASM bindings can easily support hardware wallets in the future