From dfa9b994b4149b400d1ba3767312c8472cf35b3b Mon Sep 17 00:00:00 2001 From: Sebastien Date: Wed, 15 May 2019 01:11:00 +0900 Subject: [PATCH] add chain --- cardano-wallet/Cargo.toml | 2 +- cardano-wallet/README.md | 10 +++- cardano-wallet/src/lib.rs | 103 ++++++++++++++++++++++++---------- cardano-wallet/tests/index.js | 13 ++++- 4 files changed, 93 insertions(+), 35 deletions(-) diff --git a/cardano-wallet/Cargo.toml b/cardano-wallet/Cargo.toml index 76016cc..907f0af 100644 --- a/cardano-wallet/Cargo.toml +++ b/cardano-wallet/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cardano-wallet" -version = "0.3.0" +version = "1.0.0" authors = ["Nicolas Di Prima "] description = "Cardano Wallet, from rust to JS via Wasm" homepage = "https://github.com/input-output-hk/js-cardano-wasm#README.md" diff --git a/cardano-wallet/README.md b/cardano-wallet/README.md index ccb3eba..c308960 100644 --- a/cardano-wallet/README.md +++ b/cardano-wallet/README.md @@ -44,7 +44,8 @@ let account = wallet.bip44_account(Cardano.AccountIndex.new(0 | 0x80000000)); let account_public = account.public(); // create an address -let key_pub = account_public.address_key(false, Cardano.AddressKeyIndex.new(0)); +let chain_pub = account_public.bip44_chain(false); +let key_pub = chain_pub.address_key(Cardano.AddressKeyIndex.new(0)); let address = key_pub.bootstrap_era_address(settings); console.log("Address m/bip44/ada/'0/0/0", address.to_base58()); @@ -120,7 +121,12 @@ You need to make sure: let transaction_finalizer = new Wallet.TransactionFinalized(transaction); for (let index = 0; index < inputs.length; index++) { - transaction_finalizer.sign(settings, key_prv); + const witness = Wallet.Witness.new_extended_key( + settings, + key_prv, + transaction_finalizer.id() + ); + transaction_finalizer.add_witness(witness); } // at this stage the transaction is ready to be sent diff --git a/cardano-wallet/src/lib.rs b/cardano-wallet/src/lib.rs index ae9eb99..1e858c2 100644 --- a/cardano-wallet/src/lib.rs +++ b/cardano-wallet/src/lib.rs @@ -432,10 +432,13 @@ impl Bip44AccountPrivate { derivation_scheme: self.derivation_scheme, } } - pub fn address_key(&self, internal: bool, index: AddressKeyIndex) -> PrivateKey { - self.key - .derive(self.derivation_scheme, if internal { 1 } else { 0 }) - .derive(self.derivation_scheme, index.0) + pub fn bip44_chain(&self, internal: bool) -> Bip44ChainPrivate { + Bip44ChainPrivate { + key: self + .key + .derive(self.derivation_scheme, if internal { 1 } else { 0 }), + derivation_scheme: self.derivation_scheme, + } } pub fn key(&self) -> PrivateKey { @@ -456,13 +459,73 @@ impl Bip44AccountPublic { derivation_scheme: derivation_scheme, } } - pub fn address_key( + pub fn bip44_chain( &self, internal: bool, + ) -> Result { + self + .key + .derive(self.derivation_scheme, if internal { 1 } else { 0 }) + .map(|key: PublicKey| + Bip44ChainPublic { + key, + derivation_scheme: self.derivation_scheme, + } + ) + } + + pub fn key(&self) -> PublicKey { + self.key.clone() + } +} + +#[wasm_bindgen] +pub struct Bip44ChainPrivate { + key: PrivateKey, + derivation_scheme: DerivationScheme, +} +#[wasm_bindgen] +impl Bip44ChainPrivate { + pub fn new(key: PrivateKey, derivation_scheme: DerivationScheme) -> Bip44ChainPrivate { + Bip44ChainPrivate { + key: key, + derivation_scheme: derivation_scheme, + } + } + pub fn public(&self) -> Bip44ChainPublic { + Bip44ChainPublic { + key: self.key.public(), + derivation_scheme: self.derivation_scheme, + } + } + pub fn address_key(&self, index: AddressKeyIndex) -> PrivateKey { + self.key + .derive(self.derivation_scheme, index.0) + } + + pub fn key(&self) -> PrivateKey { + self.key.clone() + } +} + +#[wasm_bindgen] +pub struct Bip44ChainPublic { + key: PublicKey, + derivation_scheme: DerivationScheme, +} +#[wasm_bindgen] +impl Bip44ChainPublic { + pub fn new(key: PublicKey, derivation_scheme: DerivationScheme) -> Bip44ChainPublic { + Bip44ChainPublic { + key: key, + derivation_scheme: derivation_scheme, + } + } + pub fn address_key( + &self, index: AddressKeyIndex, ) -> Result { self.key - .derive(self.derivation_scheme, if internal { 1 } else { 0 })? .derive(self.derivation_scheme, index.0) } @@ -959,9 +1022,13 @@ impl TransactionBuilder { } } +/// sign the inputs of the transaction (i.e. unlock the funds the input are +/// referring to). +/// +/// The signature must be added one by one in the same order the inputs have +/// been added. #[wasm_bindgen] pub struct Witness(tx::TxInWitness); - #[wasm_bindgen] impl Witness { pub fn new_extended_key( @@ -1025,28 +1092,6 @@ impl TransactionFinalized { TransactionId(self.tx_id) } - /// sign the inputs of the transaction (i.e. unlock the funds the input are - /// referring to). - /// - /// The signature must be added one by one in the same order the inputs have - /// been added. - /// - /// Deprecated: use `add_witness` instead. - pub fn sign( - &mut self, - blockchain_settings: &BlockchainSettings, - key: &PrivateKey, - ) -> Result<(), JsValue> { - let signature = tx::TxInWitness::new_extended_pk( - blockchain_settings.protocol_magic, - &key.0, - &self.tx_id, - ); - self.finalized - .add_witness(signature) - .map_err(|e| JsValue::from_str(&format! {"{:?}", e})) - } - pub fn add_witness(&mut self, witness: Witness) -> Result<(), JsValue> { self.finalized .add_witness(witness.0) diff --git a/cardano-wallet/tests/index.js b/cardano-wallet/tests/index.js index aa6d61f..b31d240 100644 --- a/cardano-wallet/tests/index.js +++ b/cardano-wallet/tests/index.js @@ -45,9 +45,11 @@ Wallet let account_public = account.public(); console.log('account public ' + account_public.key().to_hex()); - let key_prv = account.address_key(false, Wallet.AddressKeyIndex.new(0)); + let chain_prv = account.bip44_chain(false); + let key_prv = chain_prv.address_key(Wallet.AddressKeyIndex.new(0)); console.log('address public ' + key_prv.to_hex()); - let key_pub = account_public.address_key(false, Wallet.AddressKeyIndex.new(0)); + let chain_pub = account_public.bip44_chain(false); + let key_pub = chain_pub.address_key(Wallet.AddressKeyIndex.new(0)); console.log('address public ' + key_pub.to_hex()); let address = key_pub.bootstrap_era_address(settings); @@ -105,7 +107,12 @@ Wallet console.log("transaction finalizer built", transaction_finalizer); for (let index = 0; index < inputs.length; index++) { - transaction_finalizer.sign(settings, key_prv); + const witness = Wallet.Witness.new_extended_key( + settings, + key_prv, + transaction_finalizer.id() + ); + transaction_finalizer.add_witness(witness); console.log("signature ", index, "added"); }