@effectstream/wallets 0.101.1 → 0.103.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/LICENSE-APACHE +201 -0
- package/LICENSE-MIT +21 -0
- package/package.json +23 -17
- package/src/midnight/local.ts +9 -9
- package/src/solana/local.test.ts +29 -0
- package/src/solana/local.ts +115 -0
- package/src/solana/solana.test.ts +135 -0
- package/src/solana/solana.ts +366 -0
- package/src/solana/wrapper.ts +22 -0
- package/src/utils.ts +4 -0
- package/src/wallet-modes.ts +4 -2
- package/src/wallets.ts +4 -0
package/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2024-2026 Midnight Foundation
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2026 Midnight Foundation
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effectstream/wallets",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"license": "MIT",
|
|
3
|
+
"version": "0.103.0",
|
|
4
|
+
"license": "(MIT OR Apache-2.0)",
|
|
5
5
|
"homepage": "https://effectstream.github.io/docs/",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -15,16 +15,18 @@
|
|
|
15
15
|
".": "./src/mod.ts",
|
|
16
16
|
"./evm-viem": "./src/evm/viem.ts",
|
|
17
17
|
"./cardano-local": "./src/cardano/local.ts",
|
|
18
|
-
"./midnight-local": "./src/midnight/local.ts"
|
|
18
|
+
"./midnight-local": "./src/midnight/local.ts",
|
|
19
|
+
"./solana": "./src/solana/solana.ts",
|
|
20
|
+
"./solana-local": "./src/solana/local.ts"
|
|
19
21
|
},
|
|
20
22
|
"peerDependencies": {
|
|
21
|
-
"@effectstream/midnight-contracts": "0.
|
|
23
|
+
"@effectstream/midnight-contracts": "0.103.0",
|
|
22
24
|
"@lucid-evolution/lucid": "^0.4.29",
|
|
23
25
|
"@lucid-evolution/provider": "^0.1.90",
|
|
24
26
|
"@lucid-evolution/utils": "^0.1.66",
|
|
25
|
-
"@midnight-ntwrk/ledger-v8": "^8.0
|
|
26
|
-
"@
|
|
27
|
-
"@
|
|
27
|
+
"@midnight-ntwrk/ledger-v8": "^8.1.0",
|
|
28
|
+
"@midnightntwrk/wallet-sdk-hd": "^3.0.3",
|
|
29
|
+
"@midnightntwrk/wallet-sdk-unshielded-wallet": "^3.1.0"
|
|
28
30
|
},
|
|
29
31
|
"peerDependenciesMeta": {
|
|
30
32
|
"@effectstream/midnight-contracts": {
|
|
@@ -42,31 +44,34 @@
|
|
|
42
44
|
"@midnight-ntwrk/ledger-v8": {
|
|
43
45
|
"optional": true
|
|
44
46
|
},
|
|
45
|
-
"@
|
|
47
|
+
"@midnightntwrk/wallet-sdk-hd": {
|
|
46
48
|
"optional": true
|
|
47
49
|
},
|
|
48
|
-
"@
|
|
50
|
+
"@midnightntwrk/wallet-sdk-unshielded-wallet": {
|
|
49
51
|
"optional": true
|
|
50
52
|
}
|
|
51
53
|
},
|
|
52
54
|
"devDependencies": {
|
|
53
|
-
"@effectstream/midnight-contracts": "0.
|
|
55
|
+
"@effectstream/midnight-contracts": "0.103.0",
|
|
54
56
|
"@lucid-evolution/lucid": "^0.4.30",
|
|
55
57
|
"@lucid-evolution/provider": "^0.1.91",
|
|
56
58
|
"@lucid-evolution/utils": "^0.1.66",
|
|
57
|
-
"@midnight-ntwrk/ledger-v8": "8.0
|
|
58
|
-
"@
|
|
59
|
-
"@
|
|
59
|
+
"@midnight-ntwrk/ledger-v8": "8.1.0",
|
|
60
|
+
"@midnightntwrk/wallet-sdk-hd": "3.0.3",
|
|
61
|
+
"@midnightntwrk/wallet-sdk-unshielded-wallet": "3.1.0"
|
|
60
62
|
},
|
|
61
63
|
"dependencies": {
|
|
62
|
-
"@effectstream/concise": "0.
|
|
63
|
-
"@effectstream/crypto": "0.
|
|
64
|
-
"@effectstream/event-client": "0.
|
|
65
|
-
"@effectstream/utils": "0.
|
|
64
|
+
"@effectstream/concise": "0.103.0",
|
|
65
|
+
"@effectstream/crypto": "0.103.0",
|
|
66
|
+
"@effectstream/event-client": "0.103.0",
|
|
67
|
+
"@effectstream/utils": "0.103.0",
|
|
66
68
|
"@aurowallet/mina-provider": "^1.0.12",
|
|
67
69
|
"@metamask/providers": "^22.1.0",
|
|
68
70
|
"@midnight-ntwrk/dapp-connector-api": "4.0.1",
|
|
69
71
|
"@perawallet/connect": "^1.4.2",
|
|
72
|
+
"@noble/curves": "^1.8.0",
|
|
73
|
+
"@solana/web3.js": "^1.95.0",
|
|
74
|
+
"@wallet-standard/app": "^1.1.0",
|
|
70
75
|
"@polkadot/api": "15.10.2",
|
|
71
76
|
"@polkadot/extension-dapp": "^0.58.10",
|
|
72
77
|
"@polkadot/extension-inject": "^0.58.10",
|
|
@@ -75,6 +80,7 @@
|
|
|
75
80
|
"@types/semver": "7.7.1",
|
|
76
81
|
"assert-never": "^1.4.0",
|
|
77
82
|
"avail-js-sdk": "^0.4.2",
|
|
83
|
+
"bs58": "^6.0.0",
|
|
78
84
|
"bech32": "^2.0.0",
|
|
79
85
|
"ethers": "^6.15.0",
|
|
80
86
|
"semver": "^7.7.3",
|
package/src/midnight/local.ts
CHANGED
|
@@ -73,7 +73,7 @@ export type MidnightLocalApi = {
|
|
|
73
73
|
/**
|
|
74
74
|
* Full Midnight `WalletFacade`. Populated only when `networkUrls` was passed
|
|
75
75
|
* to `connectFromSeed`. Cast to
|
|
76
|
-
* `import("@
|
|
76
|
+
* `import("@midnightntwrk/wallet-sdk-facade").WalletFacade` for typed
|
|
77
77
|
* access. Carries `submitTransaction`, `balanceFinalizedTransaction`,
|
|
78
78
|
* `balanceUnboundTransaction`, `balanceUnprovenTransaction`, `signRecipe`,
|
|
79
79
|
* `signUnprovenTransaction`, `state()`, `waitForSyncedState()`, etc.
|
|
@@ -328,28 +328,28 @@ export class MidnightLocalProvider implements IProvider<MidnightApi> {
|
|
|
328
328
|
}
|
|
329
329
|
|
|
330
330
|
async function loadKeystoreDeps(): Promise<{
|
|
331
|
-
hdMod: typeof import("@
|
|
332
|
-
keystoreMod: typeof import("@
|
|
331
|
+
hdMod: typeof import("@midnightntwrk/wallet-sdk-hd");
|
|
332
|
+
keystoreMod: typeof import("@midnightntwrk/wallet-sdk-unshielded-wallet");
|
|
333
333
|
}> {
|
|
334
|
-
const hdMod = await import("@
|
|
334
|
+
const hdMod = await import("@midnightntwrk/wallet-sdk-hd").catch(() => {
|
|
335
335
|
throw new Error(
|
|
336
|
-
"@
|
|
336
|
+
"@midnightntwrk/wallet-sdk-hd is required for WalletMode.MidnightLocal.",
|
|
337
337
|
);
|
|
338
338
|
});
|
|
339
339
|
const keystoreMod = await import(
|
|
340
|
-
"@
|
|
340
|
+
"@midnightntwrk/wallet-sdk-unshielded-wallet"
|
|
341
341
|
).catch(() => {
|
|
342
342
|
throw new Error(
|
|
343
|
-
"@
|
|
343
|
+
"@midnightntwrk/wallet-sdk-unshielded-wallet is required for WalletMode.MidnightLocal.",
|
|
344
344
|
);
|
|
345
345
|
});
|
|
346
346
|
return { hdMod, keystoreMod };
|
|
347
347
|
}
|
|
348
348
|
|
|
349
349
|
function deriveUnshieldedSeed(
|
|
350
|
-
hdMod: typeof import("@
|
|
350
|
+
hdMod: typeof import("@midnightntwrk/wallet-sdk-hd"),
|
|
351
351
|
seedHex: string,
|
|
352
|
-
role: import("@
|
|
352
|
+
role: import("@midnightntwrk/wallet-sdk-hd").Role,
|
|
353
353
|
): Uint8Array {
|
|
354
354
|
const seedBuffer = Buffer.from(seedHex, "hex");
|
|
355
355
|
const hdResult = hdMod.HDWallet.fromSeed(seedBuffer);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { test, expect } from "bun:test";
|
|
2
|
+
import { SolanaLocalConnector } from "./local.ts";
|
|
3
|
+
import { CryptoManager } from "@effectstream/crypto";
|
|
4
|
+
import { AddressType } from "@effectstream/utils";
|
|
5
|
+
|
|
6
|
+
const verifier = () => CryptoManager.getCryptoManager(AddressType.SOLANA);
|
|
7
|
+
|
|
8
|
+
test("SolanaLocal: wallet signs and CryptoManager.Solana verifies (round-trip)", async () => {
|
|
9
|
+
const provider = await SolanaLocalConnector.instance().connectFromSeed();
|
|
10
|
+
const { address, type } = provider.getAddress();
|
|
11
|
+
expect(type).toBe(AddressType.SOLANA);
|
|
12
|
+
|
|
13
|
+
const message = "effectstream login challenge";
|
|
14
|
+
const signature = await provider.signMessage(message);
|
|
15
|
+
|
|
16
|
+
expect(await verifier().verifySignature(address, message, signature)).toBe(
|
|
17
|
+
true,
|
|
18
|
+
);
|
|
19
|
+
expect(
|
|
20
|
+
await verifier().verifySignature(address, "different message", signature),
|
|
21
|
+
).toBe(false);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("SolanaLocal: a given secret key is deterministic", async () => {
|
|
25
|
+
const p1 = await SolanaLocalConnector.instance().connectFromSeed();
|
|
26
|
+
const secretKey = p1.getConnection().api.secretKey;
|
|
27
|
+
const p2 = await SolanaLocalConnector.instance().connectFromSeed({ secretKey });
|
|
28
|
+
expect(p2.getAddress().address).toBe(p1.getAddress().address);
|
|
29
|
+
});
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { AddressType } from "@effectstream/utils";
|
|
2
|
+
import { ed25519 } from "@noble/curves/ed25519";
|
|
3
|
+
import bs58 from "bs58";
|
|
4
|
+
import { Buffer } from "node:buffer";
|
|
5
|
+
import type {
|
|
6
|
+
ActiveConnection,
|
|
7
|
+
AddressAndType,
|
|
8
|
+
IProvider,
|
|
9
|
+
UserSignature,
|
|
10
|
+
} from "../IProvider.ts";
|
|
11
|
+
|
|
12
|
+
export type SolanaLocalConnectArgs = {
|
|
13
|
+
/** Base58-encoded 32-byte Ed25519 secret (seed). Generated when omitted. */
|
|
14
|
+
secretKey?: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* `getConnection().api` shape for a locally-generated Solana keypair wallet.
|
|
19
|
+
* Mirrors the signing subset that real injected Solana wallets expose, so it
|
|
20
|
+
* can stand in for one in tests / headless e2e (cf. CardanoLocal, MidnightLocal).
|
|
21
|
+
*/
|
|
22
|
+
export type SolanaLocalApi = {
|
|
23
|
+
/**
|
|
24
|
+
* Ed25519 sign over the UTF-8 message bytes, returned base64-encoded —
|
|
25
|
+
* matching `CryptoManager.Solana()` and the injected SolanaProvider.
|
|
26
|
+
*/
|
|
27
|
+
signMessage: (message: string) => Promise<UserSignature>;
|
|
28
|
+
/** Base58 public key (the Solana address). */
|
|
29
|
+
address: string;
|
|
30
|
+
/** Base58-encoded secret seed, exposed so tests can persist/reuse the wallet. */
|
|
31
|
+
secretKey: string;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Locally-generated Solana keypair connector. Signs with an in-process Ed25519
|
|
36
|
+
* key (no browser extension), so it can be used in unit tests and the headless
|
|
37
|
+
* wallets-ui e2e the same way CardanoLocal / MidnightLocal are.
|
|
38
|
+
*/
|
|
39
|
+
export class SolanaLocalConnector {
|
|
40
|
+
private provider: SolanaLocalProvider | undefined;
|
|
41
|
+
private static INSTANCE: undefined | SolanaLocalConnector = undefined;
|
|
42
|
+
|
|
43
|
+
static instance(): SolanaLocalConnector {
|
|
44
|
+
if (SolanaLocalConnector.INSTANCE == null) {
|
|
45
|
+
SolanaLocalConnector.INSTANCE = new SolanaLocalConnector();
|
|
46
|
+
}
|
|
47
|
+
return SolanaLocalConnector.INSTANCE;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
connectFromSeed = async (
|
|
51
|
+
args: SolanaLocalConnectArgs = {},
|
|
52
|
+
): Promise<SolanaLocalProvider> => {
|
|
53
|
+
const secret = args.secretKey
|
|
54
|
+
? bs58.decode(args.secretKey)
|
|
55
|
+
: ed25519.utils.randomPrivateKey();
|
|
56
|
+
if (secret.length !== 32) {
|
|
57
|
+
throw new Error("Solana local secret key must be 32 bytes (base58).");
|
|
58
|
+
}
|
|
59
|
+
const address = bs58.encode(ed25519.getPublicKey(secret));
|
|
60
|
+
|
|
61
|
+
const api: SolanaLocalApi = {
|
|
62
|
+
address,
|
|
63
|
+
secretKey: bs58.encode(secret),
|
|
64
|
+
signMessage: (message: string) =>
|
|
65
|
+
Promise.resolve(
|
|
66
|
+
Buffer.from(
|
|
67
|
+
ed25519.sign(new TextEncoder().encode(message), secret),
|
|
68
|
+
).toString("base64"),
|
|
69
|
+
),
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
this.provider = new SolanaLocalProvider(
|
|
73
|
+
{
|
|
74
|
+
metadata: { name: "solana-local", displayName: "Solana (local)" },
|
|
75
|
+
api,
|
|
76
|
+
},
|
|
77
|
+
address,
|
|
78
|
+
);
|
|
79
|
+
return this.provider;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
getProvider(): undefined | SolanaLocalProvider {
|
|
83
|
+
return this.provider;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
getOrThrowProvider(): SolanaLocalProvider {
|
|
87
|
+
if (!this.provider) {
|
|
88
|
+
throw new Error("Solana local wallet not connected");
|
|
89
|
+
}
|
|
90
|
+
return this.provider;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
isConnected(): boolean {
|
|
94
|
+
return this.provider != null;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export class SolanaLocalProvider implements IProvider<SolanaLocalApi> {
|
|
99
|
+
constructor(
|
|
100
|
+
private readonly connection: ActiveConnection<SolanaLocalApi>,
|
|
101
|
+
private readonly address: string,
|
|
102
|
+
) {}
|
|
103
|
+
|
|
104
|
+
getConnection(): ActiveConnection<SolanaLocalApi> {
|
|
105
|
+
return this.connection;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
signMessage(message: string): Promise<UserSignature> {
|
|
109
|
+
return this.connection.api.signMessage(message);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
getAddress(): AddressAndType {
|
|
113
|
+
return { type: AddressType.SOLANA, address: this.address };
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { AddressType } from "@effectstream/utils";
|
|
3
|
+
import {
|
|
4
|
+
Keypair,
|
|
5
|
+
PublicKey,
|
|
6
|
+
SystemProgram,
|
|
7
|
+
Transaction,
|
|
8
|
+
} from "@solana/web3.js";
|
|
9
|
+
import { SolanaProvider, type SolanaApi } from "./solana.ts";
|
|
10
|
+
import type { ActiveConnection } from "../IProvider.ts";
|
|
11
|
+
|
|
12
|
+
const ADDRESS = "5FHwkrdxntdK24hgQU8qgBjn35Y1zwhz1GZwCkP2UJnM";
|
|
13
|
+
|
|
14
|
+
/** Deterministic signer so assertions can look for its signature. */
|
|
15
|
+
const USER = Keypair.fromSeed(new Uint8Array(32).fill(5));
|
|
16
|
+
const SPONSOR = Keypair.fromSeed(new Uint8Array(32).fill(6));
|
|
17
|
+
const BLOCKHASH = Keypair.generate().publicKey.toBase58();
|
|
18
|
+
|
|
19
|
+
/** A real, unsigned legacy transaction in the shape the batcher expects. */
|
|
20
|
+
function buildUnsignedTx(): { tx: Transaction; base64: string } {
|
|
21
|
+
const tx = new Transaction();
|
|
22
|
+
tx.feePayer = SPONSOR.publicKey;
|
|
23
|
+
tx.recentBlockhash = BLOCKHASH;
|
|
24
|
+
tx.add(SystemProgram.transfer({
|
|
25
|
+
fromPubkey: USER.publicKey,
|
|
26
|
+
toPubkey: new PublicKey(ADDRESS),
|
|
27
|
+
lamports: 1,
|
|
28
|
+
}));
|
|
29
|
+
return {
|
|
30
|
+
tx,
|
|
31
|
+
base64: tx.serialize({ requireAllSignatures: false }).toString("base64"),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function mockConnection(
|
|
36
|
+
overrides: Partial<SolanaApi> = {},
|
|
37
|
+
): ActiveConnection<SolanaApi> {
|
|
38
|
+
return {
|
|
39
|
+
metadata: { name: "mock", displayName: "Mock" },
|
|
40
|
+
api: {
|
|
41
|
+
publicKey: { toBase58: () => ADDRESS },
|
|
42
|
+
signMessage: async (_m: Uint8Array) => ({ signature: new Uint8Array() }),
|
|
43
|
+
connect: async () => {},
|
|
44
|
+
disconnect: async () => {},
|
|
45
|
+
...overrides,
|
|
46
|
+
} as SolanaApi,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
describe("SolanaProvider", () => {
|
|
51
|
+
test("getAddress returns the SOLANA address type", () => {
|
|
52
|
+
const provider = new SolanaProvider(mockConnection(), ADDRESS);
|
|
53
|
+
const { type, address } = provider.getAddress();
|
|
54
|
+
expect(type).toBe(AddressType.SOLANA);
|
|
55
|
+
expect(address).toBe(ADDRESS);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("signTransaction hands the wallet a Transaction OBJECT, not raw bytes", async () => {
|
|
59
|
+
// The regression this guards: passing raw bytes works for the Wallet
|
|
60
|
+
// Standard bridge but breaks every injected wallet (Phantom, Backpack,
|
|
61
|
+
// Solflare), which expect a web3.js Transaction and return a signed one.
|
|
62
|
+
// The previous mock echoed bytes back, so it could not distinguish the two.
|
|
63
|
+
const { tx, base64 } = buildUnsignedTx();
|
|
64
|
+
|
|
65
|
+
let received: unknown = null;
|
|
66
|
+
const conn = mockConnection({
|
|
67
|
+
signTransaction: async (t: unknown) => {
|
|
68
|
+
received = t;
|
|
69
|
+
(t as Transaction).partialSign(USER);
|
|
70
|
+
return t;
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const signed = await new SolanaProvider(conn, ADDRESS)
|
|
75
|
+
.signTransaction(base64);
|
|
76
|
+
|
|
77
|
+
expect(received).not.toBeInstanceOf(Uint8Array);
|
|
78
|
+
expect(typeof (received as any)?.serialize).toBe("function");
|
|
79
|
+
expect((received as Transaction).instructions.length)
|
|
80
|
+
.toBe(tx.instructions.length);
|
|
81
|
+
|
|
82
|
+
// The result must be base64 that SolanaAdapter.deserialize() can parse,
|
|
83
|
+
// carrying the signature the wallet just added.
|
|
84
|
+
const parsed = Transaction.from(Buffer.from(signed, "base64"));
|
|
85
|
+
expect(parsed.signatures.some(
|
|
86
|
+
(s) => s.publicKey.equals(USER.publicKey) && s.signature !== null,
|
|
87
|
+
)).toBe(true);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("signTransaction also accepts a wallet that returns raw bytes", async () => {
|
|
91
|
+
// The Wallet Standard bridge returns serialized bytes rather than an object.
|
|
92
|
+
const { base64 } = buildUnsignedTx();
|
|
93
|
+
const conn = mockConnection({
|
|
94
|
+
signTransaction: async (t: unknown) => {
|
|
95
|
+
const signedTx = t as Transaction;
|
|
96
|
+
signedTx.partialSign(USER);
|
|
97
|
+
return new Uint8Array(
|
|
98
|
+
signedTx.serialize({ requireAllSignatures: false }),
|
|
99
|
+
);
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const signed = await new SolanaProvider(conn, ADDRESS)
|
|
104
|
+
.signTransaction(base64);
|
|
105
|
+
expect(() => Transaction.from(Buffer.from(signed, "base64"))).not.toThrow();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("signTransaction rejects an unrecognised wallet result", async () => {
|
|
109
|
+
const { base64 } = buildUnsignedTx();
|
|
110
|
+
const conn = mockConnection({
|
|
111
|
+
signTransaction: async () => "not-a-transaction" as unknown,
|
|
112
|
+
});
|
|
113
|
+
await expect(
|
|
114
|
+
new SolanaProvider(conn, ADDRESS).signTransaction(base64),
|
|
115
|
+
).rejects.toThrow(/unrecognised signTransaction result/);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("signTransaction throws when the wallet lacks signTransaction", async () => {
|
|
119
|
+
const conn = mockConnection({ signTransaction: undefined });
|
|
120
|
+
const provider = new SolanaProvider(conn, ADDRESS);
|
|
121
|
+
await expect(provider.signTransaction("abc")).rejects.toThrow(
|
|
122
|
+
/does not support signTransaction/,
|
|
123
|
+
);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("signMessage returns a base64-encoded signature", async () => {
|
|
127
|
+
const sigBytes = new Uint8Array([10, 20, 30, 40]);
|
|
128
|
+
const conn = mockConnection({
|
|
129
|
+
signMessage: async (_m: Uint8Array) => ({ signature: sigBytes }),
|
|
130
|
+
});
|
|
131
|
+
const provider = new SolanaProvider(conn, ADDRESS);
|
|
132
|
+
const sig = await provider.signMessage("hello");
|
|
133
|
+
expect(sig).toBe(Buffer.from(sigBytes).toString("base64"));
|
|
134
|
+
});
|
|
135
|
+
});
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import { AddressType } from "@effectstream/utils";
|
|
2
|
+
import {
|
|
3
|
+
type IConnector,
|
|
4
|
+
type IInjectedConnector,
|
|
5
|
+
type ConnectionOption,
|
|
6
|
+
optionToActive,
|
|
7
|
+
type ActiveConnection,
|
|
8
|
+
type IProvider,
|
|
9
|
+
type AddressAndType,
|
|
10
|
+
type UserSignature,
|
|
11
|
+
} from "../IProvider.ts";
|
|
12
|
+
import { getWindow } from "../windows.ts";
|
|
13
|
+
import { getWallets } from "@wallet-standard/app";
|
|
14
|
+
import { Transaction } from "@solana/web3.js";
|
|
15
|
+
import bs58 from "bs58";
|
|
16
|
+
|
|
17
|
+
// Wallet Standard feature names. Wallets like MetaMask expose Solana support
|
|
18
|
+
// through this registry rather than a legacy injected `window.solana` global.
|
|
19
|
+
const STANDARD_CONNECT = "standard:connect";
|
|
20
|
+
const STANDARD_DISCONNECT = "standard:disconnect";
|
|
21
|
+
const SOLANA_SIGN_MESSAGE = "solana:signMessage";
|
|
22
|
+
const SOLANA_SIGN_TRANSACTION = "solana:signTransaction";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Solana wallet API interface.
|
|
26
|
+
* Matches the Solana wallet standard (Phantom, Backpack, Solflare, etc.)
|
|
27
|
+
*/
|
|
28
|
+
export interface SolanaWalletApi {
|
|
29
|
+
publicKey: { toBase58(): string };
|
|
30
|
+
signMessage(message: Uint8Array): Promise<{ signature: Uint8Array }>;
|
|
31
|
+
signTransaction?(tx: unknown): Promise<unknown>;
|
|
32
|
+
sendTransaction?(
|
|
33
|
+
tx: unknown,
|
|
34
|
+
connection: unknown,
|
|
35
|
+
): Promise<string>;
|
|
36
|
+
isPhantom?: boolean;
|
|
37
|
+
isConnected?: boolean;
|
|
38
|
+
connect(): Promise<void>;
|
|
39
|
+
disconnect(): Promise<void>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export type SolanaApi = SolanaWalletApi;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Solana wallet connector.
|
|
46
|
+
* Supports Phantom, Backpack, Solflare, and any wallet that implements
|
|
47
|
+
* the Solana wallet standard (window.solana or window.phantom).
|
|
48
|
+
*/
|
|
49
|
+
export class SolanaConnector
|
|
50
|
+
implements IConnector<SolanaApi>, IInjectedConnector<SolanaApi>
|
|
51
|
+
{
|
|
52
|
+
private provider: SolanaProvider | undefined;
|
|
53
|
+
private static INSTANCE: undefined | SolanaConnector = undefined;
|
|
54
|
+
|
|
55
|
+
static getWalletOptions(): ConnectionOption<SolanaApi>[] {
|
|
56
|
+
const win = getWindow() as any;
|
|
57
|
+
const wallets: ConnectionOption<SolanaApi>[] = [];
|
|
58
|
+
|
|
59
|
+
// Check for Phantom
|
|
60
|
+
if (win?.phantom?.solana) {
|
|
61
|
+
wallets.push({
|
|
62
|
+
metadata: {
|
|
63
|
+
name: "phantom",
|
|
64
|
+
displayName: "Phantom",
|
|
65
|
+
icon: win.phantom?.solana?.icon,
|
|
66
|
+
},
|
|
67
|
+
api: async () => win.phantom.solana as SolanaWalletApi,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Check for Backpack
|
|
72
|
+
if (win?.backpack?.solana) {
|
|
73
|
+
wallets.push({
|
|
74
|
+
metadata: {
|
|
75
|
+
name: "backpack",
|
|
76
|
+
displayName: "Backpack",
|
|
77
|
+
},
|
|
78
|
+
api: async () => win.backpack.solana as SolanaWalletApi,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Check for Solflare
|
|
83
|
+
if (win?.solflare?.isSolflare) {
|
|
84
|
+
wallets.push({
|
|
85
|
+
metadata: {
|
|
86
|
+
name: "solflare",
|
|
87
|
+
displayName: "Solflare",
|
|
88
|
+
},
|
|
89
|
+
api: async () => win.solflare as SolanaWalletApi,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Generic Solana wallet standard
|
|
94
|
+
if (
|
|
95
|
+
win?.solana &&
|
|
96
|
+
!win.solana.isPhantom &&
|
|
97
|
+
wallets.length === 0
|
|
98
|
+
) {
|
|
99
|
+
wallets.push({
|
|
100
|
+
metadata: {
|
|
101
|
+
name: "solana",
|
|
102
|
+
displayName: "Solana Wallet",
|
|
103
|
+
},
|
|
104
|
+
api: async () => win.solana as SolanaWalletApi,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Wallet Standard wallets ( MetaMask Solana, plus Phantom/Solflare/etc.
|
|
109
|
+
// that register this way ). De-duped against the injected entries above by
|
|
110
|
+
// display name so the same wallet isn't listed twice.
|
|
111
|
+
try {
|
|
112
|
+
for (const w of getWallets().get()) {
|
|
113
|
+
if (!isSolanaStandardWallet(w)) continue;
|
|
114
|
+
const name = (w.name ?? "").toLowerCase();
|
|
115
|
+
const already = wallets.some(
|
|
116
|
+
(o) => o.metadata.displayName.toLowerCase() === name,
|
|
117
|
+
);
|
|
118
|
+
if (already) continue;
|
|
119
|
+
wallets.push({
|
|
120
|
+
metadata: {
|
|
121
|
+
name: `standard:${name}`,
|
|
122
|
+
displayName: w.name,
|
|
123
|
+
icon: w.icon,
|
|
124
|
+
},
|
|
125
|
+
api: async () =>
|
|
126
|
+
new WalletStandardSolanaApi(w) as unknown as SolanaWalletApi,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
} catch {
|
|
130
|
+
// Registry unavailable ( e.g. non-browser ) — skip.
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return wallets;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
static instance(): SolanaConnector {
|
|
137
|
+
if (SolanaConnector.INSTANCE == null) {
|
|
138
|
+
const newInstance = new SolanaConnector();
|
|
139
|
+
SolanaConnector.INSTANCE = newInstance;
|
|
140
|
+
}
|
|
141
|
+
return SolanaConnector.INSTANCE;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
connectSimple = async (): Promise<SolanaProvider> => {
|
|
145
|
+
const options = SolanaConnector.getWalletOptions();
|
|
146
|
+
if (options.length === 0) {
|
|
147
|
+
throw new Error(
|
|
148
|
+
"No Solana wallet found. Please install a Solana wallet (Phantom, Backpack, or MetaMask).",
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
return this.connectNamed(options[0].metadata.name);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
connectNamed = async (name: string): Promise<SolanaProvider> => {
|
|
155
|
+
const options = SolanaConnector.getWalletOptions();
|
|
156
|
+
const option = options.find((o) => o.metadata.name === name);
|
|
157
|
+
if (!option) {
|
|
158
|
+
throw new Error(`Wallet "${name}" not found`);
|
|
159
|
+
}
|
|
160
|
+
const active = await optionToActive(option);
|
|
161
|
+
return this.connectExternal(active);
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
connectExternal = async (
|
|
165
|
+
conn: ActiveConnection<SolanaApi>,
|
|
166
|
+
): Promise<SolanaProvider> => {
|
|
167
|
+
const api = conn.api;
|
|
168
|
+
|
|
169
|
+
if (api.isConnected === false) {
|
|
170
|
+
await api.connect();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const address = api.publicKey.toBase58();
|
|
174
|
+
this.provider = new SolanaProvider(conn, address);
|
|
175
|
+
return this.provider;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
getProvider(): undefined | SolanaProvider {
|
|
179
|
+
return this.provider;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
getOrThrowProvider(): SolanaProvider {
|
|
183
|
+
if (!this.provider) {
|
|
184
|
+
throw new Error("Solana wallet not connected");
|
|
185
|
+
}
|
|
186
|
+
return this.provider;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
isConnected(): boolean {
|
|
190
|
+
return this.provider != null;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Solana wallet provider implementing IProvider.
|
|
196
|
+
*/
|
|
197
|
+
export class SolanaProvider implements IProvider<SolanaApi> {
|
|
198
|
+
constructor(
|
|
199
|
+
private readonly connection: ActiveConnection<SolanaApi>,
|
|
200
|
+
private readonly address: string,
|
|
201
|
+
) {}
|
|
202
|
+
|
|
203
|
+
getConnection(): ActiveConnection<SolanaApi> {
|
|
204
|
+
return this.connection;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async signMessage(message: string): Promise<UserSignature> {
|
|
208
|
+
const encoded = new TextEncoder().encode(message);
|
|
209
|
+
const { signature } = await this.connection.api.signMessage(encoded);
|
|
210
|
+
// Return base64-encoded signature
|
|
211
|
+
return Buffer.from(signature).toString("base64");
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
getAddress(): AddressAndType {
|
|
215
|
+
return {
|
|
216
|
+
type: AddressType.SOLANA,
|
|
217
|
+
address: this.address,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Sign a partial transaction for the batcher (fee-payer sponsor flow). The
|
|
223
|
+
* transaction's fee payer should already be the batcher's sponsor; the batcher
|
|
224
|
+
* adds that signature and submits.
|
|
225
|
+
*
|
|
226
|
+
* **base64 on both sides**, matching `SolanaBatchPayload.transactions` in
|
|
227
|
+
* `@effectstream/batcher-sdk` — the adapter deserialises with
|
|
228
|
+
* `Transaction.from(Buffer.from(tx, "base64"))`, and both the e2e suite and
|
|
229
|
+
* the solana-starter frontend post base64. This used to be base58 while
|
|
230
|
+
* claiming in its own docblock to match the batcher contract, which it did
|
|
231
|
+
* not: the adapter would have decoded garbage and rejected every transaction.
|
|
232
|
+
*/
|
|
233
|
+
async signTransaction(txBase64: string): Promise<string> {
|
|
234
|
+
if (!this.connection.api.signTransaction) {
|
|
235
|
+
throw new Error("Wallet does not support signTransaction");
|
|
236
|
+
}
|
|
237
|
+
// Pass a web3.js `Transaction`, not raw bytes. The injected wallets this
|
|
238
|
+
// connector lists first (Phantom, Backpack, Solflare) take a Transaction
|
|
239
|
+
// object and hand back a signed Transaction — raw bytes make them throw or
|
|
240
|
+
// return garbage. `WalletStandardSolanaApi` below already accepts either and
|
|
241
|
+
// branches on `.serialize`, so the object form works for both paths.
|
|
242
|
+
//
|
|
243
|
+
// Legacy transactions only, matching what SolanaAdapter accepts
|
|
244
|
+
// (`Transaction.from`); a versioned tx throws here and is rejected there.
|
|
245
|
+
const tx = Transaction.from(Buffer.from(txBase64, "base64"));
|
|
246
|
+
const signed = await this.connection.api.signTransaction(tx);
|
|
247
|
+
return serializeSignedTx(signed);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Normalise whatever a wallet returns from `signTransaction` to base64 — the
|
|
253
|
+
* encoding `SolanaBatchPayload.transactions` expects. Injected wallets return a
|
|
254
|
+
* Transaction-like object; the Wallet Standard bridge returns raw bytes.
|
|
255
|
+
* `requireAllSignatures: false` because the fee payer (the batcher's sponsor)
|
|
256
|
+
* signs later.
|
|
257
|
+
*/
|
|
258
|
+
function serializeSignedTx(signed: unknown): string {
|
|
259
|
+
if (signed != null && typeof (signed as any).serialize === "function") {
|
|
260
|
+
const bytes: Uint8Array = (signed as any).serialize({
|
|
261
|
+
requireAllSignatures: false,
|
|
262
|
+
verifySignatures: false,
|
|
263
|
+
});
|
|
264
|
+
return Buffer.from(bytes).toString("base64");
|
|
265
|
+
}
|
|
266
|
+
if (signed instanceof Uint8Array) {
|
|
267
|
+
return Buffer.from(signed).toString("base64");
|
|
268
|
+
}
|
|
269
|
+
throw new Error(
|
|
270
|
+
"Wallet returned an unrecognised signTransaction result (expected a Transaction or bytes)",
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** True if a Wallet Standard wallet can connect + sign Solana messages. */
|
|
275
|
+
function isSolanaStandardWallet(wallet: any): boolean {
|
|
276
|
+
const features = wallet?.features ?? {};
|
|
277
|
+
return Boolean(features[STANDARD_CONNECT] && features[SOLANA_SIGN_MESSAGE]);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function pickSolanaChain(account: any, wallet: any): string | undefined {
|
|
281
|
+
const chains: string[] = account?.chains ?? wallet?.chains ?? [];
|
|
282
|
+
return chains.find((c) => typeof c === "string" && c.startsWith("solana:"));
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Adapts a Wallet Standard wallet ( e.g. MetaMask's Solana account ) to the
|
|
287
|
+
* `SolanaWalletApi` shape the rest of the connector expects. The Wallet
|
|
288
|
+
* Standard works in serialized bytes, so this bridges to the byte/object
|
|
289
|
+
* contracts the injected ( Phantom-style ) wallets use.
|
|
290
|
+
*/
|
|
291
|
+
class WalletStandardSolanaApi implements SolanaWalletApi {
|
|
292
|
+
isConnected = false;
|
|
293
|
+
private account: any | undefined;
|
|
294
|
+
|
|
295
|
+
constructor(private readonly wallet: any) {}
|
|
296
|
+
|
|
297
|
+
get publicKey(): { toBase58(): string } {
|
|
298
|
+
const address = this.account?.address ?? "";
|
|
299
|
+
return { toBase58: () => address };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
async connect(): Promise<void> {
|
|
303
|
+
if (!this.account) {
|
|
304
|
+
const result = await this.wallet.features[STANDARD_CONNECT].connect();
|
|
305
|
+
this.account = (result?.accounts ?? this.wallet.accounts ?? [])[0];
|
|
306
|
+
}
|
|
307
|
+
if (!this.account) {
|
|
308
|
+
throw new Error("Wallet returned no Solana account");
|
|
309
|
+
}
|
|
310
|
+
this.isConnected = true;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
async disconnect(): Promise<void> {
|
|
314
|
+
const feature = this.wallet.features[STANDARD_DISCONNECT];
|
|
315
|
+
if (feature?.disconnect) {
|
|
316
|
+
await feature.disconnect();
|
|
317
|
+
}
|
|
318
|
+
this.account = undefined;
|
|
319
|
+
this.isConnected = false;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
async signMessage(message: Uint8Array): Promise<{ signature: Uint8Array }> {
|
|
323
|
+
const feature = this.wallet.features[SOLANA_SIGN_MESSAGE];
|
|
324
|
+
if (!feature) {
|
|
325
|
+
throw new Error("Wallet does not support solana:signMessage");
|
|
326
|
+
}
|
|
327
|
+
const [output] = await feature.signMessage({
|
|
328
|
+
account: this.account,
|
|
329
|
+
message,
|
|
330
|
+
});
|
|
331
|
+
return { signature: output.signature };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Two callers: the frontend passes a web3.js Transaction ( has `.serialize` )
|
|
335
|
+
// and calls `.serialize()` on the result; SolanaProvider.signTransaction
|
|
336
|
+
// passes raw bytes and expects bytes back. Handle both.
|
|
337
|
+
async signTransaction(tx: unknown): Promise<unknown> {
|
|
338
|
+
const feature = this.wallet.features[SOLANA_SIGN_TRANSACTION];
|
|
339
|
+
if (!feature) {
|
|
340
|
+
throw new Error(
|
|
341
|
+
"Wallet does not support solana:signTransaction. The batcher flow " +
|
|
342
|
+
"needs a sign-only signature, which this wallet does not provide.",
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
const asObject =
|
|
346
|
+
tx != null && typeof (tx as any).serialize === "function";
|
|
347
|
+
const bytes: Uint8Array = asObject
|
|
348
|
+
? (tx as any).serialize({
|
|
349
|
+
requireAllSignatures: false,
|
|
350
|
+
verifySignatures: false,
|
|
351
|
+
})
|
|
352
|
+
: (tx as Uint8Array);
|
|
353
|
+
|
|
354
|
+
const chain = pickSolanaChain(this.account, this.wallet);
|
|
355
|
+
const [output] = await feature.signTransaction({
|
|
356
|
+
account: this.account,
|
|
357
|
+
transaction: Uint8Array.from(bytes),
|
|
358
|
+
...(chain ? { chain } : {}),
|
|
359
|
+
});
|
|
360
|
+
const signed: Uint8Array = output.signedTransaction;
|
|
361
|
+
|
|
362
|
+
// Return a Transaction-like object for the frontend ( only `.serialize` is
|
|
363
|
+
// used ), or raw bytes for the SolanaProvider byte path.
|
|
364
|
+
return asObject ? { serialize: () => signed } : signed;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Result } from "@effectstream/utils";
|
|
2
|
+
import type { IProvider } from "../IProvider.ts";
|
|
3
|
+
import type { WalletMode, ApiForMode } from "../utils.ts";
|
|
4
|
+
import { type LoginInfoMap, connectInjected } from "../wallet-modes.ts";
|
|
5
|
+
import { SolanaConnector } from "./solana.ts";
|
|
6
|
+
|
|
7
|
+
export async function solanaLoginWrapper(
|
|
8
|
+
loginInfo: LoginInfoMap[WalletMode.Solana],
|
|
9
|
+
): Promise<Result<IProvider<ApiForMode<WalletMode.Solana>>>> {
|
|
10
|
+
const loginResult = await connectInjected(
|
|
11
|
+
"solanaLoginWrapper",
|
|
12
|
+
loginInfo,
|
|
13
|
+
SolanaConnector.instance(),
|
|
14
|
+
);
|
|
15
|
+
if (loginResult.success === false) {
|
|
16
|
+
return loginResult;
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
success: true,
|
|
20
|
+
result: loginResult.result,
|
|
21
|
+
};
|
|
22
|
+
}
|
package/src/utils.ts
CHANGED
|
@@ -16,6 +16,7 @@ import { MinaConnector } from "./mina/mina.ts";
|
|
|
16
16
|
import { AvailConnector } from "./avail/avail.ts";
|
|
17
17
|
import { MidnightConnector } from "./midnight/midnight.ts";
|
|
18
18
|
import { MidnightLocalConnector } from "./midnight/local.ts";
|
|
19
|
+
import { SolanaConnector } from "./solana/solana.ts";
|
|
19
20
|
|
|
20
21
|
export const enum WalletMode {
|
|
21
22
|
EvmInjected,
|
|
@@ -29,6 +30,7 @@ export const enum WalletMode {
|
|
|
29
30
|
Algorand,
|
|
30
31
|
Mina,
|
|
31
32
|
AvailJs,
|
|
33
|
+
Solana,
|
|
32
34
|
}
|
|
33
35
|
|
|
34
36
|
export const WalletNameMap: Record<WalletMode, string> = {
|
|
@@ -43,6 +45,7 @@ export const WalletNameMap: Record<WalletMode, string> = {
|
|
|
43
45
|
[WalletMode.Algorand]: 'Algorand',
|
|
44
46
|
[WalletMode.Mina]: "Mina",
|
|
45
47
|
[WalletMode.AvailJs]: "Avail",
|
|
48
|
+
[WalletMode.Solana]: "Solana",
|
|
46
49
|
};
|
|
47
50
|
|
|
48
51
|
export const WalletModeMap = {
|
|
@@ -57,6 +60,7 @@ export const WalletModeMap = {
|
|
|
57
60
|
[WalletMode.Algorand]: AlgorandConnector.instance(),
|
|
58
61
|
[WalletMode.Mina]: MinaConnector.instance(),
|
|
59
62
|
[WalletMode.AvailJs]: AvailConnector.instance(),
|
|
63
|
+
[WalletMode.Solana]: SolanaConnector.instance(),
|
|
60
64
|
};
|
|
61
65
|
|
|
62
66
|
type ExtractGeneric<T> = T extends IConnector<infer U> ? U : never;
|
package/src/wallet-modes.ts
CHANGED
|
@@ -23,6 +23,7 @@ import { AddressType } from "@effectstream/utils";
|
|
|
23
23
|
import { formatError } from "./helpers/format-error.ts";
|
|
24
24
|
import type { MidnightApi } from "./midnight/midnight.ts";
|
|
25
25
|
import type {} from "./midnight/local.ts";
|
|
26
|
+
import type { SolanaApi } from "./solana/solana.ts";
|
|
26
27
|
import type { Chain } from "viem/chains";
|
|
27
28
|
import { Wallet } from "ethers";
|
|
28
29
|
|
|
@@ -83,6 +84,7 @@ export type LoginInfoMap = {
|
|
|
83
84
|
/** Midnight network id ("undeployed" | "testnet" | …) — sets the bech32 prefix. */
|
|
84
85
|
networkId: string;
|
|
85
86
|
};
|
|
87
|
+
[WalletMode.Solana]: BaseLoginInfo<SolanaApi>;
|
|
86
88
|
};
|
|
87
89
|
|
|
88
90
|
type ToUnion<T> = {
|
|
@@ -148,8 +150,8 @@ export function getAddressType(mode: WalletMode): AddressType {
|
|
|
148
150
|
return AddressType.MIDNIGHT;
|
|
149
151
|
case WalletMode.AvailJs:
|
|
150
152
|
return AddressType.AVAIL;
|
|
151
|
-
case WalletMode.
|
|
152
|
-
return AddressType.
|
|
153
|
+
case WalletMode.Solana:
|
|
154
|
+
return AddressType.SOLANA;
|
|
153
155
|
default:
|
|
154
156
|
throw new Error(`Unsupported wallet mode: ${mode}`);
|
|
155
157
|
}
|
package/src/wallets.ts
CHANGED
|
@@ -15,6 +15,7 @@ import type { LoginInfo } from "./wallet-modes.ts";
|
|
|
15
15
|
// import { assertNever } from "assert-never";
|
|
16
16
|
import { midnightLoginWrapper } from "./midnight/wrapper.ts";
|
|
17
17
|
import { midnightLocalLoginWrapper } from "./midnight/wrapper-local.ts";
|
|
18
|
+
import { solanaLoginWrapper } from "./solana/wrapper.ts";
|
|
18
19
|
|
|
19
20
|
export async function walletLogin(
|
|
20
21
|
loginInfo: LoginInfo
|
|
@@ -68,6 +69,9 @@ async function login(loginInfo: LoginInfo): Promise<Result<IProvider<unknown>>>
|
|
|
68
69
|
case WalletMode.MidnightLocal: {
|
|
69
70
|
return await midnightLocalLoginWrapper(loginInfo);
|
|
70
71
|
}
|
|
72
|
+
case WalletMode.Solana: {
|
|
73
|
+
return await solanaLoginWrapper(loginInfo);
|
|
74
|
+
}
|
|
71
75
|
default:
|
|
72
76
|
throw new Error(`Unsupported wallet mode: ${(loginInfo as any).mode}`);
|
|
73
77
|
}
|