@morseai/sdk 0.1.0-beta.7 → 0.1.0-beta.9

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/index.mjs CHANGED
@@ -475,50 +475,6 @@ function mapErrorResponse(error) {
475
475
  return new MorseSDKError(error.message || "Unknown error");
476
476
  }
477
477
 
478
- // src/logger.ts
479
- var Logger = class {
480
- constructor() {
481
- this.level = "warn";
482
- this.levels = {
483
- none: 0,
484
- error: 1,
485
- warn: 2,
486
- info: 3,
487
- debug: 4
488
- };
489
- }
490
- setLevel(level) {
491
- this.level = level;
492
- }
493
- getLevel() {
494
- return this.level;
495
- }
496
- shouldLog(level) {
497
- return this.levels[level] <= this.levels[this.level];
498
- }
499
- error(message, ...args) {
500
- if (this.shouldLog("error")) {
501
- console.error(`[MorseSDK] ${message}`, ...args);
502
- }
503
- }
504
- warn(message, ...args) {
505
- if (this.shouldLog("warn")) {
506
- console.warn(`[MorseSDK] ${message}`, ...args);
507
- }
508
- }
509
- info(message, ...args) {
510
- if (this.shouldLog("info")) {
511
- console.info(`[MorseSDK] ${message}`, ...args);
512
- }
513
- }
514
- debug(message, ...args) {
515
- if (this.shouldLog("debug")) {
516
- console.debug(`[MorseSDK] ${message}`, ...args);
517
- }
518
- }
519
- };
520
- var logger = new Logger();
521
-
522
478
  // src/crypto.ts
523
479
  var CIPHER_VERSION = "aes-gcm-256-v1";
524
480
  function getCrypto() {
@@ -828,13 +784,6 @@ var MorseSDKV1 = class {
828
784
  const windowMs = config.rateLimit?.windowMs || 6e4;
829
785
  this.rateLimiter = new RateLimiter({ maxRequests, windowMs });
830
786
  }
831
- logger.debug("MorseSDKV1 initialized", {
832
- apiVersion: config.apiVersion || "v1",
833
- timeout: this.timeout,
834
- retries: this.retries,
835
- rateLimitEnabled: this.rateLimiter !== null,
836
- hasApiKey: !!config.apiKey
837
- });
838
787
  }
839
788
  base64ToUint8Array(base64) {
840
789
  if (typeof Buffer !== "undefined") {
@@ -914,10 +863,6 @@ var MorseSDKV1 = class {
914
863
  await this.rateLimiter.checkLimit();
915
864
  } catch (error) {
916
865
  if (error instanceof RateLimitError) {
917
- logger.warn("Rate limit exceeded", {
918
- url,
919
- retryAfterMs: error.retryAfterMs
920
- });
921
866
  if (this.config.onError) {
922
867
  this.config.onError(error);
923
868
  }
@@ -926,11 +871,6 @@ var MorseSDKV1 = class {
926
871
  throw error;
927
872
  }
928
873
  }
929
- logger.debug("Making request", {
930
- url,
931
- method: options.method || "GET",
932
- hasBody: !!options.body
933
- });
934
874
  const requestOptions = {
935
875
  ...options,
936
876
  headers: {
@@ -962,12 +902,6 @@ var MorseSDKV1 = class {
962
902
  code: "VALIDATION_ERROR",
963
903
  message: `HTTP ${response.status}: ${response.statusText}`
964
904
  }));
965
- logger.error("Request failed", {
966
- status: response.status,
967
- code: error.code,
968
- message: error.message,
969
- attempt: attempt + 1
970
- });
971
905
  const mappedError = mapErrorResponse(error);
972
906
  if (this.config.onError) {
973
907
  this.config.onError(mappedError);
@@ -975,7 +909,6 @@ var MorseSDKV1 = class {
975
909
  throw mappedError;
976
910
  }
977
911
  const data = await response.json();
978
- logger.debug("Request successful", { url, attempt: attempt + 1 });
979
912
  return data;
980
913
  } catch (error) {
981
914
  lastError = error;
@@ -988,7 +921,6 @@ var MorseSDKV1 = class {
988
921
  }
989
922
  if (error instanceof NetworkError || error.name?.includes("Error")) {
990
923
  if (attempt < this.retries) {
991
- logger.warn(`Request failed, retrying... (${attempt + 1}/${this.retries})`, { url });
992
924
  await new Promise((resolve) => setTimeout(resolve, this.retryDelay * (attempt + 1)));
993
925
  continue;
994
926
  }
@@ -999,7 +931,6 @@ var MorseSDKV1 = class {
999
931
  }
1000
932
  if (error.message?.includes("fetch") || error.message?.includes("network")) {
1001
933
  if (attempt < this.retries) {
1002
- logger.warn(`Network error, retrying... (${attempt + 1}/${this.retries})`, { url });
1003
934
  await new Promise((resolve) => setTimeout(resolve, this.retryDelay * (attempt + 1)));
1004
935
  continue;
1005
936
  }
@@ -1034,11 +965,6 @@ var MorseSDKV1 = class {
1034
965
  if (!options.hasFile && !options.hasMessage) {
1035
966
  throw new Error("Either hasFile or hasMessage must be true");
1036
967
  }
1037
- logger.info("Creating signal", {
1038
- hasFile: options.hasFile,
1039
- hasMessage: options.hasMessage,
1040
- mode: options.mode
1041
- });
1042
968
  const tempSignalId = "temp-" + Date.now();
1043
969
  const authMessage = await this.createAuthMessage("create", `signal ${tempSignalId}`);
1044
970
  const signature = await this.signMessage(wallet, authMessage);
@@ -1057,13 +983,11 @@ var MorseSDKV1 = class {
1057
983
  body: JSON.stringify(requestBody)
1058
984
  }
1059
985
  );
1060
- logger.info("Signal created", { signalId: result.signalId });
1061
986
  return result;
1062
987
  }
1063
988
  async openSignal(wallet, signalId) {
1064
989
  validateSignalId(signalId);
1065
990
  validateWalletAddress(wallet.address);
1066
- logger.info("Opening signal", { signalId });
1067
991
  const authMessage = await this.createAuthMessage("open", `signal ${signalId}`);
1068
992
  const signature = await this.signMessage(wallet, authMessage);
1069
993
  const requestBody = {
@@ -1081,7 +1005,6 @@ var MorseSDKV1 = class {
1081
1005
  body: JSON.stringify(requestBody)
1082
1006
  }
1083
1007
  );
1084
- logger.info("Signal opened", { signalId, hasFile: !!result.file });
1085
1008
  return result;
1086
1009
  }
1087
1010
  async openSignalDecrypted(wallet, signalId, keyBase64) {
@@ -1309,19 +1232,59 @@ var MorseSDKV1 = class {
1309
1232
  }
1310
1233
  const walletTarget = wallet.address;
1311
1234
  const walletCreator = wallet.address;
1312
- const domain = "morse.app";
1313
- const chainId = 8453;
1235
+ const keyService = new WalletKeyService(API_BASE_URL, this.config.apiKey, this.config.apiVersion || "v1");
1236
+ const creatorKeyResponse = await keyService.getPublicKey(wallet.address);
1237
+ let domain;
1238
+ let chainId;
1239
+ if (creatorKeyResponse.exists && creatorKeyResponse.certificate) {
1240
+ domain = creatorKeyResponse.certificate.domain || options.domain || "morseai.tech";
1241
+ chainId = creatorKeyResponse.certificate.chainId ?? (options.chainId ?? 8453);
1242
+ } else {
1243
+ domain = options.domain || "morseai.tech";
1244
+ chainId = options.chainId ?? 8453;
1245
+ const { deriveKeyPairFromWalletSignature: deriveKeyPairFromWalletSignature2, createKeyCertificate: createKeyCertificate2 } = await Promise.resolve().then(() => (init_crypto_x25519(), crypto_x25519_exports));
1246
+ const creatorKeypair = await deriveKeyPairFromWalletSignature2(
1247
+ wallet.address,
1248
+ domain,
1249
+ chainId,
1250
+ wallet.signMessage
1251
+ );
1252
+ const publicKeyBase64 = Buffer.from(creatorKeypair.publicKey).toString("base64");
1253
+ const expiresAtMsCert = Date.now() + 30 * 24 * 60 * 60 * 1e3;
1254
+ let signTypedDataFn;
1255
+ if (wallet.signTypedData) {
1256
+ signTypedDataFn = wallet.signTypedData;
1257
+ } else {
1258
+ throw new Error(
1259
+ "signTypedData is required for publishing public key. Please ensure your WalletAuth includes signTypedData. If using createWalletFromPrivateKey, update to the latest version of the SDK."
1260
+ );
1261
+ }
1262
+ const certificate = await createKeyCertificate2(
1263
+ wallet.address,
1264
+ publicKeyBase64,
1265
+ domain,
1266
+ chainId,
1267
+ expiresAtMsCert,
1268
+ signTypedDataFn
1269
+ );
1270
+ await keyService.publishPublicKey(certificate);
1271
+ }
1314
1272
  const keyBytes = this.base64ToUint8Array(keyBase64);
1315
- const sealedBox = await sealDataKey(
1316
- keyBytes,
1317
- walletTarget.toLowerCase(),
1318
- walletCreator.toLowerCase(),
1319
- expiresAtMs,
1320
- signalId,
1321
- domain,
1322
- chainId,
1323
- wallet.signMessage
1324
- );
1273
+ let sealedBox;
1274
+ try {
1275
+ sealedBox = await sealDataKey(
1276
+ keyBytes,
1277
+ walletTarget.toLowerCase(),
1278
+ walletCreator.toLowerCase(),
1279
+ expiresAtMs,
1280
+ signalId,
1281
+ domain,
1282
+ chainId,
1283
+ wallet.signMessage
1284
+ );
1285
+ } catch (sealError) {
1286
+ throw new Error(`Failed to seal encryption key: ${sealError.message || String(sealError)}`);
1287
+ }
1325
1288
  let encryptedText;
1326
1289
  let payloadNonce;
1327
1290
  let fileOptions;
@@ -1481,7 +1444,6 @@ var MorseSDKV1 = class {
1481
1444
  async burnSignal(wallet, signalId) {
1482
1445
  validateSignalId(signalId);
1483
1446
  validateWalletAddress(wallet.address);
1484
- logger.info("Burning signal", { signalId });
1485
1447
  const authMessage = await this.createAuthMessage("burn", `signal ${signalId}`);
1486
1448
  const signature = await this.signMessage(wallet, authMessage);
1487
1449
  const requestBody = {
@@ -1499,7 +1461,6 @@ var MorseSDKV1 = class {
1499
1461
  body: JSON.stringify(requestBody)
1500
1462
  }
1501
1463
  );
1502
- logger.info("Signal burned successfully", { signalId });
1503
1464
  return result;
1504
1465
  }
1505
1466
  };
@@ -1517,14 +1478,9 @@ var MorseSDK = class {
1517
1478
  this.contract = new MorseSDKV1(config);
1518
1479
  break;
1519
1480
  default:
1520
- logger.warn(`Unknown API version: ${this.apiVersion}, defaulting to v1`);
1521
1481
  this.contract = new MorseSDKV1(config);
1522
1482
  this.apiVersion = "v1";
1523
1483
  }
1524
- logger.debug("MorseSDK initialized", {
1525
- apiVersion: this.apiVersion,
1526
- contractVersion: this.contract.version
1527
- });
1528
1484
  }
1529
1485
  getConfig() {
1530
1486
  return { ...this.config };
@@ -1607,6 +1563,11 @@ function createWalletFromPrivateKey(config) {
1607
1563
  const wallet = new Wallet(privateKey);
1608
1564
  const signature = await wallet.signMessage(message);
1609
1565
  return signature;
1566
+ },
1567
+ signTypedData: async (domain, types, value) => {
1568
+ const wallet = new Wallet(privateKey);
1569
+ const signature = await wallet.signTypedData(domain, types, value);
1570
+ return signature;
1610
1571
  }
1611
1572
  };
1612
1573
  }
@@ -1651,6 +1612,50 @@ async function createBrowserWallet(ethereum, address) {
1651
1612
  };
1652
1613
  }
1653
1614
 
1615
+ // src/logger.ts
1616
+ var Logger = class {
1617
+ constructor() {
1618
+ this.level = "warn";
1619
+ this.levels = {
1620
+ none: 0,
1621
+ error: 1,
1622
+ warn: 2,
1623
+ info: 3,
1624
+ debug: 4
1625
+ };
1626
+ }
1627
+ setLevel(level) {
1628
+ this.level = level;
1629
+ }
1630
+ getLevel() {
1631
+ return this.level;
1632
+ }
1633
+ shouldLog(level) {
1634
+ return this.levels[level] <= this.levels[this.level];
1635
+ }
1636
+ error(message, ...args) {
1637
+ if (this.shouldLog("error")) {
1638
+ console.error(`[MorseSDK] ${message}`, ...args);
1639
+ }
1640
+ }
1641
+ warn(message, ...args) {
1642
+ if (this.shouldLog("warn")) {
1643
+ console.warn(`[MorseSDK] ${message}`, ...args);
1644
+ }
1645
+ }
1646
+ info(message, ...args) {
1647
+ if (this.shouldLog("info")) {
1648
+ console.info(`[MorseSDK] ${message}`, ...args);
1649
+ }
1650
+ }
1651
+ debug(message, ...args) {
1652
+ if (this.shouldLog("debug")) {
1653
+ console.debug(`[MorseSDK] ${message}`, ...args);
1654
+ }
1655
+ }
1656
+ };
1657
+ var logger = new Logger();
1658
+
1654
1659
  // src/index.ts
1655
1660
  init_crypto_x25519();
1656
1661