Skip to content

Commit

Permalink
Add secp256k1 crate back but still failing with known issue rust-bitc…
Browse files Browse the repository at this point in the history
  • Loading branch information
KendallWeihe committed Apr 14, 2024
1 parent bb6769c commit a432a68
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
1 change: 1 addition & 0 deletions tmp/prototype-crypto-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license-file.workspace = true
base64 = "0.22.0"
ed25519-dalek = "2.1.1"
rand = "0.8.5"
secp256k1 = {version="0.29.0", features=["rand"]}
k256 = {version = "0.13.3", features = ["ecdsa", "jwk"]}
serde_json = "1.0.115"
wasm-bindgen = "0.2.92"
Expand Down
43 changes: 22 additions & 21 deletions tmp/prototype-crypto-wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use base64::{engine::general_purpose, Engine as _};
use ed25519_dalek::{SigningKey, SECRET_KEY_LENGTH};
use rand::{rngs::OsRng, RngCore};
// use secp256k1::Secp256k1;
// use k256::{
// ecdsa::{signature::Signer, SigningKey as k256SigningKey},
// EncodedPoint,
// };
use k256::{
ecdsa::{
signature::{Signer, Verifier},
SigningKey as k256SigningKey, VerifyingKey,
},
EncodedPoint,
};
use rand::{rngs::OsRng, RngCore};
use secp256k1::Secp256k1;
use serde_json::json;
use wasm_bindgen::prelude::wasm_bindgen;

Expand All @@ -38,24 +34,24 @@ pub fn prove_ed25519() {
println!("JWK: {}", jwk.to_string());
}

// #[wasm_bindgen]
// pub fn prove_secp256k1() {
// let secp = Secp256k1::new();
// let (secret_key, public_key) = secp.generate_keypair(&mut OsRng);
#[wasm_bindgen]
pub fn prove_bitcoin_secp256k1() {
let secp = Secp256k1::new();
let (secret_key, public_key) = secp.generate_keypair(&mut OsRng);

// // Serialize public key in uncompressed form
// let serialized_pub_key = public_key.serialize_uncompressed(); // 65 bytes: 0x04, x (32 bytes), y (32 bytes)
// Serialize public key in uncompressed form
let serialized_pub_key = public_key.serialize_uncompressed(); // 65 bytes: 0x04, x (32 bytes), y (32 bytes)

// let jwk = json!({
// "kty": "EC",
// "crv": "secp256k1",
// "x": general_purpose::URL_SAFE_NO_PAD.encode(&serialized_pub_key[1..33]), // Skip the first byte (0x04)
// "y": general_purpose::URL_SAFE_NO_PAD.encode(&serialized_pub_key[33..65]),
// "d": general_purpose::URL_SAFE_NO_PAD.encode(&secret_key.secret_bytes())
// });
let jwk = json!({
"kty": "EC",
"crv": "secp256k1",
"x": general_purpose::URL_SAFE_NO_PAD.encode(&serialized_pub_key[1..33]), // Skip the first byte (0x04)
"y": general_purpose::URL_SAFE_NO_PAD.encode(&serialized_pub_key[33..65]),
"d": general_purpose::URL_SAFE_NO_PAD.encode(&secret_key.secret_bytes())
});

// println!("JWK: {}", jwk);
// }
println!("JWK: {}", jwk);
}

#[wasm_bindgen]
pub fn prove_secp256k1() {
Expand Down Expand Up @@ -101,4 +97,9 @@ mod tests {
fn secp256k1() {
prove_secp256k1()
}

#[test]
fn bitcoin_secp256k1() {
prove_bitcoin_secp256k1()
}
}

0 comments on commit a432a68

Please sign in to comment.