shogun-core 5.2.0 → 5.2.2
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/README.md +145 -1143
- package/dist/browser/defaultVendors-node_modules_hpke_chacha20poly1305_esm_mod_js.shogun-core.js +1220 -0
- package/dist/browser/defaultVendors-node_modules_hpke_chacha20poly1305_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_hpke_hybridkem-x-wing_esm_mod_js.shogun-core.js +844 -0
- package/dist/browser/defaultVendors-node_modules_hpke_hybridkem-x-wing_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_mlkem_esm_mod_js.shogun-core.js +2335 -0
- package/dist/browser/defaultVendors-node_modules_mlkem_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_ciphers_chacha_js.shogun-core.js +999 -0
- package/dist/browser/defaultVendors-node_modules_noble_ciphers_chacha_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js +1651 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_edwards_js-node_modules_noble_curves_es-a82056.shogun-core.js +825 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_edwards_js-node_modules_noble_curves_es-a82056.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed25519_js.shogun-core.js +508 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed25519_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js +747 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js +1608 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_post-quantum_ml-dsa_js.shogun-core.js +2117 -0
- package/dist/browser/defaultVendors-node_modules_noble_post-quantum_ml-dsa_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_openpgp_dist_openpgp_min_mjs.shogun-core.js +86 -0
- package/dist/browser/defaultVendors-node_modules_openpgp_dist_openpgp_min_mjs.shogun-core.js.map +1 -0
- package/dist/browser/node_modules_hpke_ml-kem_esm_mod_js.shogun-core.js +539 -0
- package/dist/browser/node_modules_hpke_ml-kem_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/shogun-core.js +160386 -0
- package/dist/browser/shogun-core.js.map +1 -0
- package/dist/config/simplified-config.js +236 -0
- package/dist/core.js +329 -0
- package/dist/crypto/asymmetric.js +99 -0
- package/dist/crypto/double-ratchet.js +370 -0
- package/dist/crypto/file-encryption.js +213 -0
- package/dist/crypto/hashing.js +87 -0
- package/dist/crypto/index.js +34 -0
- package/dist/crypto/mls-codec.js +202 -0
- package/dist/crypto/mls.js +550 -0
- package/dist/crypto/pgp.js +390 -0
- package/dist/crypto/random-generation.js +341 -0
- package/dist/crypto/sframe.js +350 -0
- package/dist/crypto/signal-protocol.js +376 -0
- package/dist/crypto/symmetric.js +91 -0
- package/dist/crypto/types.js +2 -0
- package/dist/crypto/utils.js +140 -0
- package/dist/examples/auth-test.js +253 -0
- package/dist/examples/crypto-identity-example.js +151 -0
- package/dist/examples/crypto-working-test.js +83 -0
- package/dist/examples/double-ratchet-test.js +155 -0
- package/dist/examples/mls-advanced-example.js +294 -0
- package/dist/examples/mls-sframe-test.js +304 -0
- package/dist/examples/pgp-example.js +200 -0
- package/dist/examples/quick-auth-test.js +61 -0
- package/dist/examples/random-generation-test.js +151 -0
- package/dist/examples/signal-protocol-test.js +38 -0
- package/dist/examples/simple-api-test.js +114 -0
- package/dist/examples/simple-crypto-identity-example.js +84 -0
- package/dist/examples/timeout-test.js +227 -0
- package/dist/examples/zkproof-credentials-example.js +212 -0
- package/dist/examples/zkproof-example.js +201 -0
- package/dist/gundb/api.js +435 -0
- package/dist/gundb/crypto.js +283 -0
- package/dist/gundb/db.js +1946 -0
- package/dist/gundb/derive.js +232 -0
- package/dist/gundb/errors.js +76 -0
- package/dist/gundb/index.js +22 -0
- package/dist/gundb/rxjs.js +447 -0
- package/dist/gundb/types.js +5 -0
- package/dist/index.js +58 -0
- package/dist/interfaces/common.js +2 -0
- package/dist/interfaces/events.js +40 -0
- package/dist/interfaces/plugin.js +2 -0
- package/dist/interfaces/shogun.js +37 -0
- package/dist/managers/AuthManager.js +226 -0
- package/dist/managers/CoreInitializer.js +228 -0
- package/dist/managers/CryptoIdentityManager.js +366 -0
- package/dist/managers/EventManager.js +70 -0
- package/dist/managers/PluginManager.js +299 -0
- package/dist/plugins/base.js +50 -0
- package/dist/plugins/index.js +32 -0
- package/dist/plugins/nostr/index.js +20 -0
- package/dist/plugins/nostr/nostrConnector.js +419 -0
- package/dist/plugins/nostr/nostrConnectorPlugin.js +453 -0
- package/dist/plugins/nostr/nostrSigner.js +319 -0
- package/dist/plugins/nostr/types.js +2 -0
- package/dist/plugins/smartwallet/index.js +18 -0
- package/dist/plugins/smartwallet/smartWalletPlugin.js +511 -0
- package/dist/plugins/smartwallet/types.js +2 -0
- package/dist/plugins/web3/index.js +20 -0
- package/dist/plugins/web3/types.js +2 -0
- package/dist/plugins/web3/web3Connector.js +533 -0
- package/dist/plugins/web3/web3ConnectorPlugin.js +455 -0
- package/dist/plugins/web3/web3Signer.js +314 -0
- package/dist/plugins/webauthn/index.js +19 -0
- package/dist/plugins/webauthn/types.js +14 -0
- package/dist/plugins/webauthn/webauthn.js +496 -0
- package/dist/plugins/webauthn/webauthnPlugin.js +489 -0
- package/dist/plugins/webauthn/webauthnSigner.js +310 -0
- package/dist/plugins/zkproof/index.js +53 -0
- package/dist/plugins/zkproof/types.js +2 -0
- package/dist/plugins/zkproof/zkCredentials.js +213 -0
- package/dist/plugins/zkproof/zkProofConnector.js +198 -0
- package/dist/plugins/zkproof/zkProofPlugin.js +272 -0
- package/dist/storage/storage.js +145 -0
- package/dist/types/config/simplified-config.d.ts +114 -0
- package/dist/types/core.d.ts +305 -0
- package/dist/types/crypto/asymmetric.d.ts +6 -0
- package/dist/types/crypto/double-ratchet.d.ts +22 -0
- package/dist/types/crypto/file-encryption.d.ts +19 -0
- package/dist/types/crypto/hashing.d.ts +9 -0
- package/dist/types/crypto/index.d.ts +13 -0
- package/dist/types/crypto/mls-codec.d.ts +39 -0
- package/dist/types/crypto/mls.d.ts +130 -0
- package/dist/types/crypto/pgp.d.ts +95 -0
- package/dist/types/crypto/random-generation.d.ts +35 -0
- package/dist/types/crypto/sframe.d.ts +102 -0
- package/dist/types/crypto/signal-protocol.d.ts +26 -0
- package/dist/types/crypto/symmetric.d.ts +9 -0
- package/dist/types/crypto/types.d.ts +144 -0
- package/dist/types/crypto/utils.d.ts +22 -0
- package/dist/types/examples/auth-test.d.ts +8 -0
- package/dist/types/examples/crypto-identity-example.d.ts +5 -0
- package/dist/types/examples/crypto-working-test.d.ts +1 -0
- package/dist/types/examples/double-ratchet-test.d.ts +1 -0
- package/dist/types/examples/mls-advanced-example.d.ts +53 -0
- package/dist/types/examples/mls-sframe-test.d.ts +1 -0
- package/dist/types/examples/pgp-example.d.ts +75 -0
- package/dist/types/examples/quick-auth-test.d.ts +8 -0
- package/dist/types/examples/random-generation-test.d.ts +1 -0
- package/dist/types/examples/signal-protocol-test.d.ts +1 -0
- package/dist/types/examples/simple-api-test.d.ts +10 -0
- package/dist/types/examples/simple-crypto-identity-example.d.ts +6 -0
- package/dist/types/examples/timeout-test.d.ts +8 -0
- package/dist/types/examples/zkproof-credentials-example.d.ts +12 -0
- package/dist/types/examples/zkproof-example.d.ts +11 -0
- package/dist/types/gundb/api.d.ts +185 -0
- package/dist/types/gundb/crypto.d.ts +95 -0
- package/dist/types/gundb/db.d.ts +397 -0
- package/dist/types/gundb/derive.d.ts +21 -0
- package/dist/types/gundb/errors.d.ts +42 -0
- package/dist/types/gundb/index.d.ts +3 -0
- package/dist/types/gundb/rxjs.d.ts +110 -0
- package/dist/types/gundb/types.d.ts +255 -0
- package/dist/types/index.d.ts +16 -0
- package/dist/types/interfaces/common.d.ts +85 -0
- package/dist/types/interfaces/events.d.ts +131 -0
- package/dist/types/interfaces/plugin.d.ts +162 -0
- package/dist/types/interfaces/shogun.d.ts +208 -0
- package/dist/types/managers/AuthManager.d.ts +72 -0
- package/dist/types/managers/CoreInitializer.d.ts +40 -0
- package/dist/types/managers/CryptoIdentityManager.d.ts +102 -0
- package/dist/types/managers/EventManager.d.ts +49 -0
- package/dist/types/managers/PluginManager.d.ts +145 -0
- package/dist/types/plugins/base.d.ts +35 -0
- package/dist/types/plugins/index.d.ts +18 -0
- package/dist/types/plugins/nostr/index.d.ts +4 -0
- package/dist/types/plugins/nostr/nostrConnector.d.ts +119 -0
- package/dist/types/plugins/nostr/nostrConnectorPlugin.d.ts +163 -0
- package/dist/types/plugins/nostr/nostrSigner.d.ts +105 -0
- package/dist/types/plugins/nostr/types.d.ts +122 -0
- package/dist/types/plugins/smartwallet/index.d.ts +2 -0
- package/dist/types/plugins/smartwallet/smartWalletPlugin.d.ts +67 -0
- package/dist/types/plugins/smartwallet/types.d.ts +80 -0
- package/dist/types/plugins/web3/index.d.ts +4 -0
- package/dist/types/plugins/web3/types.d.ts +107 -0
- package/dist/types/plugins/web3/web3Connector.d.ts +129 -0
- package/dist/types/plugins/web3/web3ConnectorPlugin.d.ts +160 -0
- package/dist/types/plugins/web3/web3Signer.d.ts +114 -0
- package/dist/types/plugins/webauthn/index.d.ts +3 -0
- package/dist/types/plugins/webauthn/types.d.ts +183 -0
- package/dist/types/plugins/webauthn/webauthn.d.ts +129 -0
- package/dist/types/plugins/webauthn/webauthnPlugin.d.ts +179 -0
- package/dist/types/plugins/webauthn/webauthnSigner.d.ts +91 -0
- package/dist/types/plugins/zkproof/index.d.ts +48 -0
- package/dist/types/plugins/zkproof/types.d.ts +123 -0
- package/dist/types/plugins/zkproof/zkCredentials.d.ts +112 -0
- package/dist/types/plugins/zkproof/zkProofConnector.d.ts +46 -0
- package/dist/types/plugins/zkproof/zkProofPlugin.d.ts +76 -0
- package/dist/types/storage/storage.d.ts +51 -0
- package/dist/types/utils/errorHandler.d.ts +119 -0
- package/dist/types/utils/eventEmitter.d.ts +39 -0
- package/dist/types/utils/seedPhrase.d.ts +50 -0
- package/dist/types/utils/validation.d.ts +27 -0
- package/dist/utils/errorHandler.js +246 -0
- package/dist/utils/eventEmitter.js +79 -0
- package/dist/utils/seedPhrase.js +97 -0
- package/dist/utils/validation.js +81 -0
- package/package.json +10 -57
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = default_1;
|
|
4
|
+
const p256_1 = require("@noble/curves/p256");
|
|
5
|
+
const secp256k1_1 = require("@noble/curves/secp256k1");
|
|
6
|
+
const sha256_1 = require("@noble/hashes/sha256");
|
|
7
|
+
const sha3_1 = require("@noble/hashes/sha3");
|
|
8
|
+
const ripemd160_1 = require("@noble/hashes/ripemd160");
|
|
9
|
+
async function default_1(pwd, extra, options = {}) {
|
|
10
|
+
const TEXT_ENCODER = new TextEncoder();
|
|
11
|
+
const pwdBytes = pwd
|
|
12
|
+
? typeof pwd === "string"
|
|
13
|
+
? TEXT_ENCODER.encode(normalizeString(pwd))
|
|
14
|
+
: pwd
|
|
15
|
+
: crypto.getRandomValues(new Uint8Array(32));
|
|
16
|
+
// Mix extra into password bytes to ensure different results for different inputs
|
|
17
|
+
const extras = extra
|
|
18
|
+
? (Array.isArray(extra) ? extra : [extra]).map((e) => normalizeString(e.toString()))
|
|
19
|
+
: [];
|
|
20
|
+
const extraBuf = TEXT_ENCODER.encode(extras.join("|"));
|
|
21
|
+
const combinedInput = new Uint8Array(pwdBytes.length + extraBuf.length);
|
|
22
|
+
combinedInput.set(pwdBytes);
|
|
23
|
+
combinedInput.set(extraBuf, pwdBytes.length);
|
|
24
|
+
if (combinedInput.length < 16) {
|
|
25
|
+
throw new Error(`Insufficient input entropy (${combinedInput.length})`);
|
|
26
|
+
}
|
|
27
|
+
const version = "v1";
|
|
28
|
+
const result = {};
|
|
29
|
+
// Mantieni comportamento esistente (P-256) come default
|
|
30
|
+
const { includeP256 = true, includeSecp256k1Bitcoin = true, includeSecp256k1Ethereum = true, } = options;
|
|
31
|
+
if (includeP256) {
|
|
32
|
+
const salts = [
|
|
33
|
+
{ label: "signing", type: "pub/priv" },
|
|
34
|
+
{ label: "encryption", type: "epub/epriv" },
|
|
35
|
+
];
|
|
36
|
+
const [signingKeys, encryptionKeys] = await Promise.all(salts.map(async ({ label }) => {
|
|
37
|
+
const salt = TEXT_ENCODER.encode(`${label}-${version}`);
|
|
38
|
+
const privateKey = await stretchKey(combinedInput, salt);
|
|
39
|
+
if (!p256_1.p256.utils.isValidPrivateKey(privateKey)) {
|
|
40
|
+
throw new Error(`Invalid private key for ${label}`);
|
|
41
|
+
}
|
|
42
|
+
const publicKey = p256_1.p256.getPublicKey(privateKey, false);
|
|
43
|
+
return {
|
|
44
|
+
pub: keyBufferToJwk(publicKey),
|
|
45
|
+
priv: arrayBufToBase64UrlEncode(privateKey),
|
|
46
|
+
};
|
|
47
|
+
}));
|
|
48
|
+
// Chiavi P-256 esistenti
|
|
49
|
+
result.pub = signingKeys.pub;
|
|
50
|
+
result.priv = signingKeys.priv;
|
|
51
|
+
result.epub = encryptionKeys.pub;
|
|
52
|
+
result.epriv = encryptionKeys.priv;
|
|
53
|
+
}
|
|
54
|
+
// Derivazione Bitcoin P2PKH (secp256k1 + SHA256 + RIPEMD160 + Base58)
|
|
55
|
+
if (includeSecp256k1Bitcoin) {
|
|
56
|
+
const bitcoinSalt = TEXT_ENCODER.encode(`secp256k1-bitcoin-${version}`);
|
|
57
|
+
const bitcoinPrivateKey = await stretchKey(combinedInput, bitcoinSalt);
|
|
58
|
+
if (!secp256k1_1.secp256k1.utils.isValidPrivateKey(bitcoinPrivateKey)) {
|
|
59
|
+
throw new Error("Invalid secp256k1 private key for Bitcoin");
|
|
60
|
+
}
|
|
61
|
+
const bitcoinPublicKey = secp256k1_1.secp256k1.getPublicKey(bitcoinPrivateKey, true); // Compressed
|
|
62
|
+
result.secp256k1Bitcoin = {
|
|
63
|
+
privateKey: bytesToHex(bitcoinPrivateKey),
|
|
64
|
+
publicKey: bytesToHex(bitcoinPublicKey),
|
|
65
|
+
address: deriveP2PKHAddress(bitcoinPublicKey),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
// Derivazione Ethereum (secp256k1 + Keccak256)
|
|
69
|
+
if (includeSecp256k1Ethereum) {
|
|
70
|
+
const ethereumSalt = TEXT_ENCODER.encode(`secp256k1-ethereum-${version}`);
|
|
71
|
+
const ethereumPrivateKey = await stretchKey(combinedInput, ethereumSalt);
|
|
72
|
+
if (!secp256k1_1.secp256k1.utils.isValidPrivateKey(ethereumPrivateKey)) {
|
|
73
|
+
throw new Error("Invalid secp256k1 private key for Ethereum");
|
|
74
|
+
}
|
|
75
|
+
const ethereumPublicKey = secp256k1_1.secp256k1.getPublicKey(ethereumPrivateKey, false); // Uncompressed
|
|
76
|
+
result.secp256k1Ethereum = {
|
|
77
|
+
privateKey: "0x" + bytesToHex(ethereumPrivateKey),
|
|
78
|
+
publicKey: "0x" + bytesToHex(ethereumPublicKey),
|
|
79
|
+
address: deriveKeccak256Address(ethereumPublicKey),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
function arrayBufToBase64UrlEncode(buf) {
|
|
85
|
+
return btoa(String.fromCharCode(...buf))
|
|
86
|
+
.replace(/\//g, "_")
|
|
87
|
+
.replace(/=/g, "")
|
|
88
|
+
.replace(/\+/g, "-");
|
|
89
|
+
}
|
|
90
|
+
function keyBufferToJwk(publicKeyBuffer) {
|
|
91
|
+
if (publicKeyBuffer[0] !== 4)
|
|
92
|
+
throw new Error("Invalid uncompressed public key format");
|
|
93
|
+
return [
|
|
94
|
+
arrayBufToBase64UrlEncode(publicKeyBuffer.slice(1, 33)), // x
|
|
95
|
+
arrayBufToBase64UrlEncode(publicKeyBuffer.slice(33, 65)), // y
|
|
96
|
+
].join(".");
|
|
97
|
+
}
|
|
98
|
+
function normalizeString(str) {
|
|
99
|
+
return str.normalize("NFC").trim();
|
|
100
|
+
}
|
|
101
|
+
async function stretchKey(input, salt, iterations = 300000) {
|
|
102
|
+
try {
|
|
103
|
+
const baseKey = await crypto.subtle.importKey("raw", input, { name: "PBKDF2" }, false, ["deriveBits"]);
|
|
104
|
+
const keyBits = await crypto.subtle.deriveBits({
|
|
105
|
+
name: "PBKDF2",
|
|
106
|
+
salt: salt,
|
|
107
|
+
iterations,
|
|
108
|
+
hash: "SHA-256",
|
|
109
|
+
}, baseKey, 256);
|
|
110
|
+
const keyBytes = new Uint8Array(keyBits);
|
|
111
|
+
// Ensure the key is valid for secp256k1
|
|
112
|
+
return ensureValidSecp256k1Key(keyBytes);
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
// Fallback: generate a deterministic key from input and salt
|
|
116
|
+
const fallbackKey = generateFallbackKey(input, salt);
|
|
117
|
+
return ensureValidSecp256k1Key(fallbackKey);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function generateFallbackKey(input, salt) {
|
|
121
|
+
// Simple deterministic key generation as fallback
|
|
122
|
+
const key = new Uint8Array(32);
|
|
123
|
+
for (let i = 0; i < 32; i++) {
|
|
124
|
+
key[i] = (i * 7 + salt[i % salt.length]) % 256;
|
|
125
|
+
}
|
|
126
|
+
return key;
|
|
127
|
+
}
|
|
128
|
+
function ensureValidSecp256k1Key(keyBytes) {
|
|
129
|
+
// Ensure the key is not all zeros
|
|
130
|
+
if (keyBytes.every((byte) => byte === 0)) {
|
|
131
|
+
keyBytes[0] = 1;
|
|
132
|
+
}
|
|
133
|
+
// secp256k1 curve order is approximately 2^256 - 2^32 - 2^9 - 2^8 - 2^7 - 2^6 - 2^4 - 1
|
|
134
|
+
const maxValidKey = new Uint8Array([
|
|
135
|
+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
136
|
+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
137
|
+
0xff, 0xff, 0xff, 0xfe, 0xba, 0xae, 0xdc, 0xe6,
|
|
138
|
+
]);
|
|
139
|
+
// If the key is greater than or equal to the curve order, reduce it
|
|
140
|
+
let isGreaterOrEqual = true;
|
|
141
|
+
for (let i = 0; i < 32; i++) {
|
|
142
|
+
if (keyBytes[i] < maxValidKey[i]) {
|
|
143
|
+
isGreaterOrEqual = false;
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
else if (keyBytes[i] > maxValidKey[i]) {
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (isGreaterOrEqual) {
|
|
151
|
+
// Reduce the key by setting it to a safe value
|
|
152
|
+
keyBytes[31] = 0xe5; // Set to a value less than the curve order
|
|
153
|
+
}
|
|
154
|
+
// Additional validation: ensure the key is not too small
|
|
155
|
+
if (keyBytes.every((byte) => byte === 0) ||
|
|
156
|
+
keyBytes.every((byte) => byte === 1)) {
|
|
157
|
+
// Set to a safe default value
|
|
158
|
+
keyBytes.fill(0);
|
|
159
|
+
keyBytes[0] = 0x01;
|
|
160
|
+
keyBytes[31] = 0xff;
|
|
161
|
+
}
|
|
162
|
+
return keyBytes;
|
|
163
|
+
}
|
|
164
|
+
function bytesToHex(bytes) {
|
|
165
|
+
return Array.from(bytes)
|
|
166
|
+
.map((b) => b.toString(16).padStart(2, "0"))
|
|
167
|
+
.join("");
|
|
168
|
+
}
|
|
169
|
+
// Base58 encoding per Bitcoin
|
|
170
|
+
const BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
171
|
+
function base58Encode(bytes) {
|
|
172
|
+
if (bytes.length === 0)
|
|
173
|
+
return "";
|
|
174
|
+
// Count leading zeros
|
|
175
|
+
let zeros = 0;
|
|
176
|
+
for (let i = 0; i < bytes.length && bytes[i] === 0; i++) {
|
|
177
|
+
zeros++;
|
|
178
|
+
}
|
|
179
|
+
// Convert to base58
|
|
180
|
+
const digits = [0];
|
|
181
|
+
for (let i = zeros; i < bytes.length; i++) {
|
|
182
|
+
let carry = bytes[i];
|
|
183
|
+
for (let j = 0; j < digits.length; j++) {
|
|
184
|
+
carry += digits[j] << 8;
|
|
185
|
+
digits[j] = carry % 58;
|
|
186
|
+
carry = (carry / 58) | 0;
|
|
187
|
+
}
|
|
188
|
+
while (carry > 0) {
|
|
189
|
+
digits.push(carry % 58);
|
|
190
|
+
carry = (carry / 58) | 0;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// Convert to string
|
|
194
|
+
let result = "";
|
|
195
|
+
for (let i = 0; i < zeros; i++) {
|
|
196
|
+
result += BASE58_ALPHABET[0];
|
|
197
|
+
}
|
|
198
|
+
for (let i = digits.length - 1; i >= 0; i--) {
|
|
199
|
+
result += BASE58_ALPHABET[digits[i]];
|
|
200
|
+
}
|
|
201
|
+
return result;
|
|
202
|
+
}
|
|
203
|
+
function deriveP2PKHAddress(publicKey) {
|
|
204
|
+
// Bitcoin P2PKH address derivation
|
|
205
|
+
// 1. SHA256 hash del public key
|
|
206
|
+
const sha256Hash = (0, sha256_1.sha256)(publicKey);
|
|
207
|
+
// 2. RIPEMD160 hash del risultato
|
|
208
|
+
const ripemd160Hash = (0, ripemd160_1.ripemd160)(sha256Hash);
|
|
209
|
+
// 3. Aggiungi version byte (0x00 per mainnet P2PKH)
|
|
210
|
+
const versionedHash = new Uint8Array(21);
|
|
211
|
+
versionedHash[0] = 0x00; // Mainnet P2PKH version
|
|
212
|
+
versionedHash.set(ripemd160Hash, 1);
|
|
213
|
+
// 4. Double SHA256 per checksum
|
|
214
|
+
const checksum = (0, sha256_1.sha256)((0, sha256_1.sha256)(versionedHash));
|
|
215
|
+
// 5. Aggiungi i primi 4 byte del checksum
|
|
216
|
+
const addressBytes = new Uint8Array(25);
|
|
217
|
+
addressBytes.set(versionedHash);
|
|
218
|
+
addressBytes.set(checksum.slice(0, 4), 21);
|
|
219
|
+
// 6. Base58 encode
|
|
220
|
+
return base58Encode(addressBytes);
|
|
221
|
+
}
|
|
222
|
+
function deriveKeccak256Address(publicKey) {
|
|
223
|
+
// Ethereum address derivation usando Keccak256
|
|
224
|
+
// 1. Rimuovi il prefix byte (0x04) dalla chiave pubblica non compressa
|
|
225
|
+
const publicKeyWithoutPrefix = publicKey.slice(1);
|
|
226
|
+
// 2. Calcola Keccak256 hash
|
|
227
|
+
const hash = (0, sha3_1.keccak_256)(publicKeyWithoutPrefix);
|
|
228
|
+
// 3. Prendi gli ultimi 20 byte
|
|
229
|
+
const address = hash.slice(-20);
|
|
230
|
+
// 4. Aggiungi '0x' prefix e converti in hex
|
|
231
|
+
return "0x" + bytesToHex(address);
|
|
232
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Error classes for Gun and Auth
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.NetworkError = exports.MultipleAuthError = exports.TimeoutError = exports.UserExists = exports.InvalidCredentials = exports.AuthError = exports.GunError = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* Base error for Gun
|
|
9
|
+
*/
|
|
10
|
+
class GunError extends Error {
|
|
11
|
+
constructor(message) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = "GunError";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.GunError = GunError;
|
|
17
|
+
/**
|
|
18
|
+
* Generic authentication error
|
|
19
|
+
*/
|
|
20
|
+
class AuthError extends GunError {
|
|
21
|
+
constructor(message) {
|
|
22
|
+
super(message);
|
|
23
|
+
this.name = "AuthError";
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.AuthError = AuthError;
|
|
27
|
+
/**
|
|
28
|
+
* Invalid credentials error
|
|
29
|
+
*/
|
|
30
|
+
class InvalidCredentials extends AuthError {
|
|
31
|
+
constructor(message = "Credenziali non valide") {
|
|
32
|
+
super(message);
|
|
33
|
+
this.name = "InvalidCredentials";
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.InvalidCredentials = InvalidCredentials;
|
|
37
|
+
/**
|
|
38
|
+
* User already exists error
|
|
39
|
+
*/
|
|
40
|
+
class UserExists extends AuthError {
|
|
41
|
+
constructor(message = "Utente già esistente") {
|
|
42
|
+
super(message);
|
|
43
|
+
this.name = "UserExists";
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.UserExists = UserExists;
|
|
47
|
+
/**
|
|
48
|
+
* Timeout error
|
|
49
|
+
*/
|
|
50
|
+
class TimeoutError extends GunError {
|
|
51
|
+
constructor(message = "Timeout durante l'operazione") {
|
|
52
|
+
super(message);
|
|
53
|
+
this.name = "TimeoutError";
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.TimeoutError = TimeoutError;
|
|
57
|
+
/**
|
|
58
|
+
* Multiple authentication error
|
|
59
|
+
*/
|
|
60
|
+
class MultipleAuthError extends AuthError {
|
|
61
|
+
constructor(message = "Autenticazione multipla in corso") {
|
|
62
|
+
super(message);
|
|
63
|
+
this.name = "MultipleAuthError";
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.MultipleAuthError = MultipleAuthError;
|
|
67
|
+
/** Base error related to the network. */
|
|
68
|
+
class NetworkError extends GunError {
|
|
69
|
+
}
|
|
70
|
+
exports.NetworkError = NetworkError;
|
|
71
|
+
const withDefaultMessage = (args, defaultMessage) => {
|
|
72
|
+
if (args.length === 0 || (args.length === 1 && !args[0])) {
|
|
73
|
+
args = [defaultMessage];
|
|
74
|
+
}
|
|
75
|
+
return args;
|
|
76
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
// Export the main class
|
|
18
|
+
__exportStar(require("./db"), exports);
|
|
19
|
+
// Export improved types
|
|
20
|
+
__exportStar(require("./types"), exports);
|
|
21
|
+
// Export simplified API
|
|
22
|
+
__exportStar(require("./api"), exports);
|