lunesrs/wallet/
assembly.rs

1use wasm_bindgen::prelude::wasm_bindgen;
2
3/**
4# Hidden your seed fist with *`blake 2b`* then *`keccak 256`* finally *`sha 256`*
5
6- The function hash (hidden) your seed and return a array of bytes(Vec u8)
7
8## In JavaScript 👍
9
10```javascript
11import * wasm from "lunesrs"
12
13const seed = "scrub guard swim catch range upon dawn ensure segment alpha sentence spend effort bar benefit"
14const hiddedSeed: Uint8Array = wasm.hiddenSeed(0, seed)
15
16hiddedSeed == [
17    163, 66, 17, 225, 21, 144, 128, 203, 241, 21, 205, 209, 16, 138, 219, 155, 50, 48, 24, 209, 227, 79, 35, 104, 252, 102, 213, 74, 63, 165, 20, 96
18]
19```
20
21## In Rust 🤝
22
23```rust
24use lunesrs::wallet::assembly::hidden_seed;
25
26let seed = "scrub guard swim catch range upon dawn ensure segment alpha sentence spend effort bar benefit".to_string();
27let hidded_seed = hidden_seed(0, seed);
28
29assert_eq!(
30    hidded_seed,
31    [163, 66, 17, 225, 21, 144, 128, 203, 241, 21, 205, 209, 16, 138, 219, 155, 50, 48, 24, 209, 227, 79, 35, 104, 252, 102, 213, 74, 63, 165, 20, 96]
32)
33```
34*/
35#[wasm_bindgen(js_name = "hiddenSeed")]
36pub fn hidden_seed(nonce: u32, seed: String) -> Vec<u8> {
37    use crate::utils::crypto::{to_blake2b32b, to_keccak256, to_sha256};
38
39    let raw_seed = [nonce.to_be_bytes().to_vec(), seed.as_bytes().to_vec()].concat();
40    to_sha256(to_keccak256(to_blake2b32b(raw_seed)))
41}
42
43/**
44# To Private Key from Hidded Seed
45
46- Receive your hash seed (hidden seed) and return a private key using Ed25519-Axolotl
47- For more details about *how to works* click [here](https://crates.io/crates/ed25519-axolotl)
48
49## In JavaScript 👍
50
51```javascript
52import * wasm from "lunesrs"
53
54const hiddedSeed: Uint8Array = [
55    163, 66, 17, 225, 21, 144, 128, 203, 241, 21, 205, 209, 16, 138, 219, 155, 50, 48, 24, 209,
56    227, 79, 35, 104, 252, 102, 213, 74, 63, 165, 20, 96,
57]
58const privateKey: Uint8Array = wasm.to_privateKey(hiddedSeed)
59
60privateKey == [
61    160, 66, 17, 225, 21, 144, 128, 203, 241, 21, 205, 209, 16, 138, 219, 155, 50, 48, 24,
62    209, 227, 79, 35, 104, 252, 102, 213, 74, 63, 165, 20, 96,
63]
64```
65
66## In Rust 🤝
67
68```rust
69use lunesrs::wallet::assembly::to_private_key;
70
71let hidden_seed: Vec<u8> = vec![
72    163, 66, 17, 225, 21, 144, 128, 203, 241, 21, 205, 209, 16, 138, 219, 155, 50, 48, 24, 209,
73    227, 79, 35, 104, 252, 102, 213, 74, 63, 165, 20, 96,
74];
75
76assert_eq!(
77    to_private_key(hidden_seed),
78    [
79        160, 66, 17, 225, 21, 144, 128, 203, 241, 21, 205, 209, 16, 138, 219, 155, 50, 48, 24,
80        209, 227, 79, 35, 104, 252, 102, 213, 74, 63, 165, 20, 96,
81    ]
82);
83```
84*/
85#[wasm_bindgen(js_name = "toPrivateKey")]
86pub fn to_private_key(hidded_seed: Vec<u8>) -> Vec<u8> {
87    use crate::utils::vectors::{to_vecu32, to_vecu8};
88    use ed25519_axolotl::crypto::keys::KeyPair;
89
90    to_vecu8(KeyPair::new(Some(to_vecu32(hidded_seed))).prvk)
91}
92
93/**
94# To Public Key from your Private Key
95
96- Receive your private key and return a public key like a bytes using Ed25519-Axolotl
97- For more details about *how to works* click [here](https://crates.io/crates/ed25519-axolotl)
98
99## In JavaScript 👍
100
101```javascript
102import * wasm from "lunesrs"
103
104const privateKey: Uint8Array = [
105    160, 66, 17, 225, 21, 144, 128, 203, 241, 21, 205, 209, 16, 138, 219, 155, 50, 48, 24,
106    209, 227, 79, 35, 104, 252, 102, 213, 74, 63, 165, 20, 96,
107]
108const publicKey: Uint8Array = wasm.toPublicKey(privateKey)
109
110publicKey == [
111    28, 105, 36, 199, 36, 111, 120, 95, 152, 208, 215, 39, 161, 71, 78, 237, 200, 160, 71,
112    209, 177, 102, 140, 170, 56, 206, 9, 214, 227, 38, 117, 117,
113]
114```
115
116## In Rust 🤝
117
118```rust
119use lunesrs::wallet::assembly::to_public_key;
120
121let private_key: Vec<u8> = vec![
122    160, 66, 17, 225, 21, 144, 128, 203, 241, 21, 205, 209, 16, 138, 219, 155, 50, 48, 24, 209,
123    227, 79, 35, 104, 252, 102, 213, 74, 63, 165, 20, 96,
124];
125
126assert_eq!(
127    to_public_key(private_key),
128    [
129        28, 105, 36, 199, 36, 111, 120, 95, 152, 208, 215, 39, 161, 71, 78, 237, 200, 160, 71,
130        209, 177, 102, 140, 170, 56, 206, 9, 214, 227, 38, 117, 117,
131    ]
132);
133```
134*/
135#[wasm_bindgen(js_name = "toPublicKey")]
136pub fn to_public_key(private_key: Vec<u8>) -> Vec<u8> {
137    use crate::utils::vectors::{to_vecu32, to_vecu8};
138    use ed25519_axolotl::crypto::keys::KeyPair;
139
140    to_vecu8(KeyPair::new(Some(to_vecu32(private_key))).pubk)
141}
142
143/**
144# to Lunes Addres from Public Key
145
146- Receive the version, chain and public key and return your lunes address like a bytes
147- For more details how to works in [telescope](https://blockchain.lunes.io/telescope)
148
149## In JavaScript 👍
150
151```javascript
152import * wasm from "lunesrs"
153
154const versionAddress = 1
155const mainnet_id = 1
156const publicKey: Uint8Array = [
157    28, 105, 36, 199, 36, 111, 120, 95, 152, 208, 215, 39, 161, 71, 78, 237, 200, 160, 71, 209,
158    177, 102, 140, 170, 56, 206, 9, 214, 227, 38, 117, 117,
159]
160const address: Uint8Array = wasm.toAddress(versionAddress, mainnet_id, publicKey)
161
162address == [
163    28, 105, 36, 199, 36, 111, 120, 95, 152, 208, 215, 39, 161, 71, 78, 237, 200, 160, 71,
164    209, 177, 102, 140, 170, 56, 206, 9, 214, 227, 38, 117, 117,
165]
166```
167
168## In Rust 🤝
169
170```rust
171use lunesrs::wallet::assembly::to_address;
172
173let mainnet_id = 1;
174let version_address = 1;
175let public_key: Vec<u8> = vec![
176    28, 105, 36, 199, 36, 111, 120, 95, 152, 208, 215, 39, 161, 71, 78, 237, 200, 160, 71, 209,
177    177, 102, 140, 170, 56, 206, 9, 214, 227, 38, 117, 117,
178];
179
180assert_eq!(
181    to_address(version_address, mainnet_id, public_key),
182    [
183        1, 49, 44, 46, 82, 88, 220, 91, 204, 187, 92, 83, 89, 68, 39, 15, 115, 185, 143, 151,
184        57, 38, 99, 41, 200, 192,
185    ],
186);
187```
188*/
189#[wasm_bindgen(js_name = "toAddress")]
190pub fn to_address(version: u8, chain: u8, public_key: Vec<u8>) -> Vec<u8> {
191    use crate::utils::crypto::{to_blake2b32b, to_keccak256};
192
193    let raw_addr = {
194        let mut pubk = to_keccak256(to_blake2b32b(public_key))[0..20].to_vec();
195        pubk.insert(0, chain.to_string().as_bytes()[0]);
196        pubk.insert(0, version);
197
198        pubk
199    };
200    let checksum = to_keccak256(to_blake2b32b(raw_addr.clone()))[0..4].to_vec();
201
202    [raw_addr, checksum].concat()
203}