Skip to content

Commit 3543470

Browse files
committed
[Redeem] Adding c bindings in order to support redeem private key to public key functionality
1 parent def5d9a commit 3543470

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

js/Redeem.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import RustModule from './RustModule';
2+
import { newArray, newArray0, copyArray } from './utils/arrays';
3+
import { apply } from './utils/functions';
4+
5+
export const PRIVATE_KEY_SIZE = 32;
6+
export const PUBLIC_KEY_SIZE = 32;
7+
8+
export const redeemPrivateKeyToPublicKey = (module, privateKey) => {
9+
10+
if (privateKey.length !== PRIVATE_KEY_SIZE) { return false; }
11+
12+
const bufprivateKey = newArray(module, privateKey);
13+
const bufpublicKey = newArray0(module, PUBLIC_KEY_SIZE);
14+
15+
let rs = module.cardano_redeem_prv_to_pub(bufprivateKey, bufpublicKey);
16+
let publicKey = null;
17+
if (rs > 0) {
18+
publicKey = copyArray(module, bufpublicKey, rs);
19+
}
20+
21+
module.dealloc(bufprivateKey);
22+
module.dealloc(bufpublicKey);
23+
24+
return publicKey;
25+
};
26+
27+
export default {
28+
fromSeed: redeemPrivateKeyToPublicKey(redeemPrivateKeyToPublicKey, RustModule),
29+
PRIVATE_KEY_SIZE: PRIVATE_KEY_SIZE,
30+
PUBLIC_KEY_SIZE: PUBLIC_KEY_SIZE
31+
};

js/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Config from './Config.js';
88
import Wallet from './Wallet.js';
99
import RandomAddressChecker from './RandomAddressChecker';
1010
import PasswordProtect from './PasswordProtect';
11+
import Redeem from './Redeem';
1112

1213
module.exports = {
1314
Payload,
@@ -21,4 +22,5 @@ module.exports = {
2122
Wallet,
2223
Config,
2324
PasswordProtect,
25+
Redeem
2426
};

wallet-wasm/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use self::cardano::hdwallet;
1818
use self::cardano::paperwallet;
1919
use self::cardano::address;
2020
use self::cardano::hdpayload;
21+
use self::cardano::redeem;
2122
use self::cardano::{util::{hex}, tx, fee, coin, txutils};
2223
use self::cardano::config::{Config};
2324
use self::cardano::wallet::{self, bip44, rindex, scheme::{Wallet, SelectionPolicy}};
@@ -1086,3 +1087,20 @@ pub extern "C" fn decrypt_with_password( password_ptr: *const c_uchar
10861087
-1
10871088
}
10881089
}
1090+
1091+
unsafe fn read_redeem_private_key(rprv_ptr: *const c_uchar) -> redeem::PrivateKey {
1092+
let rprv_ptr = std::slice::from_raw_parts(rprv_ptr, redeem::PRIVATEKEY_SIZE);
1093+
redeem::PrivateKey::from_slice(rprv_ptr).unwrap()
1094+
}
1095+
1096+
unsafe fn write_redeem_public_key(rpub: &redeem::PublicKey, rpub_ptr: *mut c_uchar) {
1097+
let out = std::slice::from_raw_parts_mut(rpub_ptr, redeem::PUBLICKEY_SIZE);
1098+
out[0..redeem::PUBLICKEY_SIZE].clone_from_slice(rpub.as_ref());
1099+
}
1100+
1101+
#[no_mangle]
1102+
pub extern "C" fn cardano_redeem_prv_to_pub(rprv_ptr: *const c_uchar, rpub_ptr: *mut c_uchar) {
1103+
let private_key = unsafe { read_redeem_private_key(rprv_ptr) };
1104+
let public_key = private_key.public();
1105+
unsafe { write_redeem_public_key(&public_key, rpub_ptr) }
1106+
}

0 commit comments

Comments
 (0)