Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions js/Redeemer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import RustModule from './RustModule';
import { newArray, newArray0, copyArray } from './utils/arrays';
import { apply } from './utils/functions';

export const PRIVATE_KEY_SIZE = 32;
export const PUBLIC_KEY_SIZE = 32;
export const ADDRESS_SIZE = 32;

export const privateKeyToPublicKey = (module, privateKey) => {

if (privateKey.length !== PRIVATE_KEY_SIZE) { return false; }

const bufprivateKey = newArray(module, privateKey);
const bufpublicKey = newArray0(module, PUBLIC_KEY_SIZE);

module.cardano_redeem_prv_to_pub(bufprivateKey, bufpublicKey);
let publicKey = copyArray(module, bufpublicKey, PUBLIC_KEY_SIZE);

module.dealloc(bufprivateKey);
module.dealloc(bufpublicKey);

return publicKey;
};

export const privateKeyToAddress = (module, privateKey) => {
if (privateKey.length !== PRIVATE_KEY_SIZE) { return false; }

const bufprivateKey = newArray(module, privateKey);
const bufaddress = newArray0(module, ADDRESS_SIZE);

module.cardano_redeem_prv_to_address(bufprivateKey, bufaddress);
let address = copyArray(module, bufaddress, ADDRESS_SIZE);
module.dealloc(bufprivateKey);
module.dealloc(bufaddress);

return address;
}

export const publicKeyToAddress = (module, publicKey) => {

if (publicKey.length !== PUBLIC_KEY_SIZE) { return false; }

const bufpublicKey = newArray(module, publicKey);
const bufaddress = newArray0(module, ADDRESS_SIZE);

module.cardano_redeem_pub_to_address(bufpublicKey, bufaddress);
let address = copyArray(module, bufaddress, ADDRESS_SIZE);
module.dealloc(bufpublicKey);
module.dealloc(bufaddress);

return address;

}

export default {
privateKeyToPublicKey: apply(privateKeyToPublicKey, RustModule),
privateKeyToAddress: apply(privateKeyToAddress, RustModule),
publicKeyToAddress: apply(publicKeyToAddress, RustModule),
PRIVATE_KEY_SIZE: PRIVATE_KEY_SIZE,
PUBLIC_KEY_SIZE: PUBLIC_KEY_SIZE,
ADDRESS_SIZE: ADDRESS_SIZE
};
2 changes: 2 additions & 0 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Config from './Config.js';
import Wallet from './Wallet.js';
import RandomAddressChecker from './RandomAddressChecker';
import PasswordProtect from './PasswordProtect';
import Redeemer from './Redeemer.js';

module.exports = {
Payload,
Expand All @@ -21,4 +22,5 @@ module.exports = {
Wallet,
Config,
PasswordProtect,
Redeemer
};
50 changes: 49 additions & 1 deletion wallet-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use self::cardano::hdwallet;
use self::cardano::paperwallet;
use self::cardano::address;
use self::cardano::hdpayload;
use self::cardano::redeem;
use self::cardano::{util::{hex}, tx, fee, coin, txutils};
use self::cardano::config::{Config};
use self::cardano::wallet::{self, bip44, rindex, scheme::{Wallet, SelectionPolicy}};
Expand Down Expand Up @@ -135,6 +136,17 @@ unsafe fn read_seed(seed_ptr: *const c_uchar) -> hdwallet::Seed {
hdwallet::Seed::from_slice(seed_slice).unwrap()
}

unsafe fn read_redeem_private_key(rprv_ptr: *const c_uchar) -> redeem::PrivateKey {
let rprv = std::slice::from_raw_parts(rprv_ptr, redeem::PRIVATEKEY_SIZE);
redeem::PrivateKey::from_slice(rprv).unwrap()
}

unsafe fn read_redeem_public_key(rpub_ptr: *const c_uchar) -> redeem::PublicKey {
let rpub = std::slice::from_raw_parts(rpub_ptr, redeem::PUBLICKEY_SIZE);
redeem::PublicKey::from_slice(rpub).unwrap()
}


#[no_mangle]
pub extern "C" fn wallet_from_enhanced_entropy( entropy_ptr: *const c_uchar
, entropy_size: usize
Expand Down Expand Up @@ -1042,7 +1054,6 @@ pub extern "C" fn encrypt_with_password( password_ptr: *const c_uchar
output.len() as i32
}


#[no_mangle]
pub extern "C" fn decrypt_with_password( password_ptr: *const c_uchar
, password_sz: usize
Expand Down Expand Up @@ -1086,3 +1097,40 @@ pub extern "C" fn decrypt_with_password( password_ptr: *const c_uchar
-1
}
}

#[no_mangle]
pub extern "C" fn cardano_redeem_prv_to_pub(rprv_ptr: *const c_uchar, rpub_ptr: *mut c_uchar) {
let private_key = unsafe { read_redeem_private_key(rprv_ptr) };
let public_key = private_key.public();
unsafe { write_data(public_key.as_ref(), rpub_ptr); }
}

#[no_mangle]
pub extern "C" fn cardano_redeem_pub_to_address(rpub_ptr: *const c_uchar, addr_ptr: *mut c_uchar) {
let public_key = unsafe { read_redeem_public_key(rpub_ptr) };

let addr_type = address::AddrType::ATRedeem;
let sd = address::SpendingData::RedeemASD(public_key.clone());
let attrs = address::Attributes::new_bootstrap_era(None);
let ea = address::ExtendedAddr::new(addr_type, sd, attrs);

let ea_bytes = cbor!(ea).unwrap();

unsafe { write_data(&ea_bytes, addr_ptr) }
}

#[no_mangle]
pub extern "C" fn cardano_redeem_prv_to_address(rprv_ptr: *const c_uchar, addr_ptr: *mut c_uchar) {

let private_key = unsafe { read_redeem_private_key(rprv_ptr) };
let public_key = private_key.public();

let addr_type = address::AddrType::ATRedeem;
let sd = address::SpendingData::RedeemASD(public_key.clone());
let attrs = address::Attributes::new_bootstrap_era(None);
let ea = address::ExtendedAddr::new(addr_type, sd, attrs);

let ea_bytes = cbor!(ea).unwrap();

unsafe { write_data(&ea_bytes, addr_ptr) }
}