@unicitylabs/sphere-sdk 0.5.7 → 0.5.8

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
@@ -846,6 +846,7 @@ __export(index_exports, {
846
846
  NIP29_KINDS: () => NIP29_KINDS,
847
847
  NOSTR_EVENT_KINDS: () => NOSTR_EVENT_KINDS,
848
848
  PaymentsModule: () => PaymentsModule,
849
+ SIGN_MESSAGE_PREFIX: () => SIGN_MESSAGE_PREFIX,
849
850
  STORAGE_KEYS: () => STORAGE_KEYS,
850
851
  STORAGE_KEYS_ADDRESS: () => STORAGE_KEYS_ADDRESS,
851
852
  STORAGE_KEYS_GLOBAL: () => STORAGE_KEYS_GLOBAL,
@@ -913,6 +914,7 @@ __export(index_exports, {
913
914
  hasValidTxfData: () => hasValidTxfData,
914
915
  hash160: () => hash160,
915
916
  hashNametag: () => import_nostr_js_sdk4.hashNametag,
917
+ hashSignMessage: () => hashSignMessage,
916
918
  hexToBytes: () => hexToBytes,
917
919
  identityFromMnemonicSync: () => identityFromMnemonicSync,
918
920
  initSphere: () => initSphere,
@@ -954,6 +956,7 @@ __export(index_exports, {
954
956
  randomUUID: () => randomUUID,
955
957
  ripemd160: () => ripemd160,
956
958
  sha256: () => sha256,
959
+ signMessage: () => signMessage,
957
960
  sleep: () => sleep,
958
961
  sphereExists: () => sphereExists,
959
962
  toHumanReadable: () => toHumanReadable,
@@ -962,7 +965,8 @@ __export(index_exports, {
962
965
  tokenIdFromKey: () => tokenIdFromKey,
963
966
  tokenToTxf: () => tokenToTxf,
964
967
  txfToToken: () => txfToToken,
965
- validateMnemonic: () => validateMnemonic2
968
+ validateMnemonic: () => validateMnemonic2,
969
+ verifySignedMessage: () => verifySignedMessage
966
970
  });
967
971
  module.exports = __toCommonJS(index_exports);
968
972
 
@@ -1238,6 +1242,73 @@ function generateAddressInfo(privateKey, index, path, prefix = "alpha") {
1238
1242
  index
1239
1243
  };
1240
1244
  }
1245
+ var SIGN_MESSAGE_PREFIX = "Sphere Signed Message:\n";
1246
+ function varint(n) {
1247
+ if (n < 253) return new Uint8Array([n]);
1248
+ const buf = new Uint8Array(3);
1249
+ buf[0] = 253;
1250
+ buf[1] = n & 255;
1251
+ buf[2] = n >> 8 & 255;
1252
+ return buf;
1253
+ }
1254
+ function hashSignMessage(message) {
1255
+ const prefix = new TextEncoder().encode(SIGN_MESSAGE_PREFIX);
1256
+ const msg = new TextEncoder().encode(message);
1257
+ const prefixLen = varint(prefix.length);
1258
+ const msgLen = varint(msg.length);
1259
+ const full = new Uint8Array(prefixLen.length + prefix.length + msgLen.length + msg.length);
1260
+ let off = 0;
1261
+ full.set(prefixLen, off);
1262
+ off += prefixLen.length;
1263
+ full.set(prefix, off);
1264
+ off += prefix.length;
1265
+ full.set(msgLen, off);
1266
+ off += msgLen.length;
1267
+ full.set(msg, off);
1268
+ const hex = Array.from(full).map((b) => b.toString(16).padStart(2, "0")).join("");
1269
+ const h1 = import_crypto_js2.default.SHA256(import_crypto_js2.default.enc.Hex.parse(hex)).toString();
1270
+ return import_crypto_js2.default.SHA256(import_crypto_js2.default.enc.Hex.parse(h1)).toString();
1271
+ }
1272
+ function signMessage(privateKeyHex, message) {
1273
+ const keyPair = ec.keyFromPrivate(privateKeyHex, "hex");
1274
+ const hashHex = hashSignMessage(message);
1275
+ const hashBytes = Buffer.from(hashHex, "hex");
1276
+ const sig = keyPair.sign(hashBytes, { canonical: true });
1277
+ const pub = keyPair.getPublic();
1278
+ let recoveryParam = -1;
1279
+ for (let i = 0; i < 4; i++) {
1280
+ try {
1281
+ if (ec.recoverPubKey(hashBytes, sig, i).eq(pub)) {
1282
+ recoveryParam = i;
1283
+ break;
1284
+ }
1285
+ } catch {
1286
+ }
1287
+ }
1288
+ if (recoveryParam === -1) {
1289
+ throw new SphereError("Could not find recovery parameter", "SIGNING_ERROR");
1290
+ }
1291
+ const v = (31 + recoveryParam).toString(16).padStart(2, "0");
1292
+ const r = sig.r.toString("hex").padStart(64, "0");
1293
+ const s = sig.s.toString("hex").padStart(64, "0");
1294
+ return v + r + s;
1295
+ }
1296
+ function verifySignedMessage(message, signature, expectedPubkey) {
1297
+ if (signature.length !== 130) return false;
1298
+ const v = parseInt(signature.slice(0, 2), 16) - 31;
1299
+ const r = signature.slice(2, 66);
1300
+ const s = signature.slice(66, 130);
1301
+ if (v < 0 || v > 3) return false;
1302
+ const hashHex = hashSignMessage(message);
1303
+ const hashBytes = Buffer.from(hashHex, "hex");
1304
+ try {
1305
+ const recovered = ec.recoverPubKey(hashBytes, { r, s }, v);
1306
+ const recoveredHex = recovered.encode("hex", true);
1307
+ return recoveredHex === expectedPubkey;
1308
+ } catch {
1309
+ return false;
1310
+ }
1311
+ }
1241
1312
 
1242
1313
  // l1/crypto.ts
1243
1314
  var import_crypto_js3 = __toESM(require("crypto-js"), 1);
@@ -14454,6 +14525,23 @@ var Sphere = class _Sphere {
14454
14525
  return this._initialized;
14455
14526
  }
14456
14527
  // ===========================================================================
14528
+ // Public Methods - Signing
14529
+ // ===========================================================================
14530
+ /**
14531
+ * Sign a plaintext message with the wallet's secp256k1 private key.
14532
+ *
14533
+ * Returns a 130-character hex string: v (2) + r (64) + s (64).
14534
+ * The private key never leaves the SDK boundary.
14535
+ *
14536
+ * @throws SphereError if the wallet is not initialized or identity is missing
14537
+ */
14538
+ signMessage(message) {
14539
+ if (!this._identity?.privateKey) {
14540
+ throw new SphereError("Wallet not initialized \u2014 cannot sign", "NOT_INITIALIZED");
14541
+ }
14542
+ return signMessage(this._identity.privateKey, message);
14543
+ }
14544
+ // ===========================================================================
14457
14545
  // Public Methods - Providers Access
14458
14546
  // ===========================================================================
14459
14547
  getStorage() {
@@ -17476,6 +17564,7 @@ function createPriceProvider(config) {
17476
17564
  NIP29_KINDS,
17477
17565
  NOSTR_EVENT_KINDS,
17478
17566
  PaymentsModule,
17567
+ SIGN_MESSAGE_PREFIX,
17479
17568
  STORAGE_KEYS,
17480
17569
  STORAGE_KEYS_ADDRESS,
17481
17570
  STORAGE_KEYS_GLOBAL,
@@ -17543,6 +17632,7 @@ function createPriceProvider(config) {
17543
17632
  hasValidTxfData,
17544
17633
  hash160,
17545
17634
  hashNametag,
17635
+ hashSignMessage,
17546
17636
  hexToBytes,
17547
17637
  identityFromMnemonicSync,
17548
17638
  initSphere,
@@ -17584,6 +17674,7 @@ function createPriceProvider(config) {
17584
17674
  randomUUID,
17585
17675
  ripemd160,
17586
17676
  sha256,
17677
+ signMessage,
17587
17678
  sleep,
17588
17679
  sphereExists,
17589
17680
  toHumanReadable,
@@ -17592,7 +17683,8 @@ function createPriceProvider(config) {
17592
17683
  tokenIdFromKey,
17593
17684
  tokenToTxf,
17594
17685
  txfToToken,
17595
- validateMnemonic
17686
+ validateMnemonic,
17687
+ verifySignedMessage
17596
17688
  });
17597
17689
  /*! Bundled license information:
17598
17690