Skip to content

Commit

Permalink
Merge pull request #57 from SebastienGllmt/sign-and-verify
Browse files Browse the repository at this point in the history
Add sign&verify and external witnesses
  • Loading branch information
NicolasDP committed Oct 22, 2019
2 parents 8f08744 + 91997d7 commit dde3a70
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/lib.rs
Expand Up @@ -17,6 +17,50 @@ use std::ops::{Add, Sub};
use std::str::FromStr;
use wasm_bindgen::prelude::*;

macro_rules! impl_signature {
($name:ident, $signee_type:ty, $verifier_type:ty) => {
#[wasm_bindgen]
pub struct $name(crypto::Signature<$signee_type, $verifier_type>);

#[wasm_bindgen]
impl $name {
pub fn to_bytes(&self) -> Vec<u8> {
self.0.to_bytes()
}

pub fn to_bech32(&self) -> String {
self.0.to_bech32_str()
}

pub fn to_hex(&self) -> String {
hex::encode(&self.0.as_ref())
}

pub fn from_bytes(bytes: &[u8]) -> Result<$name, JsValue> {
crypto::Signature::from_binary(bytes)
.map($name)
.map_err(|e| JsValue::from_str(&format!("{}", e)))
}

pub fn from_bech32(bech32_str: &str) -> Result<$name, JsValue> {
crypto::Signature::try_from_bech32_str(&bech32_str)
.map($name)
.map_err(|e| JsValue::from_str(&format!("{}", e)))
}

pub fn from_hex(input: &str) -> Result<$name, JsValue> {
crypto::Signature::from_str(input)
.map_err(|e| JsValue::from_str(&format!("{:?}", e)))
.map($name)
}
}
};
}

impl_signature!(Ed25519Signature, Vec<u8>, crypto::Ed25519);
impl_signature!(AccountWitness, tx::WitnessAccountData, crypto::Ed25519);
impl_signature!(UtxoWitness, tx::WitnessUtxoData, crypto::Ed25519);

/// ED25519 signing key, either normal or extended
#[wasm_bindgen]
pub struct PrivateKey(key::EitherEd25519SecretKey);
Expand Down Expand Up @@ -88,6 +132,10 @@ impl PrivateKey {
.map(PrivateKey)
.map_err(|_| JsValue::from_str("Invalid normal secret key"))
}

pub fn sign(&self, message: &[u8]) -> Ed25519Signature {
Ed25519Signature(self.0.sign(&message.to_vec()))
}
}

/// ED25519 key used as public key
Expand Down Expand Up @@ -123,6 +171,10 @@ impl PublicKey {
.map_err(|e| JsValue::from_str(&format!("{}", e)))
.map(PublicKey)
}

pub fn verify(&self, data: &[u8], signature: &Ed25519Signature) -> bool {
signature.0.verify_slice(&self.0, data) == crypto::Verification::Success
}
}

#[wasm_bindgen]
Expand Down Expand Up @@ -1467,6 +1519,11 @@ impl Witness {
))
}

// Witness for a utxo-based transaction generated externally (such as hardware wallets)
pub fn from_external_utxo(witness: &UtxoWitness) -> Witness {
Witness(tx::Witness::Utxo(witness.0.clone()))
}

/// Generate Witness for an account based transaction Input
/// the account-spending-counter should be incremented on each transaction from this account
pub fn for_account(
Expand All @@ -1483,6 +1540,11 @@ impl Witness {
))
}

// Witness for a account-based transaction generated externally (such as hardware wallets)
pub fn from_external_account(witness: &AccountWitness) -> Witness {
Witness(tx::Witness::Account(witness.0.clone()))
}

/// Get string representation
pub fn to_bech32(&self) -> Result<String, JsValue> {
let bytes = self
Expand Down

0 comments on commit dde3a70

Please sign in to comment.