@inversealtruism/csd-crypto 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 InverseAltruism
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ ADDR_RE: () => ADDR_RE,
24
+ addrFromPriv: () => addrFromPriv,
25
+ addrFromPub: () => addrFromPub,
26
+ buildScriptSig: () => buildScriptSig,
27
+ hash160: () => hash160,
28
+ isValidAddr: () => isValidAddr,
29
+ isValidPriv: () => isValidPriv,
30
+ keygen: () => keygen,
31
+ pubFromPriv: () => pubFromPriv,
32
+ randomNonce: () => randomNonce,
33
+ signDigest: () => signDigest,
34
+ verifyDigest: () => verifyDigest
35
+ });
36
+ module.exports = __toCommonJS(index_exports);
37
+ var import_secp256k1 = require("@noble/curves/secp256k1");
38
+ var import_ripemd160 = require("@noble/hashes/ripemd160");
39
+ var import_sha256 = require("@noble/hashes/sha256");
40
+ var import_utils = require("@noble/hashes/utils");
41
+ var strip0x = (h) => h.startsWith("0x") ? h.slice(2) : h;
42
+ var hb = (h) => (0, import_utils.hexToBytes)(strip0x(h));
43
+ var hx = (b) => "0x" + (0, import_utils.bytesToHex)(b);
44
+ function hash160(bytes) {
45
+ return hx((0, import_ripemd160.ripemd160)((0, import_sha256.sha256)(bytes)));
46
+ }
47
+ function pubFromPriv(priv) {
48
+ return hx(import_secp256k1.secp256k1.getPublicKey(hb(priv), true));
49
+ }
50
+ function addrFromPub(pub33) {
51
+ return hash160(hb(pub33));
52
+ }
53
+ function addrFromPriv(priv) {
54
+ return hash160(import_secp256k1.secp256k1.getPublicKey(hb(priv), true));
55
+ }
56
+ var ADDR_RE = /^0x[0-9a-fA-F]{40}$/;
57
+ function isValidAddr(a) {
58
+ return ADDR_RE.test(a);
59
+ }
60
+ function isValidPriv(h) {
61
+ try {
62
+ const b = hb(h);
63
+ return b.length === 32 && import_secp256k1.secp256k1.utils.isValidPrivateKey(b);
64
+ } catch {
65
+ return false;
66
+ }
67
+ }
68
+ function keygen() {
69
+ const k = import_secp256k1.secp256k1.utils.randomPrivateKey();
70
+ const priv = hx(k);
71
+ return { priv, pub: pubFromPriv(priv), addr: addrFromPriv(priv) };
72
+ }
73
+ var randomNonce = () => (0, import_utils.bytesToHex)((0, import_utils.randomBytes)(32));
74
+ function signDigest(digestHex, priv) {
75
+ const sig = import_secp256k1.secp256k1.sign(hb(digestHex), hb(priv), { lowS: true });
76
+ return { sig64: hx(sig.toCompactRawBytes()), pub33: pubFromPriv(priv) };
77
+ }
78
+ function verifyDigest(sig64, pub33, digestHex) {
79
+ const s = hb(sig64), p = hb(pub33);
80
+ if (s.length !== 64 || p.length !== 33) return false;
81
+ try {
82
+ if (import_secp256k1.secp256k1.Signature.fromCompact(s).hasHighS()) return false;
83
+ return import_secp256k1.secp256k1.verify(s, hb(digestHex), p, { lowS: true });
84
+ } catch {
85
+ return false;
86
+ }
87
+ }
88
+ function buildScriptSig(sig64, pub33) {
89
+ return "0x40" + strip0x(sig64) + "21" + strip0x(pub33);
90
+ }
91
+ // Annotate the CommonJS export names for ESM import in node:
92
+ 0 && (module.exports = {
93
+ ADDR_RE,
94
+ addrFromPriv,
95
+ addrFromPub,
96
+ buildScriptSig,
97
+ hash160,
98
+ isValidAddr,
99
+ isValidPriv,
100
+ keygen,
101
+ pubFromPriv,
102
+ randomNonce,
103
+ signDigest,
104
+ verifyDigest
105
+ });
@@ -0,0 +1,26 @@
1
+ /** hash160(x) = ripemd160(sha256(x)) → 0x-hex (20 bytes). */
2
+ declare function hash160(bytes: Uint8Array): string;
3
+ declare function pubFromPriv(priv: string): string;
4
+ declare function addrFromPub(pub33: string): string;
5
+ declare function addrFromPriv(priv: string): string;
6
+ /** A valid 0x..40-hex CSD address (raw hash160). */
7
+ declare const ADDR_RE: RegExp;
8
+ declare function isValidAddr(a: string): boolean;
9
+ declare function isValidPriv(h: string): boolean;
10
+ declare function keygen(): {
11
+ priv: string;
12
+ pub: string;
13
+ addr: string;
14
+ };
15
+ declare const randomNonce: () => string;
16
+ /** Sign a 32-byte digest (e.g. a sighash). RFC6979 + LOW-S; returns compact 64-byte sig + pub33. */
17
+ declare function signDigest(digestHex: string, priv: string): {
18
+ sig64: string;
19
+ pub33: string;
20
+ };
21
+ /** Verify a compact-64 LOW-S signature over a digest. Rejects high-S (malleability). */
22
+ declare function verifyDigest(sig64: string, pub33: string, digestHex: string): boolean;
23
+ /** p2pkh scriptSig = 0x40 ‖ sig64 ‖ 0x21 ‖ pub33 (the 99-byte spend script). */
24
+ declare function buildScriptSig(sig64: string, pub33: string): string;
25
+
26
+ export { ADDR_RE, addrFromPriv, addrFromPub, buildScriptSig, hash160, isValidAddr, isValidPriv, keygen, pubFromPriv, randomNonce, signDigest, verifyDigest };
@@ -0,0 +1,26 @@
1
+ /** hash160(x) = ripemd160(sha256(x)) → 0x-hex (20 bytes). */
2
+ declare function hash160(bytes: Uint8Array): string;
3
+ declare function pubFromPriv(priv: string): string;
4
+ declare function addrFromPub(pub33: string): string;
5
+ declare function addrFromPriv(priv: string): string;
6
+ /** A valid 0x..40-hex CSD address (raw hash160). */
7
+ declare const ADDR_RE: RegExp;
8
+ declare function isValidAddr(a: string): boolean;
9
+ declare function isValidPriv(h: string): boolean;
10
+ declare function keygen(): {
11
+ priv: string;
12
+ pub: string;
13
+ addr: string;
14
+ };
15
+ declare const randomNonce: () => string;
16
+ /** Sign a 32-byte digest (e.g. a sighash). RFC6979 + LOW-S; returns compact 64-byte sig + pub33. */
17
+ declare function signDigest(digestHex: string, priv: string): {
18
+ sig64: string;
19
+ pub33: string;
20
+ };
21
+ /** Verify a compact-64 LOW-S signature over a digest. Rejects high-S (malleability). */
22
+ declare function verifyDigest(sig64: string, pub33: string, digestHex: string): boolean;
23
+ /** p2pkh scriptSig = 0x40 ‖ sig64 ‖ 0x21 ‖ pub33 (the 99-byte spend script). */
24
+ declare function buildScriptSig(sig64: string, pub33: string): string;
25
+
26
+ export { ADDR_RE, addrFromPriv, addrFromPub, buildScriptSig, hash160, isValidAddr, isValidPriv, keygen, pubFromPriv, randomNonce, signDigest, verifyDigest };
package/dist/index.js ADDED
@@ -0,0 +1,69 @@
1
+ // src/index.ts
2
+ import { secp256k1 } from "@noble/curves/secp256k1";
3
+ import { ripemd160 } from "@noble/hashes/ripemd160";
4
+ import { sha256 } from "@noble/hashes/sha256";
5
+ import { bytesToHex, hexToBytes, randomBytes } from "@noble/hashes/utils";
6
+ var strip0x = (h) => h.startsWith("0x") ? h.slice(2) : h;
7
+ var hb = (h) => hexToBytes(strip0x(h));
8
+ var hx = (b) => "0x" + bytesToHex(b);
9
+ function hash160(bytes) {
10
+ return hx(ripemd160(sha256(bytes)));
11
+ }
12
+ function pubFromPriv(priv) {
13
+ return hx(secp256k1.getPublicKey(hb(priv), true));
14
+ }
15
+ function addrFromPub(pub33) {
16
+ return hash160(hb(pub33));
17
+ }
18
+ function addrFromPriv(priv) {
19
+ return hash160(secp256k1.getPublicKey(hb(priv), true));
20
+ }
21
+ var ADDR_RE = /^0x[0-9a-fA-F]{40}$/;
22
+ function isValidAddr(a) {
23
+ return ADDR_RE.test(a);
24
+ }
25
+ function isValidPriv(h) {
26
+ try {
27
+ const b = hb(h);
28
+ return b.length === 32 && secp256k1.utils.isValidPrivateKey(b);
29
+ } catch {
30
+ return false;
31
+ }
32
+ }
33
+ function keygen() {
34
+ const k = secp256k1.utils.randomPrivateKey();
35
+ const priv = hx(k);
36
+ return { priv, pub: pubFromPriv(priv), addr: addrFromPriv(priv) };
37
+ }
38
+ var randomNonce = () => bytesToHex(randomBytes(32));
39
+ function signDigest(digestHex, priv) {
40
+ const sig = secp256k1.sign(hb(digestHex), hb(priv), { lowS: true });
41
+ return { sig64: hx(sig.toCompactRawBytes()), pub33: pubFromPriv(priv) };
42
+ }
43
+ function verifyDigest(sig64, pub33, digestHex) {
44
+ const s = hb(sig64), p = hb(pub33);
45
+ if (s.length !== 64 || p.length !== 33) return false;
46
+ try {
47
+ if (secp256k1.Signature.fromCompact(s).hasHighS()) return false;
48
+ return secp256k1.verify(s, hb(digestHex), p, { lowS: true });
49
+ } catch {
50
+ return false;
51
+ }
52
+ }
53
+ function buildScriptSig(sig64, pub33) {
54
+ return "0x40" + strip0x(sig64) + "21" + strip0x(pub33);
55
+ }
56
+ export {
57
+ ADDR_RE,
58
+ addrFromPriv,
59
+ addrFromPub,
60
+ buildScriptSig,
61
+ hash160,
62
+ isValidAddr,
63
+ isValidPriv,
64
+ keygen,
65
+ pubFromPriv,
66
+ randomNonce,
67
+ signDigest,
68
+ verifyDigest
69
+ };
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@inversealtruism/csd-crypto",
3
+ "version": "0.1.0",
4
+ "description": "Compute Substrate crypto primitives — secp256k1 keygen/sign(lowS)/verify, hash160 address derivation. Thin wrappers over @noble.",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "LICENSE"
19
+ ],
20
+ "dependencies": {
21
+ "@noble/curves": "1.9.7",
22
+ "@noble/hashes": "1.8.0",
23
+ "@inversealtruism/csd-codec": "0.1.0"
24
+ },
25
+ "license": "MIT",
26
+ "sideEffects": false,
27
+ "devDependencies": {
28
+ "@inversealtruism/csd-vectors": "0.1.0"
29
+ },
30
+ "publishConfig": {
31
+ "access": "public"
32
+ },
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "git+https://github.com/InverseAltruism/csd-sdk.git",
36
+ "directory": "packages/crypto"
37
+ },
38
+ "homepage": "https://cairn-substrate.com",
39
+ "scripts": {
40
+ "build": "tsup src/index.ts --format esm,cjs --dts --clean",
41
+ "test": "tsx test/crypto.test.ts"
42
+ }
43
+ }