Skip to content
Permalink
Browse files

Merge pull request #69 from SebastienGllmt/feature/add_chain

Add Bip44 chain support and remove deprecated sign call
  • Loading branch information...
NicolasDP committed May 15, 2019
2 parents 9397ead + dfa9b99 commit 96d22e1e4ff764744aea19ac7b26815bddecb2b6
Showing with 93 additions and 35 deletions.
  1. +1 −1 cardano-wallet/Cargo.toml
  2. +8 −2 cardano-wallet/README.md
  3. +74 −29 cardano-wallet/src/lib.rs
  4. +10 −3 cardano-wallet/tests/index.js
@@ -1,6 +1,6 @@
[package]
name = "cardano-wallet"
version = "0.3.0"
version = "1.0.0"
authors = ["Nicolas Di Prima <nicolas.diprima@iohk.io>"]
description = "Cardano Wallet, from rust to JS via Wasm"
homepage = "https://github.com/input-output-hk/js-cardano-wasm#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
@@ -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<Bip44ChainPublic, JsValue> {
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<PublicKey, JsValue> {
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)
@@ -44,9 +44,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);
@@ -104,7 +106,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");

}

0 comments on commit 96d22e1

Please sign in to comment.
You can’t perform that action at this time.