@moltos/sdk 0.11.0 → 0.12.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/dist/chunk-TIRAZTCQ.mjs +148 -0
- package/dist/crypto.mjs +10 -131
- package/dist/index.d.mts +430 -8
- package/dist/index.d.ts +430 -8
- package/dist/index.js +563 -13
- package/dist/index.mjs +431 -14
- package/package.json +30 -11
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
// src/crypto.ts
|
|
2
|
+
import { createHash, randomBytes } from "crypto";
|
|
3
|
+
var STUB_MODE = true;
|
|
4
|
+
function setStubMode(enabled) {
|
|
5
|
+
STUB_MODE = enabled;
|
|
6
|
+
}
|
|
7
|
+
function isStubMode() {
|
|
8
|
+
return STUB_MODE;
|
|
9
|
+
}
|
|
10
|
+
function generateKeypair() {
|
|
11
|
+
const privateBytes = new Uint8Array(randomBytes(32));
|
|
12
|
+
const publicBytes = derivePublicKeyBytes(privateBytes);
|
|
13
|
+
return {
|
|
14
|
+
privateKey: {
|
|
15
|
+
bytes: privateBytes,
|
|
16
|
+
toHex: () => Buffer.from(privateBytes).toString("hex")
|
|
17
|
+
},
|
|
18
|
+
publicKey: {
|
|
19
|
+
type: "BLS12_381",
|
|
20
|
+
bytes: publicBytes,
|
|
21
|
+
toHex: () => Buffer.from(publicBytes).toString("hex")
|
|
22
|
+
},
|
|
23
|
+
mnemonic: generateMnemonic()
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function deriveKeypair(mnemonic, path = "m/44'/60'/0'/0/0") {
|
|
27
|
+
const seed = createHash("sha256").update(mnemonic + path).digest();
|
|
28
|
+
const privateBytes = new Uint8Array(seed.slice(0, 32));
|
|
29
|
+
const publicBytes = derivePublicKeyBytes(privateBytes);
|
|
30
|
+
return {
|
|
31
|
+
privateKey: {
|
|
32
|
+
bytes: privateBytes,
|
|
33
|
+
toHex: () => Buffer.from(privateBytes).toString("hex")
|
|
34
|
+
},
|
|
35
|
+
publicKey: {
|
|
36
|
+
type: "BLS12_381",
|
|
37
|
+
bytes: publicBytes,
|
|
38
|
+
toHex: () => Buffer.from(publicBytes).toString("hex")
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function hashPayload(payload) {
|
|
43
|
+
const canonical = JSON.stringify(payload, Object.keys(payload).sort());
|
|
44
|
+
const hash = createHash("sha256").update(canonical).digest();
|
|
45
|
+
return new Uint8Array(hash);
|
|
46
|
+
}
|
|
47
|
+
function signAttestation(payload, privateKey) {
|
|
48
|
+
const messageHash = hashPayload(payload);
|
|
49
|
+
const sigData = createHash("sha256").update(Buffer.from(messageHash)).update(Buffer.from(privateKey.bytes)).digest();
|
|
50
|
+
const sigBytes = new Uint8Array(sigData.slice(0, 48));
|
|
51
|
+
const signature = {
|
|
52
|
+
type: "BLS12_381",
|
|
53
|
+
bytes: sigBytes,
|
|
54
|
+
toHex: () => Buffer.from(sigBytes).toString("hex"),
|
|
55
|
+
aggregate: (other) => aggregateSignatures([sigBytes, other.bytes])
|
|
56
|
+
};
|
|
57
|
+
return {
|
|
58
|
+
payload,
|
|
59
|
+
signature,
|
|
60
|
+
publicKey: derivePublicKey(privateKey),
|
|
61
|
+
proof: {
|
|
62
|
+
type: "bls12_381",
|
|
63
|
+
scheme: "basic"
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function verifyAttestation(signed) {
|
|
68
|
+
if (STUB_MODE) {
|
|
69
|
+
console.warn("[TAP-SDK] BLS verification running in STUB mode");
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
throw new Error("Real BLS verification not yet implemented. Use stub mode for testing.");
|
|
73
|
+
}
|
|
74
|
+
function batchVerifyAttestations(attestations) {
|
|
75
|
+
if (STUB_MODE) {
|
|
76
|
+
const valid = attestations.map(() => true);
|
|
77
|
+
return { valid, allValid: true };
|
|
78
|
+
}
|
|
79
|
+
throw new Error("Batch verification not yet implemented");
|
|
80
|
+
}
|
|
81
|
+
function aggregateSignatures(signatures) {
|
|
82
|
+
const bytes = signatures.map((s) => s instanceof Uint8Array ? s : s.bytes);
|
|
83
|
+
const result = new Uint8Array(48);
|
|
84
|
+
for (const sig of bytes) {
|
|
85
|
+
for (let i = 0; i < 48 && i < sig.length; i++) {
|
|
86
|
+
result[i] ^= sig[i];
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
type: "BLS12_381",
|
|
91
|
+
bytes: result,
|
|
92
|
+
toHex: () => Buffer.from(result).toString("hex"),
|
|
93
|
+
aggregate: (other) => aggregateSignatures([result, other.bytes])
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function verifyAggregate(message, aggregateSig, publicKeys) {
|
|
97
|
+
if (STUB_MODE) {
|
|
98
|
+
console.warn("[TAP-SDK] Aggregate verification running in STUB mode");
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
throw new Error("Aggregate verification not yet implemented");
|
|
102
|
+
}
|
|
103
|
+
function derivePublicKeyBytes(privateBytes) {
|
|
104
|
+
const hash = createHash("sha256").update(privateBytes).digest();
|
|
105
|
+
const publicBytes = new Uint8Array(96);
|
|
106
|
+
for (let i = 0; i < 96; i++) {
|
|
107
|
+
publicBytes[i] = hash[i % hash.length] ^ privateBytes[i % privateBytes.length];
|
|
108
|
+
}
|
|
109
|
+
return publicBytes;
|
|
110
|
+
}
|
|
111
|
+
function derivePublicKey(privateKey) {
|
|
112
|
+
return {
|
|
113
|
+
type: "BLS12_381",
|
|
114
|
+
bytes: derivePublicKeyBytes(privateKey.bytes),
|
|
115
|
+
toHex: () => Buffer.from(derivePublicKeyBytes(privateKey.bytes)).toString("hex")
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function generateMnemonic() {
|
|
119
|
+
const words = ["abandon", "ability", "able", "about", "above", "absent", "absorb", "abstract"];
|
|
120
|
+
const phrase = Array(12).fill(0).map(() => words[Math.floor(Math.random() * words.length)]);
|
|
121
|
+
return phrase.join(" ");
|
|
122
|
+
}
|
|
123
|
+
var crypto_default = {
|
|
124
|
+
generateKeypair,
|
|
125
|
+
deriveKeypair,
|
|
126
|
+
signAttestation,
|
|
127
|
+
verifyAttestation,
|
|
128
|
+
batchVerifyAttestations,
|
|
129
|
+
aggregateSignatures,
|
|
130
|
+
verifyAggregate,
|
|
131
|
+
hashPayload,
|
|
132
|
+
setStubMode,
|
|
133
|
+
isStubMode
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export {
|
|
137
|
+
setStubMode,
|
|
138
|
+
isStubMode,
|
|
139
|
+
generateKeypair,
|
|
140
|
+
deriveKeypair,
|
|
141
|
+
hashPayload,
|
|
142
|
+
signAttestation,
|
|
143
|
+
verifyAttestation,
|
|
144
|
+
batchVerifyAttestations,
|
|
145
|
+
aggregateSignatures,
|
|
146
|
+
verifyAggregate,
|
|
147
|
+
crypto_default
|
|
148
|
+
};
|
package/dist/crypto.mjs
CHANGED
|
@@ -1,137 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
import { createHash, randomBytes } from "crypto";
|
|
3
|
-
var STUB_MODE = true;
|
|
4
|
-
function setStubMode(enabled) {
|
|
5
|
-
STUB_MODE = enabled;
|
|
6
|
-
}
|
|
7
|
-
function isStubMode() {
|
|
8
|
-
return STUB_MODE;
|
|
9
|
-
}
|
|
10
|
-
function generateKeypair() {
|
|
11
|
-
const privateBytes = new Uint8Array(randomBytes(32));
|
|
12
|
-
const publicBytes = derivePublicKeyBytes(privateBytes);
|
|
13
|
-
return {
|
|
14
|
-
privateKey: {
|
|
15
|
-
bytes: privateBytes,
|
|
16
|
-
toHex: () => Buffer.from(privateBytes).toString("hex")
|
|
17
|
-
},
|
|
18
|
-
publicKey: {
|
|
19
|
-
type: "BLS12_381",
|
|
20
|
-
bytes: publicBytes,
|
|
21
|
-
toHex: () => Buffer.from(publicBytes).toString("hex")
|
|
22
|
-
},
|
|
23
|
-
mnemonic: generateMnemonic()
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
function deriveKeypair(mnemonic, path = "m/44'/60'/0'/0/0") {
|
|
27
|
-
const seed = createHash("sha256").update(mnemonic + path).digest();
|
|
28
|
-
const privateBytes = new Uint8Array(seed.slice(0, 32));
|
|
29
|
-
const publicBytes = derivePublicKeyBytes(privateBytes);
|
|
30
|
-
return {
|
|
31
|
-
privateKey: {
|
|
32
|
-
bytes: privateBytes,
|
|
33
|
-
toHex: () => Buffer.from(privateBytes).toString("hex")
|
|
34
|
-
},
|
|
35
|
-
publicKey: {
|
|
36
|
-
type: "BLS12_381",
|
|
37
|
-
bytes: publicBytes,
|
|
38
|
-
toHex: () => Buffer.from(publicBytes).toString("hex")
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
function hashPayload(payload) {
|
|
43
|
-
const canonical = JSON.stringify(payload, Object.keys(payload).sort());
|
|
44
|
-
const hash = createHash("sha256").update(canonical).digest();
|
|
45
|
-
return new Uint8Array(hash);
|
|
46
|
-
}
|
|
47
|
-
function signAttestation(payload, privateKey) {
|
|
48
|
-
const messageHash = hashPayload(payload);
|
|
49
|
-
const sigData = createHash("sha256").update(Buffer.from(messageHash)).update(Buffer.from(privateKey.bytes)).digest();
|
|
50
|
-
const sigBytes = new Uint8Array(sigData.slice(0, 48));
|
|
51
|
-
const signature = {
|
|
52
|
-
type: "BLS12_381",
|
|
53
|
-
bytes: sigBytes,
|
|
54
|
-
toHex: () => Buffer.from(sigBytes).toString("hex"),
|
|
55
|
-
aggregate: (other) => aggregateSignatures([sigBytes, other.bytes])
|
|
56
|
-
};
|
|
57
|
-
return {
|
|
58
|
-
payload,
|
|
59
|
-
signature,
|
|
60
|
-
publicKey: derivePublicKey(privateKey),
|
|
61
|
-
proof: {
|
|
62
|
-
type: "bls12_381",
|
|
63
|
-
scheme: "basic"
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
function verifyAttestation(signed) {
|
|
68
|
-
if (STUB_MODE) {
|
|
69
|
-
console.warn("[TAP-SDK] BLS verification running in STUB mode");
|
|
70
|
-
return true;
|
|
71
|
-
}
|
|
72
|
-
throw new Error("Real BLS verification not yet implemented. Use stub mode for testing.");
|
|
73
|
-
}
|
|
74
|
-
function batchVerifyAttestations(attestations) {
|
|
75
|
-
if (STUB_MODE) {
|
|
76
|
-
const valid = attestations.map(() => true);
|
|
77
|
-
return { valid, allValid: true };
|
|
78
|
-
}
|
|
79
|
-
throw new Error("Batch verification not yet implemented");
|
|
80
|
-
}
|
|
81
|
-
function aggregateSignatures(signatures) {
|
|
82
|
-
const bytes = signatures.map((s) => s instanceof Uint8Array ? s : s.bytes);
|
|
83
|
-
const result = new Uint8Array(48);
|
|
84
|
-
for (const sig of bytes) {
|
|
85
|
-
for (let i = 0; i < 48 && i < sig.length; i++) {
|
|
86
|
-
result[i] ^= sig[i];
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return {
|
|
90
|
-
type: "BLS12_381",
|
|
91
|
-
bytes: result,
|
|
92
|
-
toHex: () => Buffer.from(result).toString("hex"),
|
|
93
|
-
aggregate: (other) => aggregateSignatures([result, other.bytes])
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
function verifyAggregate(message, aggregateSig, publicKeys) {
|
|
97
|
-
if (STUB_MODE) {
|
|
98
|
-
console.warn("[TAP-SDK] Aggregate verification running in STUB mode");
|
|
99
|
-
return true;
|
|
100
|
-
}
|
|
101
|
-
throw new Error("Aggregate verification not yet implemented");
|
|
102
|
-
}
|
|
103
|
-
function derivePublicKeyBytes(privateBytes) {
|
|
104
|
-
const hash = createHash("sha256").update(privateBytes).digest();
|
|
105
|
-
const publicBytes = new Uint8Array(96);
|
|
106
|
-
for (let i = 0; i < 96; i++) {
|
|
107
|
-
publicBytes[i] = hash[i % hash.length] ^ privateBytes[i % privateBytes.length];
|
|
108
|
-
}
|
|
109
|
-
return publicBytes;
|
|
110
|
-
}
|
|
111
|
-
function derivePublicKey(privateKey) {
|
|
112
|
-
return {
|
|
113
|
-
type: "BLS12_381",
|
|
114
|
-
bytes: derivePublicKeyBytes(privateKey.bytes),
|
|
115
|
-
toHex: () => Buffer.from(derivePublicKeyBytes(privateKey.bytes)).toString("hex")
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
function generateMnemonic() {
|
|
119
|
-
const words = ["abandon", "ability", "able", "about", "above", "absent", "absorb", "abstract"];
|
|
120
|
-
const phrase = Array(12).fill(0).map(() => words[Math.floor(Math.random() * words.length)]);
|
|
121
|
-
return phrase.join(" ");
|
|
122
|
-
}
|
|
123
|
-
var crypto_default = {
|
|
124
|
-
generateKeypair,
|
|
125
|
-
deriveKeypair,
|
|
126
|
-
signAttestation,
|
|
127
|
-
verifyAttestation,
|
|
128
|
-
batchVerifyAttestations,
|
|
1
|
+
import {
|
|
129
2
|
aggregateSignatures,
|
|
130
|
-
|
|
3
|
+
batchVerifyAttestations,
|
|
4
|
+
crypto_default,
|
|
5
|
+
deriveKeypair,
|
|
6
|
+
generateKeypair,
|
|
131
7
|
hashPayload,
|
|
8
|
+
isStubMode,
|
|
132
9
|
setStubMode,
|
|
133
|
-
|
|
134
|
-
|
|
10
|
+
signAttestation,
|
|
11
|
+
verifyAggregate,
|
|
12
|
+
verifyAttestation
|
|
13
|
+
} from "./chunk-TIRAZTCQ.mjs";
|
|
135
14
|
export {
|
|
136
15
|
aggregateSignatures,
|
|
137
16
|
batchVerifyAttestations,
|