Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More cleanup #11

Merged
merged 6 commits into from
May 26, 2023
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
run: |
mkdir artifacts
# copy all relevant headers and static and dynamic libs into the new directory
find . -maxdepth 3 -type f \( \
find . -maxdepth 4 -type f \( \
-path './LICENSE' -o \
-path '*.h' -o \
-path './target/${{ matrix.target }}/release/*.a' -o \
Expand All @@ -64,4 +64,4 @@ jobs:
- name: Release
uses: softprops/action-gh-release@v1
with:
files: ${{ env.ARTIFACT_NAME }}_${{ matrix.name }}.tar.gz
files: ${{ env.ARTIFACT_NAME }}_${{ matrix.name }}.tar.gz
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ members = [
"derivation-path",
"ed25519-bip32",
"remote-wallet",
"sdkutils",
]

[workspace.package]
Expand All @@ -21,6 +20,5 @@ qstring = "0.7.2"
solana-sdk = "=1.14.17"
spacemesh-derivation-path = { path = "derivation-path", version = "=0.0.1" }
spacemesh-remote-wallet = { path = "remote-wallet", version = "=0.0.1" }
spacemesh-sdkutils = { path = "sdkutils", version = "=0.0.1" }
thiserror = "1.0.40"
uriparse = "0.6.4"
1 change: 0 additions & 1 deletion ed25519-bip32/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ crate-type = ["staticlib", "cdylib", "rlib"]

[dependencies]
ed25519-dalek-bip32 = "0.2.0"
spacemesh-sdkutils = { workspace = true }
wasm-bindgen = "0.2.84"
54 changes: 34 additions & 20 deletions ed25519-bip32/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ extern crate wasm_bindgen;

use std::ffi::{c_char, CStr};
use ed25519_dalek_bip32::{ed25519_dalek::{Keypair, KEYPAIR_LENGTH, SECRET_KEY_LENGTH}, DerivationPath, ExtendedSecretKey};
use spacemesh_sdkutils::{check_err, err};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
Expand Down Expand Up @@ -36,48 +35,63 @@ pub extern "C" fn derive_c(
derivation_path_ptr: *const c_char,
result: *mut u8,
) -> u16 {
match _derive_c(seed, seedlen, derivation_path_ptr) {
Ok(keypair) => {
let result_slice = unsafe { std::slice::from_raw_parts_mut(result, KEYPAIR_LENGTH) };
result_slice.copy_from_slice(&keypair.to_bytes());
0
}
Err(err) => {
eprintln!("{}", err);
1
}
}
}

fn _derive_c(
seed: *const u8,
seedlen: usize,
derivation_path_ptr: *const c_char,
) -> Result<Keypair, Box<dyn std::error::Error>> {
// Seed must be at least 32 bytes
if seedlen < SECRET_KEY_LENGTH {
err!("seed must be at least 32 bytes");
return Err("seed must be at least 32 bytes".into());
}
let seed_slice = unsafe { std::slice::from_raw_parts(seed, seedlen) };
let derivation_path_str = unsafe { CStr::from_ptr(derivation_path_ptr) };
let derivation_path_str = derivation_path_str.to_str();
check_err!(derivation_path_str, "failed to convert path string from raw parts");
let derivation_path_str = derivation_path_str.unwrap().parse();
check_err!(derivation_path_str, "failed to parse derivation path");
let derivation_path: DerivationPath = derivation_path_str.unwrap();
let derivation_path: DerivationPath = derivation_path_str
.to_str()
.map_err(|e| format!("failed to convert path string to str: {}", e))?
.parse()
.map_err(|e| format!("failed to parse derivation path: {}", e))?;

// for now we are rather strict with which types of paths we accept, to avoid errors and to
// be as compatible as possible with BIP-44. the path must be of the format
// "m/44'/540'/...", i.e., it must have purpose 44 and coin type
// 540 and all path elements must be hardened. we expect it to contain between 2 and 5
// elements.
if derivation_path.path().len() < 2 {
err!("path too short");
return Err("path too short".into());
}
if derivation_path.path().len() > 5 {
err!("path too long");
return Err("path too long".into());
}
if derivation_path.path()[0].to_u32() != 44 {
err!("bad path purpose");
return Err("bad path purpose".into());
}
if derivation_path.path()[1].to_u32() != 540 {
err!("bad path coin type");
return Err("bad path coin type".into());
}
for p in derivation_path.path() {
if !p.is_hardened() {
err!("path isn't fully hardened");
return Err("path isn't fully hardened".into());
}
}

let extended = ExtendedSecretKey::from_seed(seed_slice)
.and_then(|extended| extended.derive(&derivation_path));
check_err!(extended, "failed to derive secret key from seed");
let extended_inner = extended.unwrap();
let extended_public_key = extended_inner.public_key();
let keypair = Keypair{secret: extended_inner.secret_key, public: extended_public_key};
let result_slice = unsafe { std::slice::from_raw_parts_mut(result, KEYPAIR_LENGTH) };
result_slice.copy_from_slice(&keypair.to_bytes());
0
.map_err(|e| format!("failed to derive secret key from seed: {}", e))?
.derive(&derivation_path)
.map_err(|e| format!("failed to derive derivation path: {}", e))?;
let extended_public_key = extended.public_key();
Ok(Keypair{secret: extended.secret_key, public: extended_public_key})
}
1 change: 0 additions & 1 deletion remote-wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ qstring = { workspace = true }
semver = "1.0.17"
solana-sdk = { workspace = true }
spacemesh-derivation-path = { workspace = true }
spacemesh-sdkutils = { workspace = true }
thiserror = { workspace = true }
uriparse = { workspace = true }

Expand Down
2 changes: 1 addition & 1 deletion remote-wallet/remote_wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* to result, which must be at least 32 bytes long. It returns a status code, with a return value
* of zero indicating success.
*/
uint16_t read_pubkey_from_ledger(const char *path_ptr,
uint16_t read_pubkey_from_ledger(const char *path,
const char *derivation_path_ptr,
bool confirm_key,
uint8_t *result);
8 changes: 4 additions & 4 deletions remote-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ use {
/// of zero indicating success.
#[no_mangle]
pub extern "C" fn read_pubkey_from_ledger(
path_ptr: *const c_char,
path: *const c_char,
derivation_path_ptr: *const c_char,
confirm_key: bool,
result: *mut u8,
) -> u16 {
match _read_pubkey_from_ledger(path_ptr, derivation_path_ptr, confirm_key) {
match _read_pubkey_from_ledger(path, derivation_path_ptr, confirm_key) {
Ok(pubkey) => {
let result_slice = unsafe { std::slice::from_raw_parts_mut(result, PUBKEY_BYTES) };
result_slice.copy_from_slice(pubkey.as_ref());
Expand All @@ -40,15 +40,15 @@ pub extern "C" fn read_pubkey_from_ledger(
}

fn _read_pubkey_from_ledger(
path_ptr: *const c_char,
path: *const c_char,
derivation_path_ptr: *const c_char,
confirm_key: bool,
) -> Result<Pubkey, Box<dyn std::error::Error>> {
// first handle the device path
// note: it might seem to make more sense to do all the unsafe operations in the parent
// function and leave only the "business logic" here, but doing it here allows us to handle
// all errors at the higher level and pass them back across FFI.
let path_str = unsafe { CStr::from_ptr(path_ptr) };
let path_str = unsafe { CStr::from_ptr(path) };
let path_str = path_str
.to_str()
.map_err(|e| format!("converting path string: {e}"))?;
Expand Down
11 changes: 0 additions & 11 deletions sdkutils/Cargo.toml

This file was deleted.

35 changes: 0 additions & 35 deletions sdkutils/src/lib.rs

This file was deleted.