Skip to content

Commit

Permalink
add chain
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienGllmt committed May 14, 2019
1 parent a5a643e commit dfa9b99
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 35 deletions.
2 changes: 1 addition & 1 deletion cardano-wallet/Cargo.toml
@@ -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"
Expand Down
10 changes: 8 additions & 2 deletions cardano-wallet/README.md
Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand Down
103 changes: 74 additions & 29 deletions cardano-wallet/src/lib.rs
Expand Up @@ -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 {
Expand All @@ -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)
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 10 additions & 3 deletions cardano-wallet/tests/index.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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");

}
Expand Down

0 comments on commit dfa9b99

Please sign in to comment.