@unicitylabs/sphere-sdk 0.1.4 → 0.1.5

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.
@@ -1,3 +1,126 @@
1
+ // constants.ts
2
+ var STORAGE_KEYS_GLOBAL = {
3
+ /** Encrypted BIP39 mnemonic */
4
+ MNEMONIC: "mnemonic",
5
+ /** Encrypted master private key */
6
+ MASTER_KEY: "master_key",
7
+ /** BIP32 chain code */
8
+ CHAIN_CODE: "chain_code",
9
+ /** HD derivation path (full path like m/44'/0'/0'/0/0) */
10
+ DERIVATION_PATH: "derivation_path",
11
+ /** Base derivation path (like m/44'/0'/0' without chain/index) */
12
+ BASE_PATH: "base_path",
13
+ /** Derivation mode: bip32, wif_hmac, legacy_hmac */
14
+ DERIVATION_MODE: "derivation_mode",
15
+ /** Wallet source: mnemonic, file, unknown */
16
+ WALLET_SOURCE: "wallet_source",
17
+ /** Wallet existence flag */
18
+ WALLET_EXISTS: "wallet_exists",
19
+ /** Current active address index */
20
+ CURRENT_ADDRESS_INDEX: "current_address_index",
21
+ /** Index of address nametags (JSON: { "0": "alice", "1": "bob" }) - for discovery */
22
+ ADDRESS_NAMETAGS: "address_nametags"
23
+ };
24
+ var STORAGE_KEYS_ADDRESS = {
25
+ /** Pending transfers for this address */
26
+ PENDING_TRANSFERS: "pending_transfers",
27
+ /** Transfer outbox for this address */
28
+ OUTBOX: "outbox",
29
+ /** Conversations for this address */
30
+ CONVERSATIONS: "conversations",
31
+ /** Messages for this address */
32
+ MESSAGES: "messages",
33
+ /** Transaction history for this address */
34
+ TRANSACTION_HISTORY: "transaction_history"
35
+ };
36
+ var STORAGE_KEYS = {
37
+ ...STORAGE_KEYS_GLOBAL,
38
+ ...STORAGE_KEYS_ADDRESS
39
+ };
40
+ function getAddressId(directAddress) {
41
+ let hash = directAddress;
42
+ if (hash.startsWith("DIRECT://")) {
43
+ hash = hash.slice(9);
44
+ } else if (hash.startsWith("DIRECT:")) {
45
+ hash = hash.slice(7);
46
+ }
47
+ const first = hash.slice(0, 6).toLowerCase();
48
+ const last = hash.slice(-6).toLowerCase();
49
+ return `DIRECT_${first}_${last}`;
50
+ }
51
+ var DEFAULT_NOSTR_RELAYS = [
52
+ "wss://relay.unicity.network",
53
+ "wss://relay.damus.io",
54
+ "wss://nos.lol",
55
+ "wss://relay.nostr.band"
56
+ ];
57
+ var NOSTR_EVENT_KINDS = {
58
+ /** NIP-04 encrypted direct message */
59
+ DIRECT_MESSAGE: 4,
60
+ /** Token transfer (Unicity custom - 31113) */
61
+ TOKEN_TRANSFER: 31113,
62
+ /** Payment request (Unicity custom - 31115) */
63
+ PAYMENT_REQUEST: 31115,
64
+ /** Payment request response (Unicity custom - 31116) */
65
+ PAYMENT_REQUEST_RESPONSE: 31116,
66
+ /** Nametag binding (NIP-78 app-specific data) */
67
+ NAMETAG_BINDING: 30078,
68
+ /** Public broadcast */
69
+ BROADCAST: 1
70
+ };
71
+ var DEFAULT_AGGREGATOR_URL = "https://aggregator.unicity.network/rpc";
72
+ var DEV_AGGREGATOR_URL = "https://dev-aggregator.dyndns.org/rpc";
73
+ var TEST_AGGREGATOR_URL = "https://goggregator-test.unicity.network";
74
+ var DEFAULT_AGGREGATOR_TIMEOUT = 3e4;
75
+ var DEFAULT_AGGREGATOR_API_KEY = "sk_06365a9c44654841a366068bcfc68986";
76
+ var DEFAULT_IPFS_GATEWAYS = [
77
+ "https://ipfs.unicity.network",
78
+ "https://dweb.link",
79
+ "https://ipfs.io"
80
+ ];
81
+ var DEFAULT_BASE_PATH = "m/44'/0'/0'";
82
+ var DEFAULT_DERIVATION_PATH = `${DEFAULT_BASE_PATH}/0/0`;
83
+ var DEFAULT_ELECTRUM_URL = "wss://fulcrum.alpha.unicity.network:50004";
84
+ var TEST_ELECTRUM_URL = "wss://fulcrum.alpha.testnet.unicity.network:50004";
85
+ var TEST_NOSTR_RELAYS = [
86
+ "wss://nostr-relay.testnet.unicity.network"
87
+ ];
88
+ var NETWORKS = {
89
+ mainnet: {
90
+ name: "Mainnet",
91
+ aggregatorUrl: DEFAULT_AGGREGATOR_URL,
92
+ nostrRelays: DEFAULT_NOSTR_RELAYS,
93
+ ipfsGateways: DEFAULT_IPFS_GATEWAYS,
94
+ electrumUrl: DEFAULT_ELECTRUM_URL
95
+ },
96
+ testnet: {
97
+ name: "Testnet",
98
+ aggregatorUrl: TEST_AGGREGATOR_URL,
99
+ nostrRelays: TEST_NOSTR_RELAYS,
100
+ ipfsGateways: DEFAULT_IPFS_GATEWAYS,
101
+ electrumUrl: TEST_ELECTRUM_URL
102
+ },
103
+ dev: {
104
+ name: "Development",
105
+ aggregatorUrl: DEV_AGGREGATOR_URL,
106
+ nostrRelays: TEST_NOSTR_RELAYS,
107
+ ipfsGateways: DEFAULT_IPFS_GATEWAYS,
108
+ electrumUrl: TEST_ELECTRUM_URL
109
+ }
110
+ };
111
+ var TIMEOUTS = {
112
+ /** WebSocket connection timeout */
113
+ WEBSOCKET_CONNECT: 1e4,
114
+ /** Nostr relay reconnect delay */
115
+ NOSTR_RECONNECT_DELAY: 3e3,
116
+ /** Max reconnect attempts */
117
+ MAX_RECONNECT_ATTEMPTS: 5,
118
+ /** Proof polling interval */
119
+ PROOF_POLL_INTERVAL: 1e3,
120
+ /** Sync interval */
121
+ SYNC_INTERVAL: 6e4
122
+ };
123
+
1
124
  // impl/browser/storage/LocalStorageProvider.ts
2
125
  var LocalStorageProvider = class {
3
126
  id = "localStorage";
@@ -114,8 +237,12 @@ var LocalStorageProvider = class {
114
237
  // Private Methods
115
238
  // ===========================================================================
116
239
  getFullKey(key) {
117
- const addressPart = this.identity?.l1Address ?? "default";
118
- return `${this.config.prefix}${addressPart}_${key}`;
240
+ const isPerAddressKey = Object.values(STORAGE_KEYS_ADDRESS).includes(key);
241
+ if (isPerAddressKey && this.identity?.directAddress) {
242
+ const addressId = getAddressId(this.identity.directAddress);
243
+ return `${this.config.prefix}${addressId}_${key}`;
244
+ }
245
+ return `${this.config.prefix}${key}`;
119
246
  }
120
247
  ensureConnected() {
121
248
  if (this.status !== "connected") {
@@ -170,17 +297,21 @@ var IndexedDBTokenStorageProvider = class {
170
297
  id = "indexeddb-token-storage";
171
298
  name = "IndexedDB Token Storage";
172
299
  type = "local";
300
+ dbNamePrefix;
173
301
  dbName;
174
302
  db = null;
175
303
  status = "disconnected";
176
304
  identity = null;
177
305
  constructor(config) {
178
- const prefix = config?.dbNamePrefix ?? DB_NAME;
179
- this.dbName = prefix;
306
+ this.dbNamePrefix = config?.dbNamePrefix ?? DB_NAME;
307
+ this.dbName = this.dbNamePrefix;
180
308
  }
181
309
  setIdentity(identity) {
182
310
  this.identity = identity;
183
- this.dbName = `${DB_NAME}-${identity.l1Address.slice(0, 20)}`;
311
+ if (identity.directAddress) {
312
+ const addressId = getAddressId(identity.directAddress);
313
+ this.dbName = `${this.dbNamePrefix}-${addressId}`;
314
+ }
184
315
  }
185
316
  async initialize() {
186
317
  try {
@@ -1035,122 +1166,6 @@ function defaultUUIDGenerator() {
1035
1166
  });
1036
1167
  }
1037
1168
 
1038
- // constants.ts
1039
- var STORAGE_PREFIX = "sphere_";
1040
- var STORAGE_KEYS = {
1041
- /** Encrypted BIP39 mnemonic */
1042
- MNEMONIC: `${STORAGE_PREFIX}mnemonic`,
1043
- /** Encrypted master private key */
1044
- MASTER_KEY: `${STORAGE_PREFIX}master_key`,
1045
- /** BIP32 chain code */
1046
- CHAIN_CODE: `${STORAGE_PREFIX}chain_code`,
1047
- /** HD derivation path (full path like m/44'/0'/0'/0/0) */
1048
- DERIVATION_PATH: `${STORAGE_PREFIX}derivation_path`,
1049
- /** Base derivation path (like m/44'/0'/0' without chain/index) */
1050
- BASE_PATH: `${STORAGE_PREFIX}base_path`,
1051
- /** Derivation mode: bip32, wif_hmac, legacy_hmac */
1052
- DERIVATION_MODE: `${STORAGE_PREFIX}derivation_mode`,
1053
- /** Wallet source: mnemonic, file, unknown */
1054
- WALLET_SOURCE: `${STORAGE_PREFIX}wallet_source`,
1055
- /** Wallet existence flag */
1056
- WALLET_EXISTS: `${STORAGE_PREFIX}wallet_exists`,
1057
- /** Registered nametag (legacy - single address) */
1058
- NAMETAG: `${STORAGE_PREFIX}nametag`,
1059
- /** Current active address index */
1060
- CURRENT_ADDRESS_INDEX: `${STORAGE_PREFIX}current_address_index`,
1061
- /** Address nametags map (JSON: { "0": "alice", "1": "bob" }) */
1062
- ADDRESS_NAMETAGS: `${STORAGE_PREFIX}address_nametags`,
1063
- /** Token data */
1064
- TOKENS: `${STORAGE_PREFIX}tokens`,
1065
- /** Pending transfers */
1066
- PENDING_TRANSFERS: `${STORAGE_PREFIX}pending_transfers`,
1067
- /** Transfer outbox */
1068
- OUTBOX: `${STORAGE_PREFIX}outbox`,
1069
- /** Conversations */
1070
- CONVERSATIONS: `${STORAGE_PREFIX}conversations`,
1071
- /** Messages */
1072
- MESSAGES: `${STORAGE_PREFIX}messages`,
1073
- /** Transaction history */
1074
- TRANSACTION_HISTORY: `${STORAGE_PREFIX}transaction_history`,
1075
- /** Archived tokens (spent token history) */
1076
- ARCHIVED_TOKENS: `${STORAGE_PREFIX}archived_tokens`,
1077
- /** Tombstones (records of deleted/spent tokens) */
1078
- TOMBSTONES: `${STORAGE_PREFIX}tombstones`,
1079
- /** Forked tokens (alternative histories) */
1080
- FORKED_TOKENS: `${STORAGE_PREFIX}forked_tokens`
1081
- };
1082
- var DEFAULT_NOSTR_RELAYS = [
1083
- "wss://relay.unicity.network",
1084
- "wss://relay.damus.io",
1085
- "wss://nos.lol",
1086
- "wss://relay.nostr.band"
1087
- ];
1088
- var NOSTR_EVENT_KINDS = {
1089
- /** NIP-04 encrypted direct message */
1090
- DIRECT_MESSAGE: 4,
1091
- /** Token transfer (Unicity custom - 31113) */
1092
- TOKEN_TRANSFER: 31113,
1093
- /** Payment request (Unicity custom - 31115) */
1094
- PAYMENT_REQUEST: 31115,
1095
- /** Payment request response (Unicity custom - 31116) */
1096
- PAYMENT_REQUEST_RESPONSE: 31116,
1097
- /** Nametag binding (NIP-78 app-specific data) */
1098
- NAMETAG_BINDING: 30078,
1099
- /** Public broadcast */
1100
- BROADCAST: 1
1101
- };
1102
- var DEFAULT_AGGREGATOR_URL = "https://aggregator.unicity.network/rpc";
1103
- var DEV_AGGREGATOR_URL = "https://dev-aggregator.dyndns.org/rpc";
1104
- var TEST_AGGREGATOR_URL = "https://goggregator-test.unicity.network";
1105
- var DEFAULT_AGGREGATOR_TIMEOUT = 3e4;
1106
- var DEFAULT_IPFS_GATEWAYS = [
1107
- "https://ipfs.unicity.network",
1108
- "https://dweb.link",
1109
- "https://ipfs.io"
1110
- ];
1111
- var DEFAULT_BASE_PATH = "m/44'/0'/0'";
1112
- var DEFAULT_DERIVATION_PATH = `${DEFAULT_BASE_PATH}/0/0`;
1113
- var DEFAULT_ELECTRUM_URL = "wss://fulcrum.alpha.unicity.network:50004";
1114
- var TEST_ELECTRUM_URL = "wss://fulcrum.alpha.testnet.unicity.network:50004";
1115
- var TEST_NOSTR_RELAYS = [
1116
- "wss://nostr-relay.testnet.unicity.network"
1117
- ];
1118
- var NETWORKS = {
1119
- mainnet: {
1120
- name: "Mainnet",
1121
- aggregatorUrl: DEFAULT_AGGREGATOR_URL,
1122
- nostrRelays: DEFAULT_NOSTR_RELAYS,
1123
- ipfsGateways: DEFAULT_IPFS_GATEWAYS,
1124
- electrumUrl: DEFAULT_ELECTRUM_URL
1125
- },
1126
- testnet: {
1127
- name: "Testnet",
1128
- aggregatorUrl: TEST_AGGREGATOR_URL,
1129
- nostrRelays: TEST_NOSTR_RELAYS,
1130
- ipfsGateways: DEFAULT_IPFS_GATEWAYS,
1131
- electrumUrl: TEST_ELECTRUM_URL
1132
- },
1133
- dev: {
1134
- name: "Development",
1135
- aggregatorUrl: DEV_AGGREGATOR_URL,
1136
- nostrRelays: TEST_NOSTR_RELAYS,
1137
- ipfsGateways: DEFAULT_IPFS_GATEWAYS,
1138
- electrumUrl: TEST_ELECTRUM_URL
1139
- }
1140
- };
1141
- var TIMEOUTS = {
1142
- /** WebSocket connection timeout */
1143
- WEBSOCKET_CONNECT: 1e4,
1144
- /** Nostr relay reconnect delay */
1145
- NOSTR_RECONNECT_DELAY: 3e3,
1146
- /** Max reconnect attempts */
1147
- MAX_RECONNECT_ATTEMPTS: 5,
1148
- /** Proof polling interval */
1149
- PROOF_POLL_INTERVAL: 1e3,
1150
- /** Sync interval */
1151
- SYNC_INTERVAL: 6e4
1152
- };
1153
-
1154
1169
  // transport/NostrTransportProvider.ts
1155
1170
  var EVENT_KINDS = NOSTR_EVENT_KINDS;
1156
1171
  function deriveNametagEncryptionKey(privateKeyHex) {
@@ -2776,7 +2791,7 @@ function resolveOracleConfig(network, config) {
2776
2791
  const networkConfig = getNetworkConfig(network);
2777
2792
  return {
2778
2793
  url: config?.url ?? networkConfig.aggregatorUrl,
2779
- apiKey: config?.apiKey,
2794
+ apiKey: config?.apiKey ?? DEFAULT_AGGREGATOR_API_KEY,
2780
2795
  timeout: config?.timeout,
2781
2796
  skipVerification: config?.skipVerification,
2782
2797
  debug: config?.debug,