Skip to content

Commit

Permalink
d
Browse files Browse the repository at this point in the history
  • Loading branch information
hazae41 committed Aug 24, 2024
1 parent 1fd5287 commit 4fd8c08
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 136 deletions.
12 changes: 6 additions & 6 deletions src/wasm/pkg/secp256k1_wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,19 @@ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembl

export interface InitOutput {
readonly memory: WebAssembly.Memory;
readonly __wbg_secp256k1signingkey_free: (a: number, b: number) => void;
readonly secp256k1signingkey_new: () => number;
readonly secp256k1signingkey_from_bytes: (a: number, b: number) => void;
readonly secp256k1signingkey_to_bytes: (a: number) => number;
readonly secp256k1signingkey_verifying_key: (a: number) => number;
readonly secp256k1signingkey_sign_prehash_recoverable: (a: number, b: number, c: number) => void;
readonly __wbg_secp256k1signatureandrecovery_free: (a: number, b: number) => void;
readonly secp256k1signatureandrecovery_to_bytes: (a: number) => number;
readonly __wbg_secp256k1verifyingkey_free: (a: number, b: number) => void;
readonly secp256k1verifyingkey_from_sec1_bytes: (a: number, b: number) => void;
readonly secp256k1verifyingkey_recover_from_prehash: (a: number, b: number, c: number) => void;
readonly secp256k1verifyingkey_to_sec1_compressed_bytes: (a: number) => number;
readonly secp256k1verifyingkey_to_sec1_uncompressed_bytes: (a: number) => number;
readonly __wbg_secp256k1signingkey_free: (a: number, b: number) => void;
readonly secp256k1signingkey_new: () => number;
readonly secp256k1signingkey_from_bytes: (a: number, b: number) => void;
readonly secp256k1signingkey_to_bytes: (a: number) => number;
readonly secp256k1signingkey_verifying_key: (a: number) => number;
readonly secp256k1signingkey_sign_prehash_recoverable: (a: number, b: number, c: number) => void;
readonly secp256k1signingkey_random: () => number;
readonly __wbg_memory_free: (a: number, b: number) => void;
readonly memory_new: (a: number, b: number) => number;
Expand Down
2 changes: 1 addition & 1 deletion src/wasm/pkg/secp256k1_wasm.wasm.js

Large diffs are not rendered by default.

Binary file modified src/wasm/pkg/secp256k1_wasm_bg.wasm
Binary file not shown.
12 changes: 6 additions & 6 deletions src/wasm/pkg/secp256k1_wasm_bg.wasm.d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/* tslint:disable */
/* eslint-disable */
export const memory: WebAssembly.Memory;
export function __wbg_secp256k1signingkey_free(a: number, b: number): void;
export function secp256k1signingkey_new(): number;
export function secp256k1signingkey_from_bytes(a: number, b: number): void;
export function secp256k1signingkey_to_bytes(a: number): number;
export function secp256k1signingkey_verifying_key(a: number): number;
export function secp256k1signingkey_sign_prehash_recoverable(a: number, b: number, c: number): void;
export function __wbg_secp256k1signatureandrecovery_free(a: number, b: number): void;
export function secp256k1signatureandrecovery_to_bytes(a: number): number;
export function __wbg_secp256k1verifyingkey_free(a: number, b: number): void;
export function secp256k1verifyingkey_from_sec1_bytes(a: number, b: number): void;
export function secp256k1verifyingkey_recover_from_prehash(a: number, b: number, c: number): void;
export function secp256k1verifyingkey_to_sec1_compressed_bytes(a: number): number;
export function secp256k1verifyingkey_to_sec1_uncompressed_bytes(a: number): number;
export function __wbg_secp256k1signingkey_free(a: number, b: number): void;
export function secp256k1signingkey_new(): number;
export function secp256k1signingkey_from_bytes(a: number, b: number): void;
export function secp256k1signingkey_to_bytes(a: number): number;
export function secp256k1signingkey_verifying_key(a: number): number;
export function secp256k1signingkey_sign_prehash_recoverable(a: number, b: number, c: number): void;
export function secp256k1signingkey_random(): number;
export function __wbg_memory_free(a: number, b: number): void;
export function memory_new(a: number, b: number): number;
Expand Down
8 changes: 6 additions & 2 deletions src/wasm/src/mods/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
mod secp256k1;
mod signature;
mod signing_key;
mod verifying_key;

pub use secp256k1::*;
pub use signature::*;
pub use signing_key::*;
pub use verifying_key::*;
121 changes: 0 additions & 121 deletions src/wasm/src/mods/secp256k1/mod.rs

This file was deleted.

19 changes: 19 additions & 0 deletions src/wasm/src/mods/signature/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use wasm_bindgen::prelude::*;

use memory_wasm::Memory;

#[wasm_bindgen]
pub struct Secp256k1SignatureAndRecovery {
pub(crate) signature: k256::ecdsa::Signature,
pub(crate) recovery: k256::ecdsa::RecoveryId,
}

#[wasm_bindgen]
impl Secp256k1SignatureAndRecovery {
#[wasm_bindgen]
pub fn to_bytes(&self) -> Memory {
let mut bytes = self.signature.to_bytes().to_vec();
bytes.push(self.recovery.to_byte());
Memory::new(bytes)
}
}
66 changes: 66 additions & 0 deletions src/wasm/src/mods/signing_key/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use wasm_bindgen::prelude::*;

use crate::Secp256k1SignatureAndRecovery;
use crate::Secp256k1VerifyingKey;

use memory_wasm::Memory;

#[wasm_bindgen]
pub struct Secp256k1SigningKey {
pub(crate) inner: k256::ecdsa::SigningKey,
}

#[wasm_bindgen]
impl Secp256k1SigningKey {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self::random()
}

#[wasm_bindgen]
pub fn random() -> Self {
let inner = k256::ecdsa::SigningKey::random(&mut rand_core::OsRng {});

Self { inner }
}

#[wasm_bindgen]
pub fn from_bytes(input: &Memory) -> Result<Secp256k1SigningKey, JsError> {
use k256::elliptic_curve::generic_array::GenericArray;

let array = GenericArray::from_slice(&input.inner);
let result = k256::ecdsa::SigningKey::from_bytes(array);
let inner = result.map_err(|_| JsError::new("SigningKey::from_bytes"))?;

Ok(Self { inner })
}

#[wasm_bindgen]
pub fn to_bytes(&self) -> Memory {
Memory::new(self.inner.to_bytes().to_vec())
}

#[wasm_bindgen]
pub fn verifying_key(&self) -> Secp256k1VerifyingKey {
let inner = self.inner.verifying_key().clone();

Secp256k1VerifyingKey { inner }
}

#[wasm_bindgen]
pub fn sign_prehash_recoverable(
&self,
hashed: &Memory,
) -> Result<Secp256k1SignatureAndRecovery, JsError> {
let rsign = self.inner.sign_prehash_recoverable(&hashed.inner);
let tuple = rsign.map_err(|_| JsError::new("SigningKey::sign_prehash_recoverable"))?;
let (signature0, recovery) = tuple;

let signature = signature0.normalize_s().unwrap_or(signature0);

Ok(Secp256k1SignatureAndRecovery {
signature,
recovery,
})
}
}
47 changes: 47 additions & 0 deletions src/wasm/src/mods/verifying_key/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use wasm_bindgen::prelude::*;

use crate::Secp256k1SignatureAndRecovery;

use memory_wasm::Memory;

#[wasm_bindgen]
pub struct Secp256k1VerifyingKey {
pub(crate) inner: k256::ecdsa::VerifyingKey,
}

#[wasm_bindgen]
impl Secp256k1VerifyingKey {
#[wasm_bindgen]
pub fn from_sec1_bytes(input: &Memory) -> Result<Secp256k1VerifyingKey, JsError> {
let result = k256::ecdsa::VerifyingKey::from_sec1_bytes(&input.inner);
let inner = result.map_err(|_| JsError::new("Secp256k1VerifyingKey::from_sec1_bytes"))?;

Ok(Self { inner })
}

#[wasm_bindgen]
pub fn recover_from_prehash(
hashed: &Memory,
signature: &Secp256k1SignatureAndRecovery,
) -> Result<Secp256k1VerifyingKey, JsError> {
let result = k256::ecdsa::VerifyingKey::recover_from_prehash(
&hashed.inner,
&signature.signature,
signature.recovery,
);
let inner =
result.map_err(|_| JsError::new("Secp256k1VerifyingKey::recover_from_prehash"))?;

Ok(Secp256k1VerifyingKey { inner })
}

#[wasm_bindgen]
pub fn to_sec1_compressed_bytes(&self) -> Memory {
Memory::new(self.inner.to_encoded_point(true).to_bytes().into())
}

#[wasm_bindgen]
pub fn to_sec1_uncompressed_bytes(&self) -> Memory {
Memory::new(self.inner.to_encoded_point(false).to_bytes().into())
}
}

0 comments on commit 4fd8c08

Please sign in to comment.