@0xsequence/wallet-core 0.0.0-20250520201059

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.
Files changed (106) hide show
  1. package/.turbo/turbo-build.log +5 -0
  2. package/CHANGELOG.md +9 -0
  3. package/LICENSE +202 -0
  4. package/dist/envelope.d.ts +34 -0
  5. package/dist/envelope.d.ts.map +1 -0
  6. package/dist/envelope.js +96 -0
  7. package/dist/index.d.ts +6 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +5 -0
  10. package/dist/relayer/index.d.ts +4 -0
  11. package/dist/relayer/index.d.ts.map +1 -0
  12. package/dist/relayer/index.js +3 -0
  13. package/dist/relayer/local.d.ts +28 -0
  14. package/dist/relayer/local.d.ts.map +1 -0
  15. package/dist/relayer/local.js +101 -0
  16. package/dist/relayer/pk-relayer.d.ts +18 -0
  17. package/dist/relayer/pk-relayer.d.ts.map +1 -0
  18. package/dist/relayer/pk-relayer.js +88 -0
  19. package/dist/relayer/relayer.d.ts +39 -0
  20. package/dist/relayer/relayer.d.ts.map +1 -0
  21. package/dist/relayer/relayer.js +1 -0
  22. package/dist/signers/index.d.ts +23 -0
  23. package/dist/signers/index.d.ts.map +1 -0
  24. package/dist/signers/index.js +10 -0
  25. package/dist/signers/passkey.d.ts +41 -0
  26. package/dist/signers/passkey.d.ts.map +1 -0
  27. package/dist/signers/passkey.js +196 -0
  28. package/dist/signers/pk/encrypted.d.ts +37 -0
  29. package/dist/signers/pk/encrypted.d.ts.map +1 -0
  30. package/dist/signers/pk/encrypted.js +123 -0
  31. package/dist/signers/pk/index.d.ts +35 -0
  32. package/dist/signers/pk/index.d.ts.map +1 -0
  33. package/dist/signers/pk/index.js +51 -0
  34. package/dist/signers/session/explicit.d.ts +18 -0
  35. package/dist/signers/session/explicit.d.ts.map +1 -0
  36. package/dist/signers/session/explicit.js +126 -0
  37. package/dist/signers/session/implicit.d.ts +20 -0
  38. package/dist/signers/session/implicit.d.ts.map +1 -0
  39. package/dist/signers/session/implicit.js +120 -0
  40. package/dist/signers/session/index.d.ts +4 -0
  41. package/dist/signers/session/index.d.ts.map +1 -0
  42. package/dist/signers/session/index.js +3 -0
  43. package/dist/signers/session/session.d.ts +11 -0
  44. package/dist/signers/session/session.d.ts.map +1 -0
  45. package/dist/signers/session/session.js +1 -0
  46. package/dist/signers/session-manager.d.ts +33 -0
  47. package/dist/signers/session-manager.d.ts.map +1 -0
  48. package/dist/signers/session-manager.js +181 -0
  49. package/dist/state/cached.d.ts +59 -0
  50. package/dist/state/cached.d.ts.map +1 -0
  51. package/dist/state/cached.js +157 -0
  52. package/dist/state/index.d.ts +61 -0
  53. package/dist/state/index.d.ts.map +1 -0
  54. package/dist/state/index.js +4 -0
  55. package/dist/state/local/index.d.ts +98 -0
  56. package/dist/state/local/index.d.ts.map +1 -0
  57. package/dist/state/local/index.js +247 -0
  58. package/dist/state/local/indexed-db.d.ts +41 -0
  59. package/dist/state/local/indexed-db.d.ts.map +1 -0
  60. package/dist/state/local/indexed-db.js +149 -0
  61. package/dist/state/local/memory.d.ts +41 -0
  62. package/dist/state/local/memory.d.ts.map +1 -0
  63. package/dist/state/local/memory.js +77 -0
  64. package/dist/state/remote/dev-http.d.ts +57 -0
  65. package/dist/state/remote/dev-http.d.ts.map +1 -0
  66. package/dist/state/remote/dev-http.js +162 -0
  67. package/dist/state/remote/index.d.ts +2 -0
  68. package/dist/state/remote/index.d.ts.map +1 -0
  69. package/dist/state/remote/index.js +1 -0
  70. package/dist/state/utils.d.ts +12 -0
  71. package/dist/state/utils.d.ts.map +1 -0
  72. package/dist/state/utils.js +29 -0
  73. package/dist/wallet.d.ts +58 -0
  74. package/dist/wallet.d.ts.map +1 -0
  75. package/dist/wallet.js +306 -0
  76. package/package.json +33 -0
  77. package/src/envelope.ts +148 -0
  78. package/src/index.ts +6 -0
  79. package/src/relayer/index.ts +3 -0
  80. package/src/relayer/local.ts +125 -0
  81. package/src/relayer/pk-relayer.ts +110 -0
  82. package/src/relayer/relayer.ts +52 -0
  83. package/src/signers/index.ts +44 -0
  84. package/src/signers/passkey.ts +284 -0
  85. package/src/signers/pk/encrypted.ts +153 -0
  86. package/src/signers/pk/index.ts +77 -0
  87. package/src/signers/session/explicit.ts +173 -0
  88. package/src/signers/session/implicit.ts +145 -0
  89. package/src/signers/session/index.ts +3 -0
  90. package/src/signers/session/session.ts +26 -0
  91. package/src/signers/session-manager.ts +241 -0
  92. package/src/state/cached.ts +233 -0
  93. package/src/state/index.ts +85 -0
  94. package/src/state/local/index.ts +422 -0
  95. package/src/state/local/indexed-db.ts +204 -0
  96. package/src/state/local/memory.ts +126 -0
  97. package/src/state/remote/dev-http.ts +253 -0
  98. package/src/state/remote/index.ts +1 -0
  99. package/src/state/utils.ts +50 -0
  100. package/src/wallet.ts +390 -0
  101. package/test/constants.ts +15 -0
  102. package/test/session-manager.test.ts +451 -0
  103. package/test/setup.ts +63 -0
  104. package/test/wallet.test.ts +90 -0
  105. package/tsconfig.json +10 -0
  106. package/vitest.config.ts +9 -0
@@ -0,0 +1,5 @@
1
+
2
+ 
3
+ > @0xsequence/wallet-core@0.0.0-20250520201059 build /Users/taylan/Development/Horizon/sequence.js/packages/wallet/core
4
+ > tsc
5
+
package/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # @0xsequence/wallet-core
2
+
3
+ ## 0.0.0-20250520201059
4
+
5
+ ### Patch Changes
6
+
7
+ - Alpha snapshot
8
+ - Updated dependencies
9
+ - @0xsequence/wallet-primitives@0.0.0-20250520201059
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,34 @@
1
+ import { Config, Payload, Signature } from '@0xsequence/wallet-primitives';
2
+ import { Address, Hex } from 'ox';
3
+ export type Envelope<T extends Payload.Payload> = {
4
+ readonly wallet: Address.Address;
5
+ readonly chainId: bigint;
6
+ readonly configuration: Config.Config;
7
+ readonly payload: T;
8
+ };
9
+ export type Signature = {
10
+ address: Address.Address;
11
+ signature: Signature.SignatureOfSignerLeaf;
12
+ };
13
+ export type SapientSignature = {
14
+ imageHash: Hex.Hex;
15
+ signature: Signature.SignatureOfSapientSignerLeaf;
16
+ };
17
+ export declare function isSignature(sig: any): sig is Signature;
18
+ export declare function isSapientSignature(sig: any): sig is SapientSignature;
19
+ export type Signed<T extends Payload.Payload> = Envelope<T> & {
20
+ signatures: (Signature | SapientSignature)[];
21
+ };
22
+ export declare function signatureForLeaf(envelope: Signed<Payload.Payload>, leaf: Config.Leaf): Signature | SapientSignature | undefined;
23
+ export declare function weightOf(envelope: Signed<Payload.Payload>): {
24
+ weight: bigint;
25
+ threshold: bigint;
26
+ };
27
+ export declare function reachedThreshold(envelope: Signed<Payload.Payload>): boolean;
28
+ export declare function encodeSignature(envelope: Signed<Payload.Payload>): Signature.RawSignature;
29
+ export declare function toSigned<T extends Payload.Payload>(envelope: Envelope<T>, signatures?: (Signature | SapientSignature)[]): Signed<T>;
30
+ export declare function addSignature(envelope: Signed<Payload.Payload>, signature: Signature | SapientSignature, args?: {
31
+ replace?: boolean;
32
+ }): void;
33
+ export declare function isSigned(envelope: Envelope<Payload.Payload>): envelope is Signed<Payload.Payload>;
34
+ //# sourceMappingURL=envelope.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"envelope.d.ts","sourceRoot":"","sources":["../src/envelope.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAA;AAC1E,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,IAAI,CAAA;AAEjC,MAAM,MAAM,QAAQ,CAAC,CAAC,SAAS,OAAO,CAAC,OAAO,IAAI;IAChD,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,OAAO,CAAA;IAChC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC,MAAM,CAAA;IACrC,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAA;CACpB,CAAA;AAED,MAAM,MAAM,SAAS,GAAG;IACtB,OAAO,EAAE,OAAO,CAAC,OAAO,CAAA;IACxB,SAAS,EAAE,SAAS,CAAC,qBAAqB,CAAA;CAC3C,CAAA;AAGD,MAAM,MAAM,gBAAgB,GAAG;IAC7B,SAAS,EAAE,GAAG,CAAC,GAAG,CAAA;IAClB,SAAS,EAAE,SAAS,CAAC,4BAA4B,CAAA;CAClD,CAAA;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,IAAI,SAAS,CAEtD;AAED,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,IAAI,gBAAgB,CAEpE;AAED,MAAM,MAAM,MAAM,CAAC,CAAC,SAAS,OAAO,CAAC,OAAO,IAAI,QAAQ,CAAC,CAAC,CAAC,GAAG;IAC5D,UAAU,EAAE,CAAC,SAAS,GAAG,gBAAgB,CAAC,EAAE,CAAA;CAC7C,CAAA;AAED,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,4CAepF;AAED,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAMjG;AAED,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAG3E;AAED,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,SAAS,CAAC,YAAY,CASzF;AAED,wBAAgB,QAAQ,CAAC,CAAC,SAAS,OAAO,CAAC,OAAO,EAChD,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EACrB,UAAU,GAAE,CAAC,SAAS,GAAG,gBAAgB,CAAC,EAAO,GAChD,MAAM,CAAC,CAAC,CAAC,CAKX;AAED,wBAAgB,YAAY,CAC1B,QAAQ,EAAE,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EACjC,SAAS,EAAE,SAAS,GAAG,gBAAgB,EACvC,IAAI,CAAC,EAAE;IAAE,OAAO,CAAC,EAAE,OAAO,CAAA;CAAE,QAwD7B;AAED,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,QAAQ,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAEjG"}
@@ -0,0 +1,96 @@
1
+ import { Config, Signature } from '@0xsequence/wallet-primitives';
2
+ import { Address } from 'ox';
3
+ export function isSignature(sig) {
4
+ return typeof sig === 'object' && 'address' in sig && 'signature' in sig && !('imageHash' in sig);
5
+ }
6
+ export function isSapientSignature(sig) {
7
+ return typeof sig === 'object' && 'signature' in sig && 'imageHash' in sig;
8
+ }
9
+ export function signatureForLeaf(envelope, leaf) {
10
+ if (Config.isSignerLeaf(leaf)) {
11
+ return envelope.signatures.find((sig) => isSignature(sig) && Address.isEqual(sig.address, leaf.address));
12
+ }
13
+ if (Config.isSapientSignerLeaf(leaf)) {
14
+ return envelope.signatures.find((sig) => isSapientSignature(sig) &&
15
+ sig.imageHash === leaf.imageHash &&
16
+ Address.isEqual(sig.signature.address, leaf.address));
17
+ }
18
+ return undefined;
19
+ }
20
+ export function weightOf(envelope) {
21
+ const { maxWeight } = Config.getWeight(envelope.configuration, (s) => !!signatureForLeaf(envelope, s));
22
+ return {
23
+ weight: maxWeight,
24
+ threshold: envelope.configuration.threshold,
25
+ };
26
+ }
27
+ export function reachedThreshold(envelope) {
28
+ const { weight, threshold } = weightOf(envelope);
29
+ return weight >= threshold;
30
+ }
31
+ export function encodeSignature(envelope) {
32
+ const topology = Signature.fillLeaves(envelope.configuration.topology, (s) => signatureForLeaf(envelope, s)?.signature);
33
+ return {
34
+ noChainId: envelope.chainId === 0n,
35
+ configuration: { ...envelope.configuration, topology },
36
+ };
37
+ }
38
+ export function toSigned(envelope, signatures = []) {
39
+ return {
40
+ ...envelope,
41
+ signatures,
42
+ };
43
+ }
44
+ export function addSignature(envelope, signature, args) {
45
+ if (isSapientSignature(signature)) {
46
+ // Find if the signature already exists in envelope
47
+ const prev = envelope.signatures.find((sig) => isSapientSignature(sig) &&
48
+ Address.isEqual(sig.signature.address, signature.signature.address) &&
49
+ sig.imageHash === signature.imageHash);
50
+ if (prev) {
51
+ // If the signatures are identical, then we can do nothing
52
+ if (prev.signature.data === signature.signature.data) {
53
+ return;
54
+ }
55
+ // If not and we are replacing, then remove the previous signature
56
+ if (args?.replace) {
57
+ envelope.signatures = envelope.signatures.filter((sig) => sig !== prev);
58
+ }
59
+ else {
60
+ throw new Error('Signature already defined for signer');
61
+ }
62
+ }
63
+ envelope.signatures.push(signature);
64
+ }
65
+ else if (isSignature(signature)) {
66
+ // Find if the signature already exists in envelope
67
+ const prev = envelope.signatures.find((sig) => isSignature(sig) && Address.isEqual(sig.address, signature.address));
68
+ if (prev) {
69
+ // If the signatures are identical, then we can do nothing
70
+ if (prev.signature.type === 'erc1271' && signature.signature.type === 'erc1271') {
71
+ if (prev.signature.data === signature.signature.data) {
72
+ return;
73
+ }
74
+ }
75
+ else if (prev.signature.type !== 'erc1271' && signature.signature.type !== 'erc1271') {
76
+ if (prev.signature.r === signature.signature.r && prev.signature.s === signature.signature.s) {
77
+ return;
78
+ }
79
+ }
80
+ // If not and we are replacing, then remove the previous signature
81
+ if (args?.replace) {
82
+ envelope.signatures = envelope.signatures.filter((sig) => sig !== prev);
83
+ }
84
+ else {
85
+ throw new Error('Signature already defined for signer');
86
+ }
87
+ }
88
+ envelope.signatures.push(signature);
89
+ }
90
+ else {
91
+ throw new Error('Unsupported signature type');
92
+ }
93
+ }
94
+ export function isSigned(envelope) {
95
+ return typeof envelope === 'object' && 'signatures' in envelope;
96
+ }
@@ -0,0 +1,6 @@
1
+ export * from './wallet.js';
2
+ export * as Signers from './signers/index.js';
3
+ export * as State from './state/index.js';
4
+ export * as Relayer from './relayer/index.js';
5
+ export * as Envelope from './envelope.js';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAE3B,OAAO,KAAK,OAAO,MAAM,oBAAoB,CAAA;AAC7C,OAAO,KAAK,KAAK,MAAM,kBAAkB,CAAA;AACzC,OAAO,KAAK,OAAO,MAAM,oBAAoB,CAAA;AAC7C,OAAO,KAAK,QAAQ,MAAM,eAAe,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from './wallet.js';
2
+ export * as Signers from './signers/index.js';
3
+ export * as State from './state/index.js';
4
+ export * as Relayer from './relayer/index.js';
5
+ export * as Envelope from './envelope.js';
@@ -0,0 +1,4 @@
1
+ export * from './relayer.js';
2
+ export * as Local from './local.js';
3
+ export * as Pk from './pk-relayer.js';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/relayer/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AACnC,OAAO,KAAK,EAAE,MAAM,iBAAiB,CAAA"}
@@ -0,0 +1,3 @@
1
+ export * from './relayer.js';
2
+ export * as Local from './local.js';
3
+ export * as Pk from './pk-relayer.js';
@@ -0,0 +1,28 @@
1
+ import { Payload } from '@0xsequence/wallet-primitives';
2
+ import { Address, Hex } from 'ox';
3
+ import { FeeOption, FeeQuote, OperationStatus, Relayer } from './relayer.js';
4
+ type GenericProviderTransactionReceipt = 'success' | 'failed' | 'unknown';
5
+ export interface GenericProvider {
6
+ sendTransaction(args: {
7
+ to: string;
8
+ data: string;
9
+ }, chainId: bigint): Promise<string>;
10
+ getTransactionReceipt(txHash: string, chainId: bigint): Promise<GenericProviderTransactionReceipt>;
11
+ }
12
+ export declare class LocalRelayer implements Relayer {
13
+ readonly provider: GenericProvider;
14
+ readonly id = "local";
15
+ constructor(provider: GenericProvider);
16
+ static createFromWindow(window: Window): LocalRelayer | undefined;
17
+ feeOptions(wallet: Address.Address, chainId: bigint, calls: Payload.Call[]): Promise<{
18
+ options: FeeOption[];
19
+ quote?: FeeQuote;
20
+ }>;
21
+ private decodeCalls;
22
+ relay(to: Address.Address, data: Hex.Hex, chainId: bigint, _?: FeeQuote): Promise<{
23
+ opHash: Hex.Hex;
24
+ }>;
25
+ status(opHash: Hex.Hex, chainId: bigint): Promise<OperationStatus>;
26
+ }
27
+ export {};
28
+ //# sourceMappingURL=local.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"local.d.ts","sourceRoot":"","sources":["../../src/relayer/local.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,OAAO,EAAE,MAAM,+BAA+B,CAAA;AAClE,OAAO,EAAe,OAAO,EAAS,GAAG,EAAsB,MAAM,IAAI,CAAA;AACzE,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAE5E,KAAK,iCAAiC,GAAG,SAAS,GAAG,QAAQ,GAAG,SAAS,CAAA;AAEzE,MAAM,WAAW,eAAe;IAC9B,eAAe,CAAC,IAAI,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IACrF,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,iCAAiC,CAAC,CAAA;CACnG;AAED,qBAAa,YAAa,YAAW,OAAO;aAGd,QAAQ,EAAE,eAAe;IAFrD,SAAgB,EAAE,WAAU;gBAEA,QAAQ,EAAE,eAAe;IAErD,MAAM,CAAC,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS;IA+DjE,UAAU,CACR,MAAM,EAAE,OAAO,CAAC,OAAO,EACvB,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,OAAO,CAAC,IAAI,EAAE,GACpB,OAAO,CAAC;QAAE,OAAO,EAAE,SAAS,EAAE,CAAC;QAAC,KAAK,CAAC,EAAE,QAAQ,CAAA;KAAE,CAAC;IAItD,OAAO,CAAC,WAAW;IAcb,KAAK,CAAC,EAAE,EAAE,OAAO,CAAC,OAAO,EAAE,IAAI,EAAE,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC,EAAE,QAAQ,GAAG,OAAO,CAAC;QAAE,MAAM,EAAE,GAAG,CAAC,GAAG,CAAA;KAAE,CAAC;IAatG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC;CAUzE"}
@@ -0,0 +1,101 @@
1
+ import { Constants, Payload } from '@0xsequence/wallet-primitives';
2
+ import { AbiFunction, Bytes, Hex, TransactionReceipt } from 'ox';
3
+ export class LocalRelayer {
4
+ provider;
5
+ id = 'local';
6
+ constructor(provider) {
7
+ this.provider = provider;
8
+ }
9
+ static createFromWindow(window) {
10
+ const eth = window.ethereum;
11
+ if (!eth) {
12
+ console.warn('Window.ethereum not found, skipping local relayer');
13
+ return undefined;
14
+ }
15
+ const trySwitchChain = async (chainId) => {
16
+ try {
17
+ await eth.request({
18
+ method: 'wallet_switchEthereumChain',
19
+ params: [
20
+ {
21
+ chainId: `0x${chainId.toString(16)}`,
22
+ },
23
+ ],
24
+ });
25
+ }
26
+ catch (error) {
27
+ // Log and continue
28
+ console.error('Error switching chain', error);
29
+ }
30
+ };
31
+ return new LocalRelayer({
32
+ sendTransaction: async (args, chainId) => {
33
+ const accounts = await eth.request({ method: 'eth_requestAccounts' });
34
+ const from = accounts[0];
35
+ if (!from) {
36
+ console.warn('No account selected, skipping local relayer');
37
+ return undefined;
38
+ }
39
+ await trySwitchChain(chainId);
40
+ const tx = await eth.request({
41
+ method: 'eth_sendTransaction',
42
+ params: [
43
+ {
44
+ from,
45
+ to: args.to,
46
+ data: args.data,
47
+ },
48
+ ],
49
+ });
50
+ return tx;
51
+ },
52
+ getTransactionReceipt: async (txHash, chainId) => {
53
+ await trySwitchChain(chainId);
54
+ const rpcReceipt = await eth.request({ method: 'eth_getTransactionReceipt', params: [txHash] });
55
+ if (rpcReceipt) {
56
+ const receipt = TransactionReceipt.fromRpc(rpcReceipt);
57
+ if (receipt?.status === 'success') {
58
+ return 'success';
59
+ }
60
+ else if (receipt?.status === 'reverted') {
61
+ return 'failed';
62
+ }
63
+ }
64
+ return 'unknown';
65
+ },
66
+ });
67
+ }
68
+ feeOptions(wallet, chainId, calls) {
69
+ return Promise.resolve({ options: [] });
70
+ }
71
+ decodeCalls(data) {
72
+ const executeSelector = AbiFunction.getSelector(Constants.EXECUTE);
73
+ let packedPayload;
74
+ if (data.startsWith(executeSelector)) {
75
+ const decode = AbiFunction.decodeData(Constants.EXECUTE, data);
76
+ packedPayload = decode[0];
77
+ }
78
+ else {
79
+ packedPayload = data;
80
+ }
81
+ return Payload.decode(Bytes.fromHex(packedPayload));
82
+ }
83
+ async relay(to, data, chainId, _) {
84
+ const txHash = await this.provider.sendTransaction({
85
+ to,
86
+ data,
87
+ }, chainId);
88
+ Hex.assert(txHash);
89
+ return { opHash: txHash };
90
+ }
91
+ async status(opHash, chainId) {
92
+ const receipt = await this.provider.getTransactionReceipt(opHash, chainId);
93
+ if (receipt === 'unknown') {
94
+ // Could be pending but we don't know
95
+ return { status: 'unknown' };
96
+ }
97
+ return receipt === 'success'
98
+ ? { status: 'confirmed', transactionHash: opHash }
99
+ : { status: 'failed', reason: 'failed' };
100
+ }
101
+ }
@@ -0,0 +1,18 @@
1
+ import { Payload } from '@0xsequence/wallet-primitives';
2
+ import { Address, Hex, Provider } from 'ox';
3
+ import { FeeOption, FeeQuote, OperationStatus, Relayer } from './relayer.js';
4
+ export declare class PkRelayer implements Relayer {
5
+ private readonly provider;
6
+ readonly id = "pk";
7
+ private readonly relayer;
8
+ constructor(privateKey: Hex.Hex, provider: Provider.Provider);
9
+ feeOptions(wallet: Address.Address, chainId: bigint, calls: Payload.Call[]): Promise<{
10
+ options: FeeOption[];
11
+ quote?: FeeQuote;
12
+ }>;
13
+ relay(to: Address.Address, data: Hex.Hex, chainId: bigint, _?: FeeQuote): Promise<{
14
+ opHash: Hex.Hex;
15
+ }>;
16
+ status(opHash: Hex.Hex, chainId: bigint): Promise<OperationStatus>;
17
+ }
18
+ //# sourceMappingURL=pk-relayer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pk-relayer.d.ts","sourceRoot":"","sources":["../../src/relayer/pk-relayer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,+BAA+B,CAAA;AACvD,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,EAA6D,MAAM,IAAI,CAAA;AAEtG,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAE5E,qBAAa,SAAU,YAAW,OAAO;IAMrC,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAL3B,SAAgB,EAAE,QAAO;IACzB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAc;gBAGpC,UAAU,EAAE,GAAG,CAAC,GAAG,EACF,QAAQ,EAAE,QAAQ,CAAC,QAAQ;IA+E9C,UAAU,CACR,MAAM,EAAE,OAAO,CAAC,OAAO,EACvB,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,OAAO,CAAC,IAAI,EAAE,GACpB,OAAO,CAAC;QAAE,OAAO,EAAE,SAAS,EAAE,CAAC;QAAC,KAAK,CAAC,EAAE,QAAQ,CAAA;KAAE,CAAC;IAIhD,KAAK,CAAC,EAAE,EAAE,OAAO,CAAC,OAAO,EAAE,IAAI,EAAE,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC,EAAE,QAAQ,GAAG,OAAO,CAAC;QAAE,MAAM,EAAE,GAAG,CAAC,GAAG,CAAA;KAAE,CAAC;IAQ5G,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC;CAGnE"}
@@ -0,0 +1,88 @@
1
+ import { Address, Hex, Secp256k1, TransactionEnvelopeEip1559, TransactionReceipt } from 'ox';
2
+ import { LocalRelayer } from './local.js';
3
+ export class PkRelayer {
4
+ provider;
5
+ id = 'pk';
6
+ relayer;
7
+ constructor(privateKey, provider) {
8
+ this.provider = provider;
9
+ const relayerAddress = Address.fromPublicKey(Secp256k1.getPublicKey({ privateKey }));
10
+ this.relayer = new LocalRelayer({
11
+ sendTransaction: async (args, chainId) => {
12
+ const providerChainId = BigInt(await this.provider.request({ method: 'eth_chainId' }));
13
+ if (providerChainId !== chainId) {
14
+ throw new Error('Provider chain id does not match relayer chain id');
15
+ }
16
+ const oxArgs = { ...args, to: args.to, data: args.data };
17
+ // Estimate gas with a safety buffer
18
+ const estimatedGas = BigInt(await this.provider.request({ method: 'eth_estimateGas', params: [oxArgs] }));
19
+ const safeGasLimit = estimatedGas > 21000n ? (estimatedGas * 12n) / 10n : 50000n;
20
+ // Get base fee and priority fee
21
+ const baseFee = BigInt(await this.provider.request({ method: 'eth_gasPrice' }));
22
+ const priorityFee = 100000000n; // 0.1 gwei priority fee
23
+ const maxFeePerGas = baseFee + priorityFee;
24
+ // Check sender have enough balance
25
+ const senderBalance = BigInt(await this.provider.request({ method: 'eth_getBalance', params: [relayerAddress, 'latest'] }));
26
+ if (senderBalance < maxFeePerGas * safeGasLimit) {
27
+ console.log('Sender balance:', senderBalance.toString(), 'wei');
28
+ throw new Error('Sender has insufficient balance to pay for gas');
29
+ }
30
+ const nonce = BigInt(await this.provider.request({
31
+ method: 'eth_getTransactionCount',
32
+ params: [relayerAddress, 'latest'],
33
+ }));
34
+ // Build the relay envelope
35
+ const relayEnvelope = TransactionEnvelopeEip1559.from({
36
+ chainId: Number(chainId),
37
+ type: 'eip1559',
38
+ from: relayerAddress,
39
+ to: oxArgs.to,
40
+ data: oxArgs.data,
41
+ gas: safeGasLimit,
42
+ maxFeePerGas: maxFeePerGas,
43
+ maxPriorityFeePerGas: priorityFee,
44
+ nonce: nonce,
45
+ value: 0n,
46
+ });
47
+ const relayerSignature = Secp256k1.sign({
48
+ payload: TransactionEnvelopeEip1559.getSignPayload(relayEnvelope),
49
+ privateKey: privateKey,
50
+ });
51
+ const signedRelayEnvelope = TransactionEnvelopeEip1559.from(relayEnvelope, {
52
+ signature: relayerSignature,
53
+ });
54
+ const tx = await this.provider.request({
55
+ method: 'eth_sendRawTransaction',
56
+ params: [TransactionEnvelopeEip1559.serialize(signedRelayEnvelope)],
57
+ });
58
+ return tx;
59
+ },
60
+ getTransactionReceipt: async (txHash, chainId) => {
61
+ Hex.assert(txHash);
62
+ const providerChainId = BigInt(await this.provider.request({ method: 'eth_chainId' }));
63
+ if (providerChainId !== chainId) {
64
+ throw new Error('Provider chain id does not match relayer chain id');
65
+ }
66
+ const rpcReceipt = await this.provider.request({ method: 'eth_getTransactionReceipt', params: [txHash] });
67
+ if (!rpcReceipt) {
68
+ return 'unknown';
69
+ }
70
+ const receipt = TransactionReceipt.fromRpc(rpcReceipt);
71
+ return receipt.status === 'success' ? 'success' : 'failed';
72
+ },
73
+ });
74
+ }
75
+ feeOptions(wallet, chainId, calls) {
76
+ return this.relayer.feeOptions(wallet, chainId, calls);
77
+ }
78
+ async relay(to, data, chainId, _) {
79
+ const providerChainId = BigInt(await this.provider.request({ method: 'eth_chainId' }));
80
+ if (providerChainId !== chainId) {
81
+ throw new Error('Provider chain id does not match relayer chain id');
82
+ }
83
+ return this.relayer.relay(to, data, chainId);
84
+ }
85
+ status(opHash, chainId) {
86
+ return this.relayer.status(opHash, chainId);
87
+ }
88
+ }