Skip to content

Commit

Permalink
Add mnemonic functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Nov 8, 2024
1 parent 6684d36 commit f599aed
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions napi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ num-bigint = { workspace = true }
hex = { workspace = true }
paste = { workspace = true }
tokio = { workspace = true, features = ["sync"] }
bip39 = { workspace = true }
rand = { workspace = true }
rand_chacha = { workspace = true }

[build-dependencies]
napi-build = "2.0.1"
Expand Down
6 changes: 6 additions & 0 deletions napi/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

/* auto-generated by NAPI-RS */

export declare function mnemonicFromEntropy(entropy: Uint8Array): string
export declare function mnemonicToEntropy(mnemonic: string): Uint8Array
export declare function verifyMnemonic(mnemonic: string): boolean
export declare function randomBytes(bytes: number): Uint8Array
export declare function generateMnemonic(use24: boolean): string
export declare function mnemonicToSeed(mnemonic: string, password: string): Uint8Array
export interface Output {
value: Program
cost: bigint
Expand Down
8 changes: 7 additions & 1 deletion napi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,17 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { SecretKey, PublicKey, Signature, ClvmAllocator, curryTreeHash, intToSignedBytes, signedBytesToInt, toCoinId, Tls, Peer, Program, Simulator, compareBytes, sha256, fromHexRaw, fromHex, toHex } = nativeBinding
const { SecretKey, PublicKey, Signature, mnemonicFromEntropy, mnemonicToEntropy, verifyMnemonic, randomBytes, generateMnemonic, mnemonicToSeed, ClvmAllocator, curryTreeHash, intToSignedBytes, signedBytesToInt, toCoinId, Tls, Peer, Program, Simulator, compareBytes, sha256, fromHexRaw, fromHex, toHex } = nativeBinding

module.exports.SecretKey = SecretKey
module.exports.PublicKey = PublicKey
module.exports.Signature = Signature
module.exports.mnemonicFromEntropy = mnemonicFromEntropy
module.exports.mnemonicToEntropy = mnemonicToEntropy
module.exports.verifyMnemonic = verifyMnemonic
module.exports.randomBytes = randomBytes
module.exports.generateMnemonic = generateMnemonic
module.exports.mnemonicToSeed = mnemonicToSeed
module.exports.ClvmAllocator = ClvmAllocator
module.exports.curryTreeHash = curryTreeHash
module.exports.intToSignedBytes = intToSignedBytes
Expand Down
57 changes: 56 additions & 1 deletion napi/src/bls.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::str::FromStr;

use bip39::Mnemonic;
use chia::{
bls::{
self, master_to_wallet_hardened, master_to_wallet_hardened_intermediate,
Expand All @@ -7,8 +10,10 @@ use chia::{
puzzles::DeriveSynthetic,
};
use napi::bindgen_prelude::*;
use rand::{Rng, RngCore, SeedableRng};
use rand_chacha::ChaCha20Rng;

use crate::traits::IntoRust;
use crate::traits::{IntoJs, IntoRust};

#[napi]
pub struct SecretKey(bls::SecretKey);
Expand Down Expand Up @@ -239,3 +244,53 @@ impl Signature {
self.0.is_valid()
}
}

#[napi]
pub fn mnemonic_from_entropy(entropy: Uint8Array) -> Result<String> {
Ok(Mnemonic::from_entropy(&entropy)
.map_err(|error| Error::from_reason(error.to_string()))?
.to_string())
}

#[napi]
pub fn mnemonic_to_entropy(mnemonic: String) -> Result<Uint8Array> {
Ok(Mnemonic::from_str(&mnemonic)
.map_err(|error| Error::from_reason(error.to_string()))?
.to_entropy()
.into())
}

#[napi]
pub fn verify_mnemonic(mnemonic: String) -> bool {
Mnemonic::from_str(&mnemonic).is_ok()
}

#[napi]
pub fn random_bytes(bytes: u32) -> Uint8Array {
let mut rng = ChaCha20Rng::from_entropy();
let mut buffer = vec![0; bytes as usize];
rng.fill_bytes(&mut buffer);
Uint8Array::new(buffer)
}

#[napi]
pub fn generate_mnemonic(use_24: bool) -> Result<String> {
let mut rng = ChaCha20Rng::from_entropy();

let mnemonic = if use_24 {
let entropy: [u8; 32] = rng.gen();
Mnemonic::from_entropy(&entropy).map_err(|error| Error::from_reason(error.to_string()))?
} else {
let entropy: [u8; 16] = rng.gen();
Mnemonic::from_entropy(&entropy).map_err(|error| Error::from_reason(error.to_string()))?
};

Ok(mnemonic.to_string())
}

#[napi]
pub fn mnemonic_to_seed(mnemonic: String, password: String) -> Result<Uint8Array> {
let mnemonic =
Mnemonic::from_str(&mnemonic).map_err(|error| Error::from_reason(error.to_string()))?;
mnemonic.to_seed(password).into_js()
}

0 comments on commit f599aed

Please sign in to comment.