Skip to content

Commit

Permalink
Add examples (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid authored Mar 24, 2021
1 parent 3ee8dc7 commit f63b100
Show file tree
Hide file tree
Showing 12 changed files with 26,105 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/benches/node_modules
/examples/react-app/dist
/examples/react-app/node_modules
/lib
/node_modules
/target
/tests/browser
/types

/package-lock.json
15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,29 @@ build-wasm-debug:

.PHONY: clean
clean:
rm -rf benches/node_modules lib node_modules target tests/browser types
rm -rf \
benches/node_modules \
examples/react-app/dist/*.js \
examples/react-app/dist/*.wasm \
examples/react-app/node_modules \
lib \
node_modules \
target \
tests/browser \
types

.PHONY: format
format:
cargo-fmt
npx eslint benches/*.{js,json} src_ts/*.ts tests/*.js util/*.js *.json *.cjs --fix
npx eslint benches/*.{js,json} examples/**/*.{js,json} src_ts/*.ts tests/*.js util/*.js *.json *.cjs --fix
npx sort-package-json package.json benches/package.json

.PHONY: lint
lint:
cargo fmt -- --check
cargo clippy --package secp256k1-node
cargo clippy --package secp256k1-wasm --target wasm32-unknown-unknown
npx eslint benches/*.{js,json} src_ts/*.ts tests/*.js util/*.js *.json *.cjs
npx eslint benches/*.{js,json} examples/**/*.{js,json} src_ts/*.ts tests/*.js util/*.js *.json *.cjs

.PHONY: test
test: test-browser test-node
Expand Down
18 changes: 18 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

## random-in-node

Generate data and demonstrate arguments/result for different methods of `tiny-secp256k1`.

```
npm start
```

## react-app

[React](https://reactjs.org/) application with inputs for testing `tiny-secp256k1` methods.

```
npm start
```

and open [http://localhost:8080/](http://localhost:8080/).
44 changes: 44 additions & 0 deletions examples/random-in-node/bin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as secp256k1 from "../../lib/index.js";
import { generate } from "./index.js";

const toHex = (v) =>
v instanceof Uint8Array ? Buffer.from(v).toString("hex") : v;

const lineDash = new Array(80).fill("-").join("");
const lineEq = new Array(80).fill("=").join("");
function print(items) {
let firstPrint = true;
for (const item of items) {
if (firstPrint) {
console.log(lineEq);
firstPrint = false;
}
console.log(`Method: ${item.name}`);
console.log(lineDash);
for (let i = 0; i < item.args.length; ++i) {
console.log(`Arg${(i + 1).toString()}: ${toHex(item.args[i])}`);
}
console.log(`Result: ${toHex(secp256k1[item.name](...item.args))}`);
console.log(lineEq);
}
}

const data = generate();
print([
{ name: "isPoint", args: [data.pubkey_uncompressed] },
{ name: "isPointCompressed", args: [data.pubkey] },
{ name: "isPrivate", args: [data.seckey] },
{ name: "pointAdd", args: [data.pubkey, data.pubkey2] },
{ name: "pointAddScalar", args: [data.pubkey, data.tweak] },
{ name: "pointCompress", args: [data.pubkey_uncompressed, true] },
{ name: "pointFromScalar", args: [data.seckey] },
{ name: "pointMultiply", args: [data.pubkey, data.tweak] },
{ name: "privateAdd", args: [data.seckey, data.tweak] },
{ name: "privateSub", args: [data.seckey, data.tweak] },
{ name: "sign", args: [data.hash, data.seckey] },
{ name: "signWithEntropy", args: [data.hash, data.seckey, data.entropy] },
{
name: "verify",
args: [data.hash, data.pubkey, secp256k1.sign(data.hash, data.seckey)],
},
]);
60 changes: 60 additions & 0 deletions examples/random-in-node/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { randomBytes } from "crypto";
import * as secp256k1 from "../../lib/index.js";

const eq = (v1, v2) =>
v1.length === v2.length && v1.every((v, i) => v === v2[i]);

function isValidData(data) {
return (
secp256k1.isPoint(data.pubkey) &&
secp256k1.isPoint(data.pubkey_uncompressed) &&
secp256k1.isPoint(data.pubkey2) &&
secp256k1.isPointCompressed(data.pubkey) &&
secp256k1.isPointCompressed(data.pubkey2) &&
secp256k1.isPrivate(data.seckey) &&
secp256k1.isPrivate(data.seckey2) &&
secp256k1.pointAdd(data.pubkey, data.pubkey2) !== null &&
secp256k1.pointAddScalar(data.pubkey, data.tweak) !== null &&
secp256k1.pointAddScalar(data.pubkey2, data.tweak) !== null &&
eq(secp256k1.pointCompress(data.pubkey, false), data.pubkey_uncompressed) &&
eq(secp256k1.pointFromScalar(data.seckey, true), data.pubkey) &&
eq(
secp256k1.pointFromScalar(data.seckey, false),
data.pubkey_uncompressed
) &&
eq(secp256k1.pointFromScalar(data.seckey2, true), data.pubkey2) &&
secp256k1.pointMultiply(data.pubkey, data.tweak) !== null &&
secp256k1.pointMultiply(data.pubkey2, data.tweak) !== null &&
secp256k1.privateAdd(data.seckey, data.tweak) !== null &&
secp256k1.privateAdd(data.seckey2, data.tweak) !== null &&
secp256k1.privateSub(data.seckey, data.tweak) !== null &&
secp256k1.privateSub(data.seckey2, data.tweak) !== null &&
secp256k1.verify(
data.hash,
data.pubkey,
secp256k1.sign(data.hash, data.seckey)
)
);
}

export function generate() {
for (;;) {
const seckey = new Uint8Array(randomBytes(32));
const seckey2 = new Uint8Array(randomBytes(32));
const hash = new Uint8Array(randomBytes(32));

const data = {
seckey,
pubkey: secp256k1.pointFromScalar(seckey, true),
pubkey_uncompressed: secp256k1.pointFromScalar(seckey, false),
seckey2,
pubkey2: secp256k1.pointFromScalar(seckey2, true),
tweak: new Uint8Array(randomBytes(32)),
hash,
entropy: new Uint8Array(randomBytes(32)),
signature: secp256k1.sign(hash, seckey),
};

if (isValidData(data)) return data;
}
}
10 changes: 10 additions & 0 deletions examples/random-in-node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "random-in-node",
"version": "0.0.0",
"private": true,
"main": "./index.js",
"scripts": {
"start": "node bin.js"
},
"type": "module"
}
10 changes: 10 additions & 0 deletions examples/react-app/dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>tiny-secp256k1 React example</title>
</head>
<body>
<div id="app"></div>
<script src="./bundle.js"></script>
</body>
</html>
Loading

0 comments on commit f63b100

Please sign in to comment.