@orangecheck/sdk 1.15.0 → 1.16.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.
@@ -1,6 +1,6 @@
1
1
  import { bech32 } from '@scure/base';
2
- import { Buffer } from 'buffer';
3
2
  import { verifySignature as verifySignature$1 } from '@orangecheck/bip322';
3
+ import { Buffer } from 'buffer';
4
4
 
5
5
  // src/canonical.ts
6
6
  function toLowerHex(bytes) {
@@ -155,107 +155,28 @@ function rfc3339Ms(value) {
155
155
  const ms = Date.parse(value);
156
156
  return Number.isNaN(ms) ? null : ms;
157
157
  }
158
- var log = createLogger("ocp/verify");
159
- function looksLikeHex(s) {
160
- return /^[0-9a-fA-F]+$/.test(s) && s.length % 2 === 0;
161
- }
162
158
  function toBase64Maybe(sig) {
163
- if (looksLikeHex(sig)) {
159
+ if (/^[0-9a-fA-F]+$/.test(sig) && sig.length % 2 === 0) {
164
160
  return Buffer.from(sig, "hex").toString("base64");
165
161
  }
166
162
  return sig;
167
163
  }
168
- function detectSignatureScheme(addr, sig) {
169
- const isSegWit = addr.startsWith("bc1") || addr.startsWith("tb1");
170
- if (isSegWit) {
171
- return "bip322";
172
- }
164
+ function signatureScheme(sigBase64) {
165
+ if (/^(ful|pof)/.test(sigBase64)) return null;
166
+ if (sigBase64.startsWith("smp")) return "bip322";
167
+ return Buffer.from(sigBase64, "base64").length === 65 ? "legacy" : "bip322";
168
+ }
169
+
170
+ // src/verify.ts
171
+ var log = createLogger("ocp/verify");
172
+ async function verifySignature({ addr, msg, sig }) {
173
173
  const sigBase64 = toBase64Maybe(sig);
174
- const looksLikeLegacy = sigBase64.length === 88 && /^[IHG]/.test(sigBase64);
175
- if (looksLikeLegacy) {
176
- return "legacy";
177
- }
178
- return "bip322";
179
- }
180
- async function verifySignature({
181
- addr,
182
- msg,
183
- sig,
184
- scheme
185
- }) {
186
- const detectedScheme = scheme || detectSignatureScheme(addr, sig);
187
- log.debug(
188
- { addr, msgLength: msg.length, sigLength: sig.length, scheme: detectedScheme },
189
- "Verifying signature"
190
- );
191
- if (detectedScheme === "bip322") {
192
- const sigBase64 = toBase64Maybe(sig);
193
- log.debug(
194
- { sigBase64: sigBase64.substring(0, 50) + "..." },
195
- "Signature converted to base64"
196
- );
197
- log.debug(
198
- {
199
- msgFull: msg,
200
- msgLines: msg.split("\n").length,
201
- msgFirstLine: msg.split("\n")[0],
202
- msgLastLine: msg.split("\n")[msg.split("\n").length - 1]
203
- },
204
- "Message being verified"
205
- );
206
- const ok = verifySignature$1(addr, msg, sigBase64);
207
- log.info({ addr, ok }, "BIP-322 verification result");
208
- if (!ok) {
209
- const isSegWit = addr.startsWith("bc1") || addr.startsWith("tb1");
210
- if (isSegWit && sigBase64.length === 88) {
211
- log.warn(
212
- { addr, wallet: "possibly Electrum", sigLength: sigBase64.length },
213
- "BIP-322 verification failed - likely Electrum wallet (does not support BIP-322 for SegWit)"
214
- );
215
- } else {
216
- log.error(
217
- {
218
- addr,
219
- sigLength: sigBase64.length,
220
- msgLength: msg.length,
221
- msgPreview: msg.substring(0, 200)
222
- },
223
- "BIP-322 verification failed - signature does not match message"
224
- );
225
- }
226
- }
227
- return ok ? "sig_ok_bip322" : "sig_invalid";
228
- }
229
- if (detectedScheme === "legacy") {
230
- const sigBase64 = toBase64Maybe(sig);
231
- if (!addr.startsWith("1") && !addr.startsWith("m") && !addr.startsWith("n")) {
232
- log.warn({ addr }, "Legacy signature attempted on non-P2PKH address");
233
- return "sig_unsupported_script";
234
- }
235
- log.debug(
236
- {
237
- addr,
238
- sigBase64: sigBase64.substring(0, 50) + "...",
239
- msgPreview: msg.substring(0, 100) + "...",
240
- msgLength: msg.length
241
- },
242
- "Verifying legacy signature"
243
- );
244
- const ok = verifySignature$1(addr, msg, sigBase64);
245
- log.info({ addr, ok, sigBase64Length: sigBase64.length }, "Legacy verification result");
246
- if (!ok) {
247
- log.error(
248
- {
249
- addr,
250
- msgFirstLine: msg.split("\n")[0],
251
- sigFormat: sigBase64.substring(0, 10)
252
- },
253
- "Legacy signature verification failed - check message format and signature"
254
- );
255
- }
256
- return ok ? "sig_ok_legacy" : "sig_invalid";
257
- }
258
- return "invalid_scheme";
174
+ const scheme = signatureScheme(sigBase64);
175
+ if (!scheme) return "sig_unsupported_script";
176
+ const ok = verifySignature$1(addr, msg, sigBase64);
177
+ log.info({ addr, ok, scheme, msgLength: msg.length }, "Signature verification result");
178
+ if (!ok) return "sig_invalid";
179
+ return scheme === "legacy" ? "sig_ok_legacy" : "sig_ok_bip322";
259
180
  }
260
181
  async function signatureIsValid(input) {
261
182
  try {