Skip to content

Commit 30b3793

Browse files
committed
feat: Add required patches
1 parent 592bfba commit 30b3793

5 files changed

+18580
-0
lines changed
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
diff --git a/.DS_Store b/.DS_Store
2+
new file mode 100644
3+
index 0000000000000000000000000000000000000000..9211d3bfe85cf15dbed0f66b9014a322c5f36ceb
4+
--- /dev/null
5+
+++ b/.DS_Store
6+
@@ -0,0 +1 @@
7+
+
8+
\ No newline at end of file
9+
diff --git a/dist/index.js b/dist/index.js
10+
index 4a914633a13067af05838ab582a9dd41b53306ba..68df133248e9a1d067735ad1915f64afd4563057 100644
11+
--- a/dist/index.js
12+
+++ b/dist/index.js
13+
@@ -4,6 +4,27 @@ var secp256k1 = require('@noble/curves/secp256k1');
14+
var mod = require('@noble/curves/abstract/modular');
15+
var utils = require('@noble/curves/abstract/utils');
16+
17+
+// Prefer native Nitro implementation when available
18+
+let pointAddScalarNative;
19+
+try {
20+
+ const NativeAvalabsCrypto = require('react-native-nitro-avalabs-crypto');
21+
+ pointAddScalarNative = NativeAvalabsCrypto && NativeAvalabsCrypto.pointAddScalar;
22+
+} catch (_) {
23+
+ pointAddScalarNative = undefined;
24+
+}
25+
+
26+
+// Native sign/verify (Nitro) support
27+
+let signNative, verifyNative, signSchnorrNative, verifySchnorrNative;
28+
+try {
29+
+ const NativeAvalabsCrypto = require('react-native-nitro-avalabs-crypto');
30+
+ signNative = NativeAvalabsCrypto && NativeAvalabsCrypto.sign;
31+
+ verifyNative = NativeAvalabsCrypto && NativeAvalabsCrypto.verify;
32+
+ signSchnorrNative = NativeAvalabsCrypto && NativeAvalabsCrypto.signSchnorr;
33+
+ verifySchnorrNative = NativeAvalabsCrypto && NativeAvalabsCrypto.verifySchnorr;
34+
+} catch (_) {
35+
+ signNative = verifyNative = signSchnorrNative = verifySchnorrNative = undefined;
36+
+}
37+
+
38+
function _interopNamespaceDefault(e) {
39+
var n = Object.create(null);
40+
if (e) {
41+
@@ -176,6 +197,31 @@ function _privateNegate(privateKey) {
42+
}
43+
44+
function _pointAddScalar(p, tweak, isCompressed) {
45+
+ // Try native (RN Nitro) fast-path first
46+
+ if (typeof pointAddScalarNative === 'function') {
47+
+ try {
48+
+ const out = pointAddScalarNative(p, tweak, isCompressed);
49+
+ if (out.length === 0) {
50+
+ throw new Error("Tweaked point at infinity");
51+
+ }
52+
+
53+
+ const con = console;
54+
+ con.log('pointAddScalarNative output:', out);
55+
+
56+
+ // Native returns ArrayBuffer or Uint8Array
57+
+ return out instanceof ArrayBuffer ? new Uint8Array(out) : out;
58+
+ } catch (e) { /* fall back to JS path below */
59+
+ if (e instanceof Error && e.message === "Tweaked point at infinity") {
60+
+ throw e;
61+
+ }
62+
+ const con = console;
63+
+ con.log('pointAddScalarNative failed, using JS fallback', e);
64+
+ }
65+
+ } else {
66+
+ const con = console;
67+
+ con.log('pointAddScalarNative not available, using JS fallback');
68+
+ }
69+
+ // Fallback to JS implementation
70+
const P = fromHex(p);
71+
const t = normalizeScalar(tweak);
72+
// multiplyAndAddUnsafe(P, scalar, 1) = P + scalar*G
73+
@@ -361,6 +407,18 @@ function sign(h, d, e) {
74+
if (!isExtraData(e)) {
75+
throw new Error(THROW_BAD_EXTRA_DATA);
76+
}
77+
+
78+
+ // Try native first
79+
+ if (typeof signNative === 'function') {
80+
+ try {
81+
+ const result = signNative(d, h);
82+
+ return result instanceof ArrayBuffer ? new Uint8Array(result) : result;
83+
+ } catch (_) {
84+
+ // fallback
85+
+ }
86+
+ }
87+
+
88+
+ // JS fallback
89+
return secp256k1.secp256k1.sign(h, d, { extraEntropy: e }).toCompactRawBytes();
90+
}
91+
92+
@@ -381,7 +439,7 @@ function signRecoverable(h, d, e) {
93+
};
94+
}
95+
96+
-function signSchnorr(h, d, e) {
97+
+function signSchnorr(h, d, e, forceJs = false) {
98+
if (!isPrivate(d)) {
99+
throw new Error(THROW_BAD_PRIVATE);
100+
}
101+
@@ -391,6 +449,18 @@ function signSchnorr(h, d, e) {
102+
if (!isExtraData(e)) {
103+
throw new Error(THROW_BAD_EXTRA_DATA);
104+
}
105+
+
106+
+ // Try native first
107+
+ if (typeof signSchnorrNative === 'function' && !forceJs) {
108+
+ try {
109+
+ const result = signSchnorrNative(h, d, e);
110+
+ return result instanceof ArrayBuffer ? new Uint8Array(result) : result;
111+
+ } catch (_) {
112+
+ // fallback to JS implementation
113+
+ }
114+
+ }
115+
+
116+
+ // JS fallback
117+
return secp256k1.schnorr.sign(h, d, e);
118+
}
119+
120+
@@ -428,10 +498,21 @@ function verify(h, Q, signature, strict) {
121+
if (!isHash(h)) {
122+
throw new Error(THROW_BAD_SCALAR);
123+
}
124+
+
125+
+ // Try native verify first
126+
+ if (typeof verifyNative === 'function') {
127+
+ try {
128+
+ return !!verifyNative(Q, h, signature);
129+
+ } catch (_) {
130+
+ // fallback
131+
+ }
132+
+ }
133+
+
134+
+ // JS fallback
135+
return secp256k1.secp256k1.verify(signature, h, Q, { lowS: strict });
136+
}
137+
138+
-function verifySchnorr(h, Q, signature) {
139+
+function verifySchnorr(h, Q, signature, forceJs = false) {
140+
if (!isXOnlyPoint(Q)) {
141+
throw new Error(THROW_BAD_POINT);
142+
}
143+
@@ -441,6 +522,17 @@ function verifySchnorr(h, Q, signature) {
144+
if (!isHash(h)) {
145+
throw new Error(THROW_BAD_SCALAR);
146+
}
147+
+
148+
+ // Try native verify first
149+
+ if (typeof verifySchnorrNative === 'function' && !forceJs) {
150+
+ try {
151+
+ return !!verifySchnorrNative(Q, h, signature);
152+
+ } catch (_) {
153+
+ // fallback to JS implementation
154+
+ }
155+
+ }
156+
+
157+
+ // JS fallback
158+
return secp256k1.schnorr.verify(signature, h, Q);
159+
}
160+
161+
@@ -465,3 +557,4 @@ exports.verifySchnorr = verifySchnorr;
162+
exports.xOnlyPointAddTweak = xOnlyPointAddTweak;
163+
exports.xOnlyPointFromPoint = xOnlyPointFromPoint;
164+
exports.xOnlyPointFromScalar = xOnlyPointFromScalar;
165+
+
166+
diff --git a/package.json b/package.json
167+
index 8966c2604340ecac199be87de4517dd5b2d81a0a..13aea201ba47186003710bc30ecccc1a89118e7e 100644
168+
--- a/package.json
169+
+++ b/package.json
170+
@@ -42,5 +42,35 @@
171+
},
172+
"dependencies": {
173+
"@noble/curves": "^1.7.0"
174+
+ },
175+
+ "react-native": {
176+
+ "zlib": "browserify-zlib",
177+
+ "http": "@tradle/react-native-http",
178+
+ "https": "https-browserify",
179+
+ "os": "react-native-os",
180+
+ "path": "path-browserify",
181+
+ "fs": "react-native-level-fs",
182+
+ "_stream_transform": "readable-stream/transform",
183+
+ "_stream_readable": "readable-stream/readable",
184+
+ "_stream_writable": "readable-stream/writable",
185+
+ "_stream_duplex": "readable-stream/duplex",
186+
+ "_stream_passthrough": "readable-stream/passthrough",
187+
+ "stream": "stream-browserify",
188+
+ "vm": "vm-browserify"
189+
+ },
190+
+ "browser": {
191+
+ "zlib": "browserify-zlib",
192+
+ "http": "@tradle/react-native-http",
193+
+ "https": "https-browserify",
194+
+ "os": "react-native-os",
195+
+ "path": "path-browserify",
196+
+ "fs": "react-native-level-fs",
197+
+ "_stream_transform": "readable-stream/transform",
198+
+ "_stream_readable": "readable-stream/readable",
199+
+ "_stream_writable": "readable-stream/writable",
200+
+ "_stream_duplex": "readable-stream/duplex",
201+
+ "_stream_passthrough": "readable-stream/passthrough",
202+
+ "stream": "stream-browserify",
203+
+ "vm": "vm-browserify"
204+
}
205+
}

0 commit comments

Comments
 (0)