@orangecheck/sdk 0.1.2 → 0.1.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.
package/dist/index.mjs CHANGED
@@ -1,8 +1,8 @@
1
1
  import { Buffer } from 'buffer';
2
2
  import { Verifier } from 'bip322-js';
3
3
  import * as bitcoinMessage from 'bitcoinjs-message';
4
- import { schnorr } from '@noble/curves/secp256k1';
5
- import { sha256 } from '@noble/hashes/sha2';
4
+ import { schnorr } from '@noble/curves/secp256k1.js';
5
+ import { sha256 } from '@noble/hashes/sha2.js';
6
6
  import { bech32 } from '@scure/base';
7
7
 
8
8
  // src/canonical.ts
@@ -67,9 +67,12 @@ function parseIdentities(identitiesStr) {
67
67
  }
68
68
  return bindings;
69
69
  }
70
- function buildCanonicalMessage({ address, identities }, extensions = {}) {
71
- const nonce = randomNonce16BHexLower();
72
- const issuedAt = (/* @__PURE__ */ new Date()).toISOString();
70
+ function buildCanonicalMessage({ address, identities }, extensions = {}, options = {}) {
71
+ const nonce = options.nonce ?? randomNonce16BHexLower();
72
+ if (!/^[0-9a-f]{32}$/.test(nonce)) {
73
+ throw new Error(`invalid nonce: must be 32 lowercase hex chars`);
74
+ }
75
+ const issuedAt = options.issuedAt ?? (/* @__PURE__ */ new Date()).toISOString();
73
76
  const identitiesStr = formatIdentities(identities);
74
77
  const core = [
75
78
  "orangecheck",
@@ -233,16 +236,15 @@ async function publishToRelays(event, relays = DEFAULT_RELAYS) {
233
236
  ws = new WebSocket(relayUrl);
234
237
  const wsRef = ws;
235
238
  await new Promise((resolve, reject) => {
236
- const timeout = setTimeout(() => {
239
+ const deadline = setTimeout(() => {
237
240
  wsRef.close();
238
- reject(new Error("Connection timeout"));
239
- }, 5e3);
241
+ reject(new Error("Relay publish timeout"));
242
+ }, 1e4);
240
243
  const cleanup = () => {
241
- clearTimeout(timeout);
244
+ clearTimeout(deadline);
242
245
  wsRef.close();
243
246
  };
244
247
  wsRef.onopen = () => {
245
- clearTimeout(timeout);
246
248
  wsRef.send(JSON.stringify(["EVENT", event]));
247
249
  };
248
250
  wsRef.onmessage = (msg) => {
@@ -280,7 +282,7 @@ async function publishToRelays(event, relays = DEFAULT_RELAYS) {
280
282
  reject(err);
281
283
  };
282
284
  wsRef.onclose = () => {
283
- clearTimeout(timeout);
285
+ clearTimeout(deadline);
284
286
  };
285
287
  });
286
288
  } catch (err) {
@@ -658,6 +660,24 @@ async function getAttestationsForIdentity(protocol, identifier, relays = DEFAULT
658
660
  log3.info({ protocol, identifier }, "Getting all attestations for identity");
659
661
  return discoverAttestations({ identity: { protocol, identifier }, relays });
660
662
  }
663
+ function formatIdentitiesForDisplay(identities) {
664
+ return identities.map((id) => `${id.protocol}:${id.identifier}`).join(", ");
665
+ }
666
+ function getVerificationUrl(attestationId, baseUrl = "https://ochk.io") {
667
+ return `${baseUrl}/attest/${attestationId}`;
668
+ }
669
+ function extractAttestationIdFromUrl(url) {
670
+ try {
671
+ const u = new URL(url);
672
+ const m = u.pathname.match(/^\/attest\/([0-9a-f]{64})\/?$/i);
673
+ if (m) return m[1].toLowerCase();
674
+ const q = u.searchParams.get("id");
675
+ if (q && /^[0-9a-f]{64}$/i.test(q)) return q.toLowerCase();
676
+ return null;
677
+ } catch {
678
+ return null;
679
+ }
680
+ }
661
681
 
662
682
  // src/scoring.ts
663
683
  function scoreV0(sats, days) {
@@ -681,12 +701,12 @@ function computeScore(sats, days, config) {
681
701
  return null;
682
702
  }
683
703
  }
704
+ function computeAllScores(sats, days) {
705
+ return { v0: scoreV0(sats, days), tier: scoreTier(sats, days) };
706
+ }
684
707
 
685
708
  // src/verify.ts
686
709
  var log4 = createLogger("ocp/verify");
687
- function scoreV02(sats, days) {
688
- return computeScore(sats, days, { algorithm: "v0" });
689
- }
690
710
  function looksLikeHex(s) {
691
711
  return /^[0-9a-fA-F]+$/.test(s) && s.length % 2 === 0;
692
712
  }
@@ -950,7 +970,7 @@ async function verify2(input, opts = {}) {
950
970
  const metrics = {
951
971
  sats_bonded,
952
972
  days_unspent,
953
- score: scoreV02(sats_bonded, days_unspent)
973
+ score: computeScore(sats_bonded, days_unspent, { algorithm: "v0" })
954
974
  // ≈ 55.26
955
975
  };
956
976
  codes.push("sig_ok_bip322");
@@ -1046,7 +1066,7 @@ async function verify2(input, opts = {}) {
1046
1066
  const metrics = {
1047
1067
  sats_bonded,
1048
1068
  days_unspent,
1049
- score: scoreV02(sats_bonded, days_unspent)
1069
+ score: computeScore(sats_bonded, days_unspent, { algorithm: "v0" })
1050
1070
  };
1051
1071
  return {
1052
1072
  ok: true,
@@ -1184,16 +1204,20 @@ function parseChallenge(message) {
1184
1204
  throw new Error("not an orangecheck-auth challenge");
1185
1205
  }
1186
1206
  const out = { extensions: [] };
1187
- const coreKeys = /* @__PURE__ */ new Set(["address", "nonce", "issued_at", "expires_at", "ack"]);
1188
- let seenCore = 0;
1207
+ const coreKeys = ["address", "nonce", "issued_at", "expires_at", "ack"];
1208
+ const coreKeySet = new Set(coreKeys);
1209
+ const seenCore = /* @__PURE__ */ new Set();
1189
1210
  for (let i = 1; i < lines.length; i++) {
1190
1211
  const line = lines[i] ?? "";
1191
1212
  const sep = line.indexOf(": ");
1192
1213
  if (sep === -1) continue;
1193
1214
  const key = line.slice(0, sep);
1194
1215
  const val = line.slice(sep + 2);
1195
- if (coreKeys.has(key)) {
1196
- seenCore++;
1216
+ if (coreKeySet.has(key)) {
1217
+ if (seenCore.has(key)) {
1218
+ throw new Error(`duplicate core key in challenge: ${key}`);
1219
+ }
1220
+ seenCore.add(key);
1197
1221
  if (key === "address") out.address = val;
1198
1222
  else if (key === "nonce") out.nonce = val;
1199
1223
  else if (key === "issued_at") out.issuedAt = new Date(val);
@@ -1205,8 +1229,8 @@ function parseChallenge(message) {
1205
1229
  if (key === "purpose") out.purpose = val;
1206
1230
  }
1207
1231
  }
1208
- if (seenCore !== 5) {
1209
- throw new Error(`challenge missing core lines (${seenCore}/5)`);
1232
+ if (seenCore.size !== coreKeys.length) {
1233
+ throw new Error(`challenge missing core lines (${seenCore.size}/${coreKeys.length})`);
1210
1234
  }
1211
1235
  return out;
1212
1236
  }
@@ -2066,6 +2090,6 @@ var STATUS_META = {
2066
2090
  }
2067
2091
  };
2068
2092
 
2069
- export { DEFAULT_RELAYS, STATUS_META, buildCanonicalMessage, check, computeScore, createAttestation, createAttestationEnvelope, createAttestationEvent, discoverAttestations, formatIdentities, generateAttestationId, generateGistTemplate, generateTweetText, generateWellKnownContent, getAttestationsForAddress, getAttestationsForIdentity, getDnsVerificationInstructions, getGistCreationUrl, getGitHubVerificationInstructions, getManualVerificationInstructions, getNip07Info, getNostrPublicKey, getNostrVerificationInstructions, getTweetUrl, getTwitterComposeUrl, getTwitterVerificationInstructions, getVerificationInstructions, getVerificationSummary, getWellKnownUrl, isDnsIdentityVerified, isGitHubIdentityVerified, isNip07Available, isNostrIdentityVerified, isValidDomain, isValidTweetUrl, issueChallenge, parseAttestationFromEvent, parseIdentities, publishAttestation, publishToRelays, queryByAddress, queryByAttestationId, queryByIdentity, requiresManualVerification, signNostrEvent, supportsAutomaticVerification, verify2 as verify, verifyAllIdentities, verifyAttestationById, verifyChallenge, verifyDnsIdentity, verifyGitHubIdentity, verifyIdentity, verifyNostrIdentity, verifyTwitterIdentity };
2093
+ export { DEFAULT_RELAYS, STATUS_META, buildCanonicalMessage, check, computeAllScores, computeScore, createAttestation, createAttestationEnvelope, createAttestationEvent, discoverAttestations, extractAttestationIdFromUrl, formatIdentities, formatIdentitiesForDisplay, generateAttestationId, generateGistTemplate, generateTweetText, generateWellKnownContent, getAttestationsForAddress, getAttestationsForIdentity, getDnsVerificationInstructions, getGistCreationUrl, getGitHubVerificationInstructions, getManualVerificationInstructions, getNip07Info, getNostrPublicKey, getNostrVerificationInstructions, getTweetUrl, getTwitterComposeUrl, getTwitterVerificationInstructions, getVerificationInstructions, getVerificationSummary, getVerificationUrl, getWellKnownUrl, isDnsIdentityVerified, isGitHubIdentityVerified, isNip07Available, isNostrIdentityVerified, isValidDomain, isValidTweetUrl, issueChallenge, parseAttestationFromEvent, parseIdentities, publishAttestation, publishToRelays, queryByAddress, queryByAttestationId, queryByIdentity, requiresManualVerification, signNostrEvent, supportsAutomaticVerification, verify2 as verify, verifyAllIdentities, verifyAttestationById, verifyChallenge, verifyDnsIdentity, verifyGitHubIdentity, verifyIdentity, verifyNostrIdentity, verifyTwitterIdentity };
2070
2094
  //# sourceMappingURL=index.mjs.map
2071
2095
  //# sourceMappingURL=index.mjs.map