shogun-core 1.0.2 → 1.0.4

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.
Files changed (44) hide show
  1. package/README.md +246 -61
  2. package/dist/browser/shogun-core.js +1 -1
  3. package/dist/browser/shogun-core.light.js +1 -1
  4. package/dist/browser/shogun-core.vendors.light.js +1 -1
  5. package/dist/browser.js +5 -25
  6. package/dist/gun/gun.js +16 -17
  7. package/dist/index.js +16 -52
  8. package/dist/plugins/did/DID.js +4 -36
  9. package/dist/plugins/did/didPlugin.js +6 -0
  10. package/dist/plugins/metamask/metamask.js +12 -0
  11. package/dist/plugins/metamask/metamaskPlugin.js +6 -0
  12. package/dist/plugins/stealth/stealth.js +63 -545
  13. package/dist/plugins/stealth/stealthPlugin.js +37 -7
  14. package/dist/plugins/wallet/walletManager.js +34 -2
  15. package/dist/plugins/wallet/walletPlugin.js +18 -0
  16. package/dist/relay/didVerifier.js +230 -0
  17. package/dist/relay/index.js +9 -0
  18. package/dist/relay/oracleBridge.js +232 -0
  19. package/dist/relay/relayMembershipVerifier.js +247 -0
  20. package/dist/types/browser.d.ts +0 -3
  21. package/dist/types/gun/gun.d.ts +1 -0
  22. package/dist/types/index.d.ts +6 -11
  23. package/dist/types/plugins/did/DID.d.ts +1 -2
  24. package/dist/types/plugins/did/didPlugin.d.ts +8 -0
  25. package/dist/types/plugins/did/types.d.ts +3 -0
  26. package/dist/types/plugins/metamask/metamask.d.ts +4 -0
  27. package/dist/types/plugins/metamask/metamaskPlugin.d.ts +4 -0
  28. package/dist/types/plugins/stealth/stealth.d.ts +25 -68
  29. package/dist/types/plugins/stealth/stealthPlugin.d.ts +9 -2
  30. package/dist/types/plugins/stealth/types.d.ts +7 -4
  31. package/dist/types/plugins/wallet/walletManager.d.ts +5 -1
  32. package/dist/types/plugins/wallet/walletPlugin.d.ts +12 -0
  33. package/dist/types/relay/didVerifier.d.ts +78 -0
  34. package/dist/types/relay/index.d.ts +6 -0
  35. package/dist/types/relay/oracleBridge.d.ts +81 -0
  36. package/dist/types/relay/relayMembershipVerifier.d.ts +84 -0
  37. package/dist/types/types/shogun.d.ts +1 -15
  38. package/dist/types/utils/errorHandler.d.ts +2 -1
  39. package/dist/utils/errorHandler.js +1 -0
  40. package/package.json +1 -1
  41. package/dist/gun/example-model-repository.js +0 -106
  42. package/dist/types/gun/example-model-repository.d.ts +0 -81
  43. /package/dist/plugins/{activitypub → _activitypub}/rsaSigner.js +0 -0
  44. /package/dist/types/plugins/{activitypub → _activitypub}/rsaSigner.d.ts +0 -0
package/README.md CHANGED
@@ -35,6 +35,11 @@
35
35
  - [Registering a Plugin](#registering-a-plugin)
36
36
  - [Using the Plugin](#using-the-plugin)
37
37
  - [Stealth Transactions](#stealth-transactions)
38
+ - [Relay Verification](#relay-verification)
39
+ - [RelayMembershipVerifier](#1-relaymembershipverifier)
40
+ - [DIDVerifier](#2-didverifier)
41
+ - [OracleBridge](#3-oraclebridge)
42
+ - [Casi d'Uso per la Relay Verification](#casi-duso-per-la-relay-verification)
38
43
  - [Browser Integration](#browser-integration)
39
44
  - [Setup](#setup)
40
45
  - [Examples](#examples)
@@ -892,79 +897,87 @@ const shogun = new ShogunCore({
892
897
  // Accesso al plugin Stealth
893
898
  const stealthPlugin = shogun.getPlugin(CorePlugins.Stealth);
894
899
 
895
- // Esempio di funzionalità Stealth
900
+ // 1. Generazione di chiavi effimere
901
+ async function generateEphemeralKeys() {
902
+ const ephemeralKeys = await stealthPlugin.generateEphemeralKeyPair();
903
+ console.log("Chiave privata effimera:", ephemeralKeys.privateKey);
904
+ console.log("Chiave pubblica effimera:", ephemeralKeys.publicKey);
905
+ return ephemeralKeys;
906
+ }
896
907
 
897
- // 1. Generazione di un nuovo indirizzo stealth
898
- async function generateStealthAddress() {
899
- const stealthResult = await stealthPlugin.generateStealthAddress();
900
- console.log("Nuovo indirizzo stealth:", stealthResult.stealthAddress);
901
- console.log("Chiave di scansione:", stealthResult.scanKey);
902
- return stealthResult;
908
+ // 2. Recupero delle chiavi stealth dell'utente
909
+ async function getMyStealthKeys() {
910
+ const stealthKeys = await stealthPlugin.getStealthKeys();
911
+ console.log("Chiave di visualizzazione:", stealthKeys.viewingKey);
912
+ console.log("Chiave di spesa:", stealthKeys.spendingKey);
913
+ return stealthKeys;
903
914
  }
904
915
 
905
- // 2. Invio di una transazione stealth
906
- async function sendStealthPayment(recipientStealthAddress, amountInEth) {
916
+ // 3. Generazione di un indirizzo stealth per il destinatario
917
+ async function createStealthAddressForRecipient(recipientViewingPublicKey, recipientSpendingPublicKey) {
907
918
  try {
908
- // Ottieni il wallet principale
909
- const walletPlugin = shogun.getPlugin(CorePlugins.Wallet);
910
- const senderWallet = walletPlugin.getMainWallet();
911
-
912
- // Crea e invia la transazione stealth
913
- const txResult = await stealthPlugin.createStealthTransaction(
914
- recipientStealthAddress,
915
- amountInEth,
916
- {
917
- gasLimit: 150000,
918
- maxFeePerGas: "50", // in gwei
919
- }
919
+ // Genera un indirizzo stealth per il destinatario
920
+ const stealthAddressResult = await stealthPlugin.generateStealthAddress(
921
+ recipientViewingPublicKey,
922
+ recipientSpendingPublicKey
920
923
  );
921
-
922
- console.log("Transazione stealth inviata:", txResult);
923
- console.log("Hash della transazione:", txResult.hash);
924
-
925
- return txResult;
924
+
925
+ console.log("Indirizzo stealth generato:", stealthAddressResult.stealthAddress);
926
+ console.log("Chiave pubblica effimera:", stealthAddressResult.ephemeralPublicKey);
927
+ console.log("Numero casuale crittografato:", stealthAddressResult.encryptedRandomNumber);
928
+
929
+ return stealthAddressResult;
926
930
  } catch (error) {
927
- console.error("Errore nell'invio della transazione stealth:", error);
931
+ console.error("Errore nella generazione dell'indirizzo stealth:", error);
928
932
  throw error;
929
933
  }
930
934
  }
931
935
 
932
- // 3. Scansione per transazioni stealth in entrata
933
- async function scanForPayments() {
936
+ // 4. Apertura di un indirizzo stealth ricevuto
937
+ async function openReceivedStealthAddress(stealthAddress, encryptedRandomNumber, ephemeralPublicKey) {
934
938
  try {
935
- // Scansiona la blockchain per pagamenti stealth ricevuti
936
- const payments = await stealthPlugin.scanStealthPayments();
937
-
938
- console.log(`Trovati ${payments.length} pagamenti stealth`);
939
- payments.forEach((payment, index) => {
940
- console.log(`Pagamento ${index + 1}:`);
941
- console.log(`- Indirizzo: ${payment.address}`);
942
- console.log(`- Valore: ${payment.amount} ETH`);
943
- console.log(`- Blocco: ${payment.blockNumber}`);
944
- });
945
-
946
- return payments;
939
+ // Apre un indirizzo stealth usando le chiavi dell'utente
940
+ const wallet = await stealthPlugin.openStealthAddress(
941
+ stealthAddress,
942
+ encryptedRandomNumber,
943
+ ephemeralPublicKey
944
+ );
945
+
946
+ console.log("Indirizzo wallet derivato:", wallet.address);
947
+ console.log("Chiave privata wallet:", wallet.privateKey);
948
+
949
+ // Questo wallet può essere usato per accedere ai fondi inviati all'indirizzo stealth
950
+ return wallet;
947
951
  } catch (error) {
948
- console.error("Errore durante la scansione dei pagamenti stealth:", error);
952
+ console.error("Errore nell'apertura dell'indirizzo stealth:", error);
949
953
  throw error;
950
954
  }
951
955
  }
952
956
 
953
- // 4. Reclama una transazione stealth ricevuta
954
- async function claimStealthPayment(paymentInfo) {
957
+ // 5. Invio di fondi a un indirizzo stealth (usando ethers.js separatamente)
958
+ async function sendFundsToStealthAddress(stealthAddress, amountInEth) {
955
959
  try {
956
- // Reclama il pagamento stealth spostando i fondi al wallet principale
957
- const claimResult = await stealthPlugin.claimStealthPayment(
958
- paymentInfo.address,
959
- paymentInfo.privateKey
960
- );
961
-
962
- console.log("Pagamento stealth reclamato:", claimResult);
963
- console.log("Hash della transazione di claim:", claimResult.hash);
964
-
965
- return claimResult;
960
+ // Ottieni un wallet per inviare la transazione
961
+ const walletPlugin = shogun.getPlugin(CorePlugins.Wallet);
962
+ const senderWallet = walletPlugin.getMainWallet();
963
+
964
+ // Prepara la transazione usando ethers.js
965
+ const tx = {
966
+ to: stealthAddress,
967
+ value: ethers.parseEther(amountInEth)
968
+ };
969
+
970
+ // Invia la transazione
971
+ const txResponse = await senderWallet.sendTransaction(tx);
972
+ console.log("Transazione inviata:", txResponse.hash);
973
+
974
+ // Attendi la conferma
975
+ const receipt = await txResponse.wait();
976
+ console.log("Transazione confermata nel blocco:", receipt.blockNumber);
977
+
978
+ return receipt;
966
979
  } catch (error) {
967
- console.error("Errore nel reclamo del pagamento stealth:", error);
980
+ console.error("Errore nell'invio di fondi all'indirizzo stealth:", error);
968
981
  throw error;
969
982
  }
970
983
  }
@@ -975,16 +988,188 @@ async function claimStealthPayment(paymentInfo) {
975
988
  - **Maggiore Privacy**: Gli indirizzi stealth nascondono la vera identità del destinatario
976
989
  - **Transazioni Non Collegabili**: Ogni transazione utilizza un indirizzo diverso
977
990
  - **Compatibilità**: Funziona con qualsiasi portafoglio Ethereum
978
- - **Sicurezza**: Solo il destinatario può rilevare e riscattare i pagamenti stealth
991
+ - **Sicurezza**: Solo il destinatario può derivare la chiave privata necessaria per accedere ai fondi
979
992
 
980
993
  ### Come Funziona
981
994
 
982
- 1. **Generazione**: Il destinatario genera un indirizzo stealth utilizzando una chiave di scansione
983
- 2. **Pagamento**: Il mittente utilizza l'indirizzo stealth per inviare fondi
984
- 3. **Scansione**: Il destinatario scansiona la blockchain utilizzando la sua chiave di scansione
985
- 4. **Reclamo**: Il destinatario reclama i fondi trasferendoli a un indirizzo di sua scelta
995
+ 1. **Chiavi Stealth**: Ogni utente possiede una coppia di chiavi di visualizzazione e spesa
996
+ 2. **Generazione Indirizzo**: Il mittente genera un indirizzo stealth unico per il destinatario usando le sue chiavi pubbliche
997
+ 3. **Invio Fondi**: Il mittente trasferisce i fondi all'indirizzo stealth generato
998
+ 4. **Recupero Fondi**: Il destinatario può derivare la chiave privata dell'indirizzo stealth usando l'encryptedRandomNumber e la ephemeralPublicKey forniti dal mittente
999
+ 5. **Accesso Sicuro**: Solo il legittimo destinatario può calcolare la chiave privata necessaria per spostare i fondi
1000
+
1001
+ ### Note Implementative
1002
+
1003
+ - Il protocollo stealth di Shogun utilizza la crittografia asimmetrica per garantire che solo il destinatario previsto possa derivare la chiave privata dell'indirizzo stealth
1004
+ - L'implementazione consente la creazione di portafogli effimeri per ogni transazione
1005
+ - La comunicazione delle informazioni necessarie (ephemeralPublicKey, encryptedRandomNumber) avviene off-chain tra mittente e destinatario
1006
+
1007
+ ## Relay Verification
1008
+
1009
+ Shogun SDK include moduli per la verifica e l'interazione con sistemi di relay blockchain, fornendo funzionalità per verificare l'appartenenza, l'identità decentralizzata e la sincronizzazione dei dati.
1010
+
1011
+ ### 1. RelayMembershipVerifier
1012
+
1013
+ Questo componente consente di verificare se un indirizzo o una chiave pubblica è autorizzato all'interno del protocollo Shogun.
1014
+
1015
+ ```typescript
1016
+ import { ShogunCore, RelayMembershipVerifier } from "shogun-core";
1017
+
1018
+ // Inizializza Shogun
1019
+ const shogun = new ShogunCore({
1020
+ // Configurazione...
1021
+ providerUrl: "https://ethereum-rpc-url.com",
1022
+ });
1023
+
1024
+ // Crea un'istanza RelayMembershipVerifier
1025
+ const verifier = new RelayMembershipVerifier({
1026
+ contractAddress: "0xContractAddress",
1027
+ providerUrl: "https://ethereum-rpc-url.com", // Opzionale se riutilizzi il provider di ShogunCore
1028
+ }, shogun); // Passa l'istanza ShogunCore per riutilizzare il provider
1029
+
1030
+ // Verifica se un indirizzo è autorizzato
1031
+ async function checkAddressAuthorization(address) {
1032
+ const isAuthorized = await verifier.isAddressAuthorized(address);
1033
+ console.log(`L'indirizzo ${address} è ${isAuthorized ? 'autorizzato' : 'non autorizzato'}`);
1034
+ return isAuthorized;
1035
+ }
1036
+
1037
+ // Verifica se una chiave pubblica è autorizzata
1038
+ async function checkPublicKeyAuthorization(publicKey) {
1039
+ const isAuthorized = await verifier.isPublicKeyAuthorized(publicKey);
1040
+ console.log(`La chiave pubblica è ${isAuthorized ? 'autorizzata' : 'non autorizzata'}`);
1041
+ return isAuthorized;
1042
+ }
1043
+
1044
+ // Recupera informazioni utente
1045
+ async function getUserInformation(address) {
1046
+ const userInfo = await verifier.getUserInfo(address);
1047
+ if (userInfo) {
1048
+ console.log(`Informazioni utente per ${address}:`);
1049
+ console.log(`- Scadenza: ${userInfo.expires}`);
1050
+ console.log(`- Chiave pubblica: ${userInfo.pubKey}`);
1051
+ } else {
1052
+ console.log(`Nessuna informazione trovata per ${address}`);
1053
+ }
1054
+ return userInfo;
1055
+ }
1056
+ ```
1057
+
1058
+ ### 2. DIDVerifier
1059
+
1060
+ Questo componente consente di verificare e interagire con Identificatori Decentralizzati (DID) registrati nel contratto DIDRegistry.
1061
+
1062
+ ```typescript
1063
+ import { ShogunCore, DIDVerifier } from "shogun-core";
1064
+
1065
+ // Inizializza Shogun
1066
+ const shogun = new ShogunCore({
1067
+ // Configurazione...
1068
+ providerUrl: "https://ethereum-rpc-url.com",
1069
+ });
1070
+
1071
+ // Crea un'istanza DIDVerifier
1072
+ const didVerifier = new DIDVerifier({
1073
+ contractAddress: "0xDIDRegistryAddress",
1074
+ providerUrl: "https://ethereum-rpc-url.com", // Opzionale se riutilizzi il provider di ShogunCore
1075
+ }, shogun);
1076
+
1077
+ // Verifica un DID e ottieni il suo controller
1078
+ async function verifyDecentralizedIdentifier(did) {
1079
+ const controller = await didVerifier.verifyDID(did);
1080
+ if (controller) {
1081
+ console.log(`DID ${did} verificato con successo`);
1082
+ console.log(`Controller: ${controller}`);
1083
+ } else {
1084
+ console.log(`DID ${did} non trovato o non valido`);
1085
+ }
1086
+ return controller;
1087
+ }
1088
+
1089
+ // Autentica un utente usando il suo DID e una firma
1090
+ async function authenticateUser(did, message, signature) {
1091
+ const isAuthenticated = await didVerifier.authenticateWithDID(did, message, signature);
1092
+ console.log(`Autenticazione ${isAuthenticated ? 'riuscita' : 'fallita'} per DID: ${did}`);
1093
+ return isAuthenticated;
1094
+ }
1095
+
1096
+ // Registra un nuovo DID (richiede un signer con diritti di amministrazione)
1097
+ async function registerNewDID(did, controller) {
1098
+ const success = await didVerifier.registerDID(did, controller);
1099
+ console.log(`Registrazione DID ${success ? 'completata' : 'fallita'} per ${did}`);
1100
+ return success;
1101
+ }
1102
+ ```
1103
+
1104
+ ### 3. OracleBridge
1105
+
1106
+ Questo componente consente di interagire con il contratto OracleBridge che pubblica e verifica le radici Merkle per varie epoche, facilitando la sincronizzazione sicura tra on-chain e off-chain.
1107
+
1108
+ ```typescript
1109
+ import { ShogunCore, OracleBridge } from "shogun-core";
1110
+ import { ethers } from "ethers";
1111
+
1112
+ // Inizializza Shogun
1113
+ const shogun = new ShogunCore({
1114
+ // Configurazione...
1115
+ providerUrl: "https://ethereum-rpc-url.com",
1116
+ });
1117
+
1118
+ // Crea un'istanza OracleBridge
1119
+ const oracleBridge = new OracleBridge({
1120
+ contractAddress: "0xOracleBridgeAddress",
1121
+ providerUrl: "https://ethereum-rpc-url.com", // Opzionale se riutilizzi il provider di ShogunCore
1122
+ }, shogun);
1123
+
1124
+ // Ottieni l'ID dell'epoca corrente
1125
+ async function getCurrentEpoch() {
1126
+ const epochId = await oracleBridge.getEpochId();
1127
+ console.log(`Epoca corrente: ${epochId}`);
1128
+ return epochId;
1129
+ }
1130
+
1131
+ // Ottieni la radice Merkle per un'epoca specifica
1132
+ async function getMerkleRoot(epochId) {
1133
+ const root = await oracleBridge.getRootForEpoch(epochId);
1134
+ console.log(`Radice Merkle per epoca ${epochId}: ${root}`);
1135
+ return root;
1136
+ }
1137
+
1138
+ // Verifica se la radice Merkle corrisponde al valore atteso
1139
+ async function verifyMerkleRoot(epochId, expectedRoot) {
1140
+ const isValid = await oracleBridge.verifyRoot(epochId, expectedRoot);
1141
+ console.log(`Verifica radice per epoca ${epochId}: ${isValid ? 'valida' : 'non valida'}`);
1142
+ return isValid;
1143
+ }
1144
+
1145
+ // Pubblica una nuova radice Merkle (solo admin)
1146
+ async function publishNewRoot(epochId, root, wallet) {
1147
+ // Imposta un signer con wallet.connect(provider)
1148
+ const signer = wallet.connect(new ethers.JsonRpcProvider("https://ethereum-rpc-url.com"));
1149
+ oracleBridge.setSigner(signer);
1150
+
1151
+ const receipt = await oracleBridge.publishRoot(epochId, root);
1152
+ if (receipt) {
1153
+ console.log(`Radice pubblicata con successo per epoca ${epochId}`);
1154
+ console.log(`Transaction hash: ${receipt.hash}`);
1155
+ } else {
1156
+ console.log(`Pubblicazione fallita per epoca ${epochId}`);
1157
+ }
1158
+ return receipt;
1159
+ }
1160
+ ```
1161
+
1162
+ ### Casi d'Uso per la Relay Verification
1163
+
1164
+ I componenti di verifica relay sono particolarmente utili per:
1165
+
1166
+ - **Autenticazione Decentralizzata**: Verifica l'identità degli utenti attraverso DID e firme crittografiche
1167
+ - **Controllo Accessi**: Limita l'accesso ai relay solo a membri autorizzati
1168
+ - **Sincronizzazione Sicura**: Garantisce l'integrità dei dati tra blockchain e sistemi off-chain
1169
+ - **Gestione del Consenso**: Implementa meccanismi di consenso per dati critici
1170
+ - **Audit e Tracciamento**: Traccia le operazioni di sincronizzazione e verifica attraverso l'Oracle Bridge
986
1171
 
987
- Questa implementazione utilizza il protocollo Stealth Address per migliorare la privacy delle transazioni sulla blockchain Ethereum.
1172
+ Per ulteriori dettagli sulle API di verifica relay, consulta la documentazione tecnica.
988
1173
 
989
1174
  ## Browser Integration
990
1175