Commit eff50ab7 authored by Éloïs's avatar Éloïs

first commit

parents
Pipeline #3647 failed with stages
/bin
/pkg
/target
**/*.rs.bk
Cargo.lock
wasm-pack.log
stages:
- fmt
- tests
- clippy
- publish
variables:
CARGO_HOME: $CI_PROJECT_DIR/cargo
.rust_stable_wasm: &rust_stable_wasm
image: registry.duniter.org/docker/rust/wasm:v0.0.2
tags:
- redshift-rs-stable
before_script:
- export PATH="$HOME/.cargo/bin:$PATH"
- rustc --version && cargo --version
fmt:
<<: *rust_stable_wasm
stage: fmt
before_script:
- rustup show
- rustup component add rustfmt-preview
- cargo fmt -- --version
script:
- cargo fmt -- --check
allow_failure: true
tests:wasm:stable:
<<: *rust_stable_wasm
stage: tests
tags:
- redshift-rs-stable
script:
- wasm-pack test --headless --firefox
clippy:
image: guangie88/rustfmt-clippy:nightly
tags:
- redshift-rs-nightly
before_script:
- export PATH="$HOME/.cargo/bin:$PATH"
- rustc --version && cargo --version
stage: clippy
script:
- cargo clippy --all -- -D warnings --verbose
allow_failure: true
publish:npm:
<<: *rust_stable_wasm
stage: publish
tags:
- redshift-rs-stable
script:
- wasm-pack publish
\ No newline at end of file
[package]
name = "dup-tools-wasm"
version = "0.1.0"
authors = ["librelois <elois@ifee.fr>"]
description = "Tools for DUP Protocol."
license = "AGPL-3.0"
[lib]
crate-type = ["cdylib", "rlib"]
[features]
default = ["console_error_panic_hook"]
[dependencies]
cfg-if = "0.1.2"
dup-crypto = "0.4.0-b1"
wasm-bindgen = "0.2"
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.1", optional = true }
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.2", optional = true }
[dev-dependencies]
wasm-bindgen-test = "0.2"
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
This diff is collapsed.
<meta charset="utf-8"/>
# DUP Tools
Tools for DUP Protocol.
### 🛠️ Build with `wasm-pack build`
```
wasm-pack build
```
### 🔬 Test in Headless Browsers with `wasm-pack test`
```
wasm-pack test --headless --firefox
```
### 🎁 Publish to NPM with `wasm-pack publish`
```
wasm-pack publish
```
extern crate cfg_if;
extern crate dup_crypto;
extern crate wasm_bindgen;
mod utils;
use cfg_if::cfg_if;
use dup_crypto::keys::*;
use wasm_bindgen::prelude::*;
cfg_if! {
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
if #[cfg(feature = "wee_alloc")] {
extern crate wee_alloc;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
}
}
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet() {
alert("Hello, dup-tools-wasm 2 !");
}
#[wasm_bindgen]
pub fn verify(datas: &str, pubkey: &str, signature: &str) -> bool {
let pubkey = match ed25519::PublicKey::from_base58(pubkey) {
Ok(pubkey) => pubkey,
Err(_) => return false,
};
let sig = match ed25519::Signature::from_base64(signature) {
Ok(signature) => signature,
Err(_) => return false,
};
pubkey.verify(datas.as_bytes(), &sig)
}
#[wasm_bindgen]
pub fn generate_ed25519_keypair(salt: &str, password: &str) -> String {
let keypair = ed25519::KeyPairFromSaltedPasswordGenerator::with_default_parameters()
.generate(salt.as_bytes(), password.as_bytes());
format!("{}:{}", keypair.pubkey, keypair.privkey)
}
#[wasm_bindgen]
pub fn generate_ed25519_pubkey(salt: &str, password: &str) -> String {
let keypair = ed25519::KeyPairFromSaltedPasswordGenerator::with_default_parameters()
.generate(salt.as_bytes(), password.as_bytes());
format!("{}", keypair.pubkey)
}
use cfg_if::cfg_if;
cfg_if! {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
if #[cfg(feature = "console_error_panic_hook")] {
extern crate console_error_panic_hook;
pub use self::console_error_panic_hook::set_once as set_panic_hook;
} else {
#[inline]
pub fn set_panic_hook() {}
}
}
//! Test suite for the Web and headless browsers.
#![cfg(target_arch = "wasm32")]
extern crate dup_tools_wasm;
extern crate wasm_bindgen_test;
use dup_tools_wasm::*;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
fn pass() {
assert_eq!(1 + 1, 2);
}
#[wasm_bindgen_test]
fn test_verify_sig() {
assert!(verify(
"WS2POCAIC:HEAD:1:4C4jsvxmFQBoHN86BHsSreTKoK2bYvQV9gAGtH3BMNc3:167482-000000309D633082BCF18046F1291D28EA8912C85B4BEA437E46D1EF342E9E1F:26856cc8:duniter:1.6.25:1",
"4C4jsvxmFQBoHN86BHsSreTKoK2bYvQV9gAGtH3BMNc3",
"WDgS+ZtHgMLjcBOVCRAelldrfYpJka64A8ICIwQW4dX/AR/bNrMfbj3hSZSzF+27ClJ4Z6wH1jDwMHHGOiNsCQ=="
));
}
#[wasm_bindgen_test]
fn test_generate_ed25519_keypair() {
let pubkey = generate_ed25519_pubkey(
"JhxtHB7UcsDbA9wMSyMKXUzBZUQvqVyB32KwzS9SWoLkjrUhHV",
"JhxtHB7UcsDbA9wMSyMKXUzBZUQvqVyB32KwzS9SWoLkjrUhHV_",
);
assert_eq!(
pubkey.as_str(),
"7iMV3b6j2hSj5WtrfchfvxivS9swN3opDgxudeHq64fb"
);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment