Initial commit
Once again a weird place to commit, I have already done a lot of work, but I am just bad at using git, okay.
This commit is contained in:
60
packages/lib/README.md
Normal file
60
packages/lib/README.md
Normal file
@@ -0,0 +1,60 @@
|
||||
# @impost/lib
|
||||
|
||||
This package contains the types and WASM code for the impost. It is useful for
|
||||
building other packages that want to use the WASM code, a client, or a server.
|
||||
|
||||
## Usage
|
||||
|
||||
```ts
|
||||
import {
|
||||
validate_challenge,
|
||||
generate_target_number_challenge,
|
||||
} from "@impost/lib/validator";
|
||||
import {
|
||||
init_solver,
|
||||
get_wasm_module,
|
||||
solve_target_number_challenge,
|
||||
} from "@impost/lib/solver";
|
||||
import { type Challenge, ChallengeStrategy } from "@impost/lib";
|
||||
|
||||
const challenge = await generate_target_number_challenge(
|
||||
// timeout in ms
|
||||
{
|
||||
// impost automatically recognizes this parameter, its also recommended to
|
||||
// add a timestamp to prevent replay or pre-computation attacks
|
||||
expires_at: Date.now() + 3600,
|
||||
}
|
||||
// max number
|
||||
1_000
|
||||
);
|
||||
|
||||
if (challenge === null) {
|
||||
throw new Error("Failed to generate challenge");
|
||||
}
|
||||
|
||||
let solver = await init_solver(
|
||||
{
|
||||
__get_solution: () => Atomics.load(atomic_solution!, 0),
|
||||
__set_solution: (value: number) =>
|
||||
Atomics.store(atomic_solution!, 0, value),
|
||||
__cmpxchg_solution: (expected: number, replacement: number) =>
|
||||
Atomics.compareExchange(atomic_solution!, 0, expected, replacement),
|
||||
__fetch_add_nonce: (value: number) => Atomics.add(atomic_nonce!, 0, value),
|
||||
},
|
||||
await get_wasm_module()
|
||||
);
|
||||
|
||||
const solution = await solve_target_number_challenge(solver, {
|
||||
salt: challenge.salt,
|
||||
target: challenge.target,
|
||||
});
|
||||
|
||||
const is_valid = await validate_challenge(challenge, {
|
||||
challenge: challenge.target,
|
||||
nonce: solution,
|
||||
});
|
||||
|
||||
if (is_valid) {
|
||||
console.log("Challenge solved!", solution);
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user