@unicitylabs/sphere-sdk 0.5.7 → 0.6.0-dev.1

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.cjs CHANGED
@@ -845,7 +845,10 @@ __export(index_exports, {
845
845
  NETWORKS: () => NETWORKS,
846
846
  NIP29_KINDS: () => NIP29_KINDS,
847
847
  NOSTR_EVENT_KINDS: () => NOSTR_EVENT_KINDS,
848
+ NostrClient: () => import_nostr_js_sdk5.NostrClient,
849
+ NostrKeyManager: () => import_nostr_js_sdk5.NostrKeyManager,
848
850
  PaymentsModule: () => PaymentsModule,
851
+ SIGN_MESSAGE_PREFIX: () => SIGN_MESSAGE_PREFIX,
849
852
  STORAGE_KEYS: () => STORAGE_KEYS,
850
853
  STORAGE_KEYS_ADDRESS: () => STORAGE_KEYS_ADDRESS,
851
854
  STORAGE_KEYS_GLOBAL: () => STORAGE_KEYS_GLOBAL,
@@ -881,6 +884,7 @@ __export(index_exports, {
881
884
  createTokenValidator: () => createTokenValidator,
882
885
  decodeBech32: () => decodeBech32,
883
886
  decryptCMasterKey: () => decryptCMasterKey,
887
+ decryptNametag: () => import_nostr_js_sdk4.decryptNametag,
884
888
  decryptPrivateKey: () => decryptPrivateKey,
885
889
  decryptTextFormatKey: () => decryptTextFormatKey,
886
890
  deriveAddressInfo: () => deriveAddressInfo,
@@ -888,6 +892,7 @@ __export(index_exports, {
888
892
  deriveKeyAtPath: () => deriveKeyAtPath,
889
893
  doubleSha256: () => doubleSha256,
890
894
  encodeBech32: () => encodeBech32,
895
+ encryptNametag: () => import_nostr_js_sdk4.encryptNametag,
891
896
  extractFromText: () => extractFromText,
892
897
  findPattern: () => findPattern,
893
898
  forkedKeyFromTokenIdAndState: () => forkedKeyFromTokenIdAndState,
@@ -912,7 +917,9 @@ __export(index_exports, {
912
917
  hasUncommittedTransactions: () => hasUncommittedTransactions,
913
918
  hasValidTxfData: () => hasValidTxfData,
914
919
  hash160: () => hash160,
920
+ hashAddressForTag: () => import_nostr_js_sdk4.hashAddressForTag,
915
921
  hashNametag: () => import_nostr_js_sdk4.hashNametag,
922
+ hashSignMessage: () => hashSignMessage,
916
923
  hexToBytes: () => hexToBytes,
917
924
  identityFromMnemonicSync: () => identityFromMnemonicSync,
918
925
  initSphere: () => initSphere,
@@ -954,6 +961,7 @@ __export(index_exports, {
954
961
  randomUUID: () => randomUUID,
955
962
  ripemd160: () => ripemd160,
956
963
  sha256: () => sha256,
964
+ signMessage: () => signMessage,
957
965
  sleep: () => sleep,
958
966
  sphereExists: () => sphereExists,
959
967
  toHumanReadable: () => toHumanReadable,
@@ -962,7 +970,8 @@ __export(index_exports, {
962
970
  tokenIdFromKey: () => tokenIdFromKey,
963
971
  tokenToTxf: () => tokenToTxf,
964
972
  txfToToken: () => txfToToken,
965
- validateMnemonic: () => validateMnemonic2
973
+ validateMnemonic: () => validateMnemonic2,
974
+ verifySignedMessage: () => verifySignedMessage
966
975
  });
967
976
  module.exports = __toCommonJS(index_exports);
968
977
 
@@ -1238,6 +1247,73 @@ function generateAddressInfo(privateKey, index, path, prefix = "alpha") {
1238
1247
  index
1239
1248
  };
1240
1249
  }
1250
+ var SIGN_MESSAGE_PREFIX = "Sphere Signed Message:\n";
1251
+ function varint(n) {
1252
+ if (n < 253) return new Uint8Array([n]);
1253
+ const buf = new Uint8Array(3);
1254
+ buf[0] = 253;
1255
+ buf[1] = n & 255;
1256
+ buf[2] = n >> 8 & 255;
1257
+ return buf;
1258
+ }
1259
+ function hashSignMessage(message) {
1260
+ const prefix = new TextEncoder().encode(SIGN_MESSAGE_PREFIX);
1261
+ const msg = new TextEncoder().encode(message);
1262
+ const prefixLen = varint(prefix.length);
1263
+ const msgLen = varint(msg.length);
1264
+ const full = new Uint8Array(prefixLen.length + prefix.length + msgLen.length + msg.length);
1265
+ let off = 0;
1266
+ full.set(prefixLen, off);
1267
+ off += prefixLen.length;
1268
+ full.set(prefix, off);
1269
+ off += prefix.length;
1270
+ full.set(msgLen, off);
1271
+ off += msgLen.length;
1272
+ full.set(msg, off);
1273
+ const hex = Array.from(full).map((b) => b.toString(16).padStart(2, "0")).join("");
1274
+ const h1 = import_crypto_js2.default.SHA256(import_crypto_js2.default.enc.Hex.parse(hex)).toString();
1275
+ return import_crypto_js2.default.SHA256(import_crypto_js2.default.enc.Hex.parse(h1)).toString();
1276
+ }
1277
+ function signMessage(privateKeyHex, message) {
1278
+ const keyPair = ec.keyFromPrivate(privateKeyHex, "hex");
1279
+ const hashHex = hashSignMessage(message);
1280
+ const hashBytes = Buffer.from(hashHex, "hex");
1281
+ const sig = keyPair.sign(hashBytes, { canonical: true });
1282
+ const pub = keyPair.getPublic();
1283
+ let recoveryParam = -1;
1284
+ for (let i = 0; i < 4; i++) {
1285
+ try {
1286
+ if (ec.recoverPubKey(hashBytes, sig, i).eq(pub)) {
1287
+ recoveryParam = i;
1288
+ break;
1289
+ }
1290
+ } catch {
1291
+ }
1292
+ }
1293
+ if (recoveryParam === -1) {
1294
+ throw new SphereError("Could not find recovery parameter", "SIGNING_ERROR");
1295
+ }
1296
+ const v = (31 + recoveryParam).toString(16).padStart(2, "0");
1297
+ const r = sig.r.toString("hex").padStart(64, "0");
1298
+ const s = sig.s.toString("hex").padStart(64, "0");
1299
+ return v + r + s;
1300
+ }
1301
+ function verifySignedMessage(message, signature, expectedPubkey) {
1302
+ if (signature.length !== 130) return false;
1303
+ const v = parseInt(signature.slice(0, 2), 16) - 31;
1304
+ const r = signature.slice(2, 66);
1305
+ const s = signature.slice(66, 130);
1306
+ if (v < 0 || v > 3) return false;
1307
+ const hashHex = hashSignMessage(message);
1308
+ const hashBytes = Buffer.from(hashHex, "hex");
1309
+ try {
1310
+ const recovered = ec.recoverPubKey(hashBytes, { r, s }, v);
1311
+ const recoveredHex = recovered.encode("hex", true);
1312
+ return recoveredHex === expectedPubkey;
1313
+ } catch {
1314
+ return false;
1315
+ }
1316
+ }
1241
1317
 
1242
1318
  // l1/crypto.ts
1243
1319
  var import_crypto_js3 = __toESM(require("crypto-js"), 1);
@@ -14454,6 +14530,23 @@ var Sphere = class _Sphere {
14454
14530
  return this._initialized;
14455
14531
  }
14456
14532
  // ===========================================================================
14533
+ // Public Methods - Signing
14534
+ // ===========================================================================
14535
+ /**
14536
+ * Sign a plaintext message with the wallet's secp256k1 private key.
14537
+ *
14538
+ * Returns a 130-character hex string: v (2) + r (64) + s (64).
14539
+ * The private key never leaves the SDK boundary.
14540
+ *
14541
+ * @throws SphereError if the wallet is not initialized or identity is missing
14542
+ */
14543
+ signMessage(message) {
14544
+ if (!this._identity?.privateKey) {
14545
+ throw new SphereError("Wallet not initialized \u2014 cannot sign", "NOT_INITIALIZED");
14546
+ }
14547
+ return signMessage(this._identity.privateKey, message);
14548
+ }
14549
+ // ===========================================================================
14457
14550
  // Public Methods - Providers Access
14458
14551
  // ===========================================================================
14459
14552
  getStorage() {
@@ -15839,6 +15932,17 @@ var Sphere = class _Sphere {
15839
15932
  if (this._identity?.nametag) {
15840
15933
  throw new SphereError(`Unicity ID already registered for address ${this._currentAddressIndex}: @${this._identity.nametag}`, "ALREADY_INITIALIZED");
15841
15934
  }
15935
+ if (!this._payments.hasNametag()) {
15936
+ logger.debug("Sphere", `Minting nametag token for @${cleanNametag}...`);
15937
+ const result = await this.mintNametag(cleanNametag);
15938
+ if (!result.success) {
15939
+ throw new SphereError(
15940
+ `Failed to mint nametag token: ${result.error}`,
15941
+ "AGGREGATOR_ERROR"
15942
+ );
15943
+ }
15944
+ logger.debug("Sphere", `Nametag token minted successfully`);
15945
+ }
15842
15946
  if (this._transport.publishIdentityBinding) {
15843
15947
  const success = await this._transport.publishIdentityBinding(
15844
15948
  this._identity.chainPubkey,
@@ -15862,15 +15966,6 @@ var Sphere = class _Sphere {
15862
15966
  nametags.set(0, cleanNametag);
15863
15967
  }
15864
15968
  await this.persistAddressNametags();
15865
- if (!this._payments.hasNametag()) {
15866
- logger.debug("Sphere", `Minting nametag token for @${cleanNametag}...`);
15867
- const result = await this.mintNametag(cleanNametag);
15868
- if (!result.success) {
15869
- logger.warn("Sphere", `Failed to mint nametag token: ${result.error}`);
15870
- } else {
15871
- logger.debug("Sphere", `Nametag token minted successfully`);
15872
- }
15873
- }
15874
15969
  this.emitEvent("nametag:registered", {
15875
15970
  nametag: cleanNametag,
15876
15971
  addressIndex: this._currentAddressIndex
@@ -17203,6 +17298,7 @@ function createTokenValidator(options) {
17203
17298
 
17204
17299
  // index.ts
17205
17300
  var import_nostr_js_sdk4 = require("@unicitylabs/nostr-js-sdk");
17301
+ var import_nostr_js_sdk5 = require("@unicitylabs/nostr-js-sdk");
17206
17302
 
17207
17303
  // price/CoinGeckoPriceProvider.ts
17208
17304
  init_logger();
@@ -17475,7 +17571,10 @@ function createPriceProvider(config) {
17475
17571
  NETWORKS,
17476
17572
  NIP29_KINDS,
17477
17573
  NOSTR_EVENT_KINDS,
17574
+ NostrClient,
17575
+ NostrKeyManager,
17478
17576
  PaymentsModule,
17577
+ SIGN_MESSAGE_PREFIX,
17479
17578
  STORAGE_KEYS,
17480
17579
  STORAGE_KEYS_ADDRESS,
17481
17580
  STORAGE_KEYS_GLOBAL,
@@ -17511,6 +17610,7 @@ function createPriceProvider(config) {
17511
17610
  createTokenValidator,
17512
17611
  decodeBech32,
17513
17612
  decryptCMasterKey,
17613
+ decryptNametag,
17514
17614
  decryptPrivateKey,
17515
17615
  decryptTextFormatKey,
17516
17616
  deriveAddressInfo,
@@ -17518,6 +17618,7 @@ function createPriceProvider(config) {
17518
17618
  deriveKeyAtPath,
17519
17619
  doubleSha256,
17520
17620
  encodeBech32,
17621
+ encryptNametag,
17521
17622
  extractFromText,
17522
17623
  findPattern,
17523
17624
  forkedKeyFromTokenIdAndState,
@@ -17542,7 +17643,9 @@ function createPriceProvider(config) {
17542
17643
  hasUncommittedTransactions,
17543
17644
  hasValidTxfData,
17544
17645
  hash160,
17646
+ hashAddressForTag,
17545
17647
  hashNametag,
17648
+ hashSignMessage,
17546
17649
  hexToBytes,
17547
17650
  identityFromMnemonicSync,
17548
17651
  initSphere,
@@ -17584,6 +17687,7 @@ function createPriceProvider(config) {
17584
17687
  randomUUID,
17585
17688
  ripemd160,
17586
17689
  sha256,
17690
+ signMessage,
17587
17691
  sleep,
17588
17692
  sphereExists,
17589
17693
  toHumanReadable,
@@ -17592,7 +17696,8 @@ function createPriceProvider(config) {
17592
17696
  tokenIdFromKey,
17593
17697
  tokenToTxf,
17594
17698
  txfToToken,
17595
- validateMnemonic
17699
+ validateMnemonic,
17700
+ verifySignedMessage
17596
17701
  });
17597
17702
  /*! Bundled license information:
17598
17703