feat(ios,android): setup uniffi infra (#8828)

This commit is contained in:
Brooooooklyn
2024-12-10 03:43:34 +00:00
parent 95597ec139
commit adc69548ef
26 changed files with 3097 additions and 124 deletions
+9 -1
View File
@@ -4,13 +4,21 @@ name = "affine_native"
version = "0.0.0"
[lib]
crate-type = ["cdylib"]
crate-type = ["rlib", "cdylib"]
[features]
noop = ["napi/noop", "napi-derive/noop"]
[[bench]]
name = "hashcash"
harness = false
[dependencies]
affine_common = { workspace = true }
affine_schema = { path = "./schema" }
anyhow = { workspace = true }
chrono = { workspace = true }
criterion2 = { workspace = true }
napi = { workspace = true }
napi-derive = { workspace = true }
notify = { workspace = true, features = ["serde"] }
@@ -0,0 +1,28 @@
use std::hint::black_box;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use affine_native::hashcash::Stamp;
fn bench_hashcash(c: &mut Criterion) {
let mut group = c.benchmark_group("hashcash");
group.bench_function(BenchmarkId::from_parameter("Generate"), |b| {
b.iter(|| {
black_box(Stamp::mint("test".to_string(), Some(20)).format());
});
});
group.bench_function(BenchmarkId::from_parameter("Verify"), |b| {
b.iter(|| {
black_box(
Stamp::try_from("1:20:20241114061212:test::RsRAAkoxjr4FattQ:292f0d")
.unwrap()
.check(20, "test"),
);
});
});
}
criterion_group!(benches, bench_hashcash);
criterion_main!(benches);
+1
View File
@@ -14,6 +14,7 @@ async fn main() -> Result<(), std::io::Error> {
fs::remove_file(db_path)?;
}
#[cfg(not(feature = "noop"))]
napi_build::setup();
let options = SqliteConnectOptions::new()
.filename(db_path)
+9 -9
View File
@@ -1,7 +1,7 @@
use std::convert::TryFrom;
use affine_common::hashcash::Stamp;
use napi::{bindgen_prelude::AsyncTask, Env, JsBoolean, JsString, Result as NapiResult, Task};
use napi::{bindgen_prelude::AsyncTask, Env, Result, Task};
use napi_derive::napi;
pub struct AsyncVerifyChallengeResponse {
@@ -13,9 +13,9 @@ pub struct AsyncVerifyChallengeResponse {
#[napi]
impl Task for AsyncVerifyChallengeResponse {
type Output = bool;
type JsValue = JsBoolean;
type JsValue = bool;
fn compute(&mut self) -> NapiResult<Self::Output> {
fn compute(&mut self) -> Result<Self::Output> {
Ok(if let Ok(stamp) = Stamp::try_from(self.response.as_str()) {
stamp.check(self.bits, &self.resource)
} else {
@@ -23,8 +23,8 @@ impl Task for AsyncVerifyChallengeResponse {
})
}
fn resolve(&mut self, env: Env, output: bool) -> NapiResult<Self::JsValue> {
env.get_boolean(output)
fn resolve(&mut self, _: Env, output: bool) -> Result<Self::JsValue> {
Ok(output)
}
}
@@ -49,14 +49,14 @@ pub struct AsyncMintChallengeResponse {
#[napi]
impl Task for AsyncMintChallengeResponse {
type Output = String;
type JsValue = JsString;
type JsValue = String;
fn compute(&mut self) -> NapiResult<Self::Output> {
fn compute(&mut self) -> Result<Self::Output> {
Ok(Stamp::mint(self.resource.clone(), self.bits).format())
}
fn resolve(&mut self, env: Env, output: String) -> NapiResult<Self::JsValue> {
env.create_string(&output)
fn resolve(&mut self, _: Env, output: String) -> Result<Self::JsValue> {
Ok(output)
}
}