forked from bitcoinjs/tiny-secp256k1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.js
75 lines (71 loc) · 2.31 KB
/
bin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { toHex as _toHex } from "uint8array-tools";
import * as secp256k1 from "../../lib/index.js";
import { generate } from "./index.js";
const toHex = (v) => (v instanceof Uint8Array ? _toHex(v) : v);
const JSONstring = (data, spacing) =>
JSON.stringify(data, (_, val) => toHex(val), spacing);
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])}`);
}
const result = secp256k1[item.name](...item.args);
console.log(
`Result: ${
result instanceof Uint8Array ? toHex(result) : JSONstring(result)
}`
);
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: "xOnlyPointFromScalar", args: [data.seckey] },
{ name: "xOnlyPointFromPoint", args: [data.pubkey] },
{
name: "xOnlyPointAddTweak",
args: [data.x_only_pubkey, data.x_only_pubkey2],
},
{
name: "xOnlyPointAddTweakCheck",
args: [
data.x_only_pubkey,
data.x_only_add_tweak,
data.x_only_pubkey2,
data.x_only_add_parity,
],
},
{ 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, data.entropy] },
{ name: "signSchnorr", args: [data.hash, data.seckey, data.entropy] },
{
name: "verify",
args: [data.hash, data.pubkey, secp256k1.sign(data.hash, data.seckey)],
},
{
name: "verifySchnorr",
args: [
data.hash,
data.x_only_pubkey,
secp256k1.signSchnorr(data.hash, data.seckey),
],
},
]);