@orangecheck/sdk 1.15.0 → 1.16.0

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/README.md CHANGED
@@ -61,7 +61,6 @@ const outcome = await verify({
61
61
  addr: 'bc1q...',
62
62
  msg: canonicalMessage, // the exact signed text
63
63
  sig: signature,
64
- scheme: 'bip322',
65
64
  });
66
65
 
67
66
  if (outcome.ok) {
@@ -69,6 +68,8 @@ if (outcome.ok) {
69
68
  }
70
69
  ```
71
70
 
71
+ The signature's bytes decide how it is checked: 65 bytes is a legacy (BIP-137) signature, on any address it can stand for, and anything else is BIP-322 simple, with or without the `smp` prefix. `outcome.codes` names the one that verified, `sig_ok_bip322` or `sig_ok_legacy`. A BIP-322 full or proof-of-funds signature gets `sig_unsupported_script`. The `scheme` input is ignored.
72
+
72
73
  Use this when you already hold the `(addr, msg, sig)` tuple — e.g., a user pasted it into your UI, or you're verifying an offline proof.
73
74
 
74
75
  ---
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var base = require('@scure/base');
4
- var buffer = require('buffer');
5
4
  var bip322 = require('@orangecheck/bip322');
5
+ var buffer = require('buffer');
6
6
 
7
7
  // src/canonical.ts
8
8
  function toLowerHex(bytes) {
@@ -157,107 +157,28 @@ function rfc3339Ms(value) {
157
157
  const ms = Date.parse(value);
158
158
  return Number.isNaN(ms) ? null : ms;
159
159
  }
160
- var log = createLogger("ocp/verify");
161
- function looksLikeHex(s) {
162
- return /^[0-9a-fA-F]+$/.test(s) && s.length % 2 === 0;
163
- }
164
160
  function toBase64Maybe(sig) {
165
- if (looksLikeHex(sig)) {
161
+ if (/^[0-9a-fA-F]+$/.test(sig) && sig.length % 2 === 0) {
166
162
  return buffer.Buffer.from(sig, "hex").toString("base64");
167
163
  }
168
164
  return sig;
169
165
  }
170
- function detectSignatureScheme(addr, sig) {
171
- const isSegWit = addr.startsWith("bc1") || addr.startsWith("tb1");
172
- if (isSegWit) {
173
- return "bip322";
174
- }
166
+ function signatureScheme(sigBase64) {
167
+ if (/^(ful|pof)/.test(sigBase64)) return null;
168
+ if (sigBase64.startsWith("smp")) return "bip322";
169
+ return buffer.Buffer.from(sigBase64, "base64").length === 65 ? "legacy" : "bip322";
170
+ }
171
+
172
+ // src/verify.ts
173
+ var log = createLogger("ocp/verify");
174
+ async function verifySignature({ addr, msg, sig }) {
175
175
  const sigBase64 = toBase64Maybe(sig);
176
- const looksLikeLegacy = sigBase64.length === 88 && /^[IHG]/.test(sigBase64);
177
- if (looksLikeLegacy) {
178
- return "legacy";
179
- }
180
- return "bip322";
181
- }
182
- async function verifySignature({
183
- addr,
184
- msg,
185
- sig,
186
- scheme
187
- }) {
188
- const detectedScheme = scheme || detectSignatureScheme(addr, sig);
189
- log.debug(
190
- { addr, msgLength: msg.length, sigLength: sig.length, scheme: detectedScheme },
191
- "Verifying signature"
192
- );
193
- if (detectedScheme === "bip322") {
194
- const sigBase64 = toBase64Maybe(sig);
195
- log.debug(
196
- { sigBase64: sigBase64.substring(0, 50) + "..." },
197
- "Signature converted to base64"
198
- );
199
- log.debug(
200
- {
201
- msgFull: msg,
202
- msgLines: msg.split("\n").length,
203
- msgFirstLine: msg.split("\n")[0],
204
- msgLastLine: msg.split("\n")[msg.split("\n").length - 1]
205
- },
206
- "Message being verified"
207
- );
208
- const ok = bip322.verifySignature(addr, msg, sigBase64);
209
- log.info({ addr, ok }, "BIP-322 verification result");
210
- if (!ok) {
211
- const isSegWit = addr.startsWith("bc1") || addr.startsWith("tb1");
212
- if (isSegWit && sigBase64.length === 88) {
213
- log.warn(
214
- { addr, wallet: "possibly Electrum", sigLength: sigBase64.length },
215
- "BIP-322 verification failed - likely Electrum wallet (does not support BIP-322 for SegWit)"
216
- );
217
- } else {
218
- log.error(
219
- {
220
- addr,
221
- sigLength: sigBase64.length,
222
- msgLength: msg.length,
223
- msgPreview: msg.substring(0, 200)
224
- },
225
- "BIP-322 verification failed - signature does not match message"
226
- );
227
- }
228
- }
229
- return ok ? "sig_ok_bip322" : "sig_invalid";
230
- }
231
- if (detectedScheme === "legacy") {
232
- const sigBase64 = toBase64Maybe(sig);
233
- if (!addr.startsWith("1") && !addr.startsWith("m") && !addr.startsWith("n")) {
234
- log.warn({ addr }, "Legacy signature attempted on non-P2PKH address");
235
- return "sig_unsupported_script";
236
- }
237
- log.debug(
238
- {
239
- addr,
240
- sigBase64: sigBase64.substring(0, 50) + "...",
241
- msgPreview: msg.substring(0, 100) + "...",
242
- msgLength: msg.length
243
- },
244
- "Verifying legacy signature"
245
- );
246
- const ok = bip322.verifySignature(addr, msg, sigBase64);
247
- log.info({ addr, ok, sigBase64Length: sigBase64.length }, "Legacy verification result");
248
- if (!ok) {
249
- log.error(
250
- {
251
- addr,
252
- msgFirstLine: msg.split("\n")[0],
253
- sigFormat: sigBase64.substring(0, 10)
254
- },
255
- "Legacy signature verification failed - check message format and signature"
256
- );
257
- }
258
- return ok ? "sig_ok_legacy" : "sig_invalid";
259
- }
260
- return "invalid_scheme";
176
+ const scheme = signatureScheme(sigBase64);
177
+ if (!scheme) return "sig_unsupported_script";
178
+ const ok = bip322.verifySignature(addr, msg, sigBase64);
179
+ log.info({ addr, ok, scheme, msgLength: msg.length }, "Signature verification result");
180
+ if (!ok) return "sig_invalid";
181
+ return scheme === "legacy" ? "sig_ok_legacy" : "sig_ok_bip322";
261
182
  }
262
183
  async function signatureIsValid(input) {
263
184
  try {