@opeqlabs/sdk 0.0.0-stage → 0.1.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 +191 -0
- package/README.md +123 -2
- package/examples/evm-send.mjs +27 -0
- package/examples/solana-send.mjs +34 -0
- package/examples/verify.mjs +29 -0
- package/filestore.js +149 -0
- package/index.js +384 -0
- package/locks.js +28 -0
- package/package.json +17 -4
- package/src/feekey.js +36 -0
- package/src/lib/compute.js +73 -0
- package/src/lib/engine.js +639 -0
- package/src/lib/evmcode.js +4 -0
- package/src/lib/evmtx.js +62 -0
- package/src/lib/evmwallet.js +636 -0
- package/src/lib/evmwots.js +322 -0
- package/src/lib/keccak.js +165 -0
- package/src/lib/mnemonic.js +80 -0
- package/src/lib/protocol.js +129 -0
- package/src/lib/secp256k1.js +123 -0
- package/src/lib/sha256.js +120 -0
- package/src/lib/solana.js +264 -0
- package/src/lib/vaultstore.js +133 -0
- package/src/lib/wordlist.js +1 -0
- package/src/lib/wots.js +128 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
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
|
+
Copyright 2026 Sercan0x
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,124 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @opeqlabs/sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Quantum-resistant accounts for Solana, Ethereum, Base, Arbitrum and Robinhood Chain.
|
|
4
|
+
|
|
5
|
+
An OPEQ account is controlled by hash-based one-time keys (WOTS+ in a Merkle tree of 1024 keys) instead of an elliptic-curve key. A quantum computer that breaks Ed25519 or secp256k1 does not break it. This SDK lets you create those accounts, sign with them and verify their signatures from your own code.
|
|
6
|
+
|
|
7
|
+
It is the same code the OPEQ wallet runs. `src/` is copied byte for byte from the wallet's library by `build_sdk.py`, and the test suite fails if the two ever differ. It has no dependencies.
|
|
8
|
+
|
|
9
|
+
## Status
|
|
10
|
+
|
|
11
|
+
**Beta, not yet externally audited.** The audits planned for the wallet will cover this SDK too. Until then, use small amounts.
|
|
12
|
+
|
|
13
|
+
**Upgrade authority.** The Solana program (`HL598vrersxS5AUUV6NyzHgpSyK31xAmqMsqUhxzeKm8`) can still be upgraded by the OPEQ team. The plan is to give that up after the audits. The program cannot move funds on its own: every transfer needs a one-time signature from the account's own key tree. The EVM account and factory contracts are not upgradeable.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
npm install @opeqlabs/sdk
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Node.js 20 or later, or any current browser. In Node, use `FileStore`; in a browser, `IdbStore`. Both refuse to move a key counter backwards.
|
|
22
|
+
|
|
23
|
+
## The one rule
|
|
24
|
+
|
|
25
|
+
**A one-time key must never sign twice.** The SDK enforces this for you:
|
|
26
|
+
|
|
27
|
+
- Every store refuses to move a key counter backwards.
|
|
28
|
+
- `FileStore` locks its folder, so two processes cannot use the same wallet at once. The folder must be on a local disk, owned by you and closed to other users (`chmod 700`); a shared, network or synced folder defeats the lock.
|
|
29
|
+
- Signing jobs inside one process run one at a time.
|
|
30
|
+
|
|
31
|
+
What the SDK cannot see is a second copy somewhere else. If the same 24 words are used on another device, that device must call `useHere()` first. It moves the account to a fresh key tree, and the old copy stops signing. Never restore a store from a backup to continue signing; recover from the words and call `useHere()` instead.
|
|
32
|
+
|
|
33
|
+
## Solana
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
import { Vault } from "@opeqlabs/sdk";
|
|
37
|
+
import { FileStore } from "@opeqlabs/sdk/filestore";
|
|
38
|
+
|
|
39
|
+
const store = await FileStore.open("./opeq-wallet");
|
|
40
|
+
const vault = new Vault({ rpcUrl: "https://your-solana-rpc", checkRpcUrl: "https://a-second-provider", network: "mainnet", store });
|
|
41
|
+
|
|
42
|
+
const { id, words } = await vault.create(password);
|
|
43
|
+
const acct = await vault.open(id, password);
|
|
44
|
+
|
|
45
|
+
await acct.feeKey();
|
|
46
|
+
await acct.activate();
|
|
47
|
+
await acct.send(to, 1_000_000n);
|
|
48
|
+
await acct.execute([instruction]);
|
|
49
|
+
await acct.rekey();
|
|
50
|
+
|
|
51
|
+
acct.close();
|
|
52
|
+
await store.close();
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
- `acct.address` is the quantum account. Keep funds there.
|
|
56
|
+
- `await acct.feeKey()` returns the fee key's address. It only pays Solana network fees, signs with Ed25519 and is not quantum-safe, so keep only a little SOL on it.
|
|
57
|
+
- `activate()` writes the account's key root on chain once. The fee key pays for it.
|
|
58
|
+
- `checkRpcUrl` is required on mainnet and must be another provider. Before the wallet ever reuses a key slot after a failure, both RPCs must agree the old transaction can no longer land.
|
|
59
|
+
- `execute()` takes web3.js-style instructions (`{ programId, keys, data }`) or `[program, [[key, signer, writable]], data]`. The account signs as the wallet address; no other key may be marked as a signer. Data must be a `Uint8Array`/`Buffer`, a hex string or an array of bytes 0-255; anything else is refused rather than guessed.
|
|
60
|
+
- Amounts are whole numbers in base units (lamports, wei) as bigint, integer or digit string.
|
|
61
|
+
- `status()` reports how many one-time keys are left. Call `rekey()` before they run out; it moves the account to a new tree.
|
|
62
|
+
- `recover(words, password)` brings an existing account into a new store. Then call `useHere()` (see the rule above).
|
|
63
|
+
|
|
64
|
+
## EVM
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
const acct = await vault.open(id, password);
|
|
68
|
+
const base = await acct.evm(8453, { rpcUrl: "https://your-base-rpc", checkUrl: "https://a-second-base-rpc" });
|
|
69
|
+
|
|
70
|
+
console.log(base.address, base.feeAddress);
|
|
71
|
+
await base.setup();
|
|
72
|
+
await base.send(to, 10n ** 16n);
|
|
73
|
+
await base.execute([{ to: token, value: 0n, data: "0xa9059cbb..." }]);
|
|
74
|
+
|
|
75
|
+
acct.close();
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
- Chains: 1 (Ethereum), 8453 (Base), 42161 (Arbitrum), 4663 (Robinhood Chain), 99901 (Elysium, gas in HYPE).
|
|
79
|
+
- `checkUrl` must be a second, independent RPC. Before signing, the SDK checks the account state on both and refuses if they disagree.
|
|
80
|
+
- The same words give the same account address on every chain.
|
|
81
|
+
- The fee key (`feeAddress`) pays gas. It is an ordinary secp256k1 key, so keep only gas money on it.
|
|
82
|
+
- `setup()` deploys the account contract and moves it to a key tree only this device knows. It needs gas on the fee key.
|
|
83
|
+
|
|
84
|
+
## Verify without trusting anyone
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
import { verifySolanaInstruction, verifyEvmExecute } from "@opeqlabs/sdk";
|
|
88
|
+
|
|
89
|
+
verifySolanaInstruction(data, accountKeys, { pubSeed, root, aid, epoch });
|
|
90
|
+
verifyEvmExecute(calldata, { chainId, account, epoch, pubSeed, root });
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Both return the one-time key index, or `null` if the signature does not match or the input is malformed. They make no network calls. `verifyEvmExecute` only accepts calldata in the exact form the account contract decodes. `verifyWots` checks every length (16-byte digest, full signature and path, leaf inside the tree) before verifying. `examples/verify.mjs` checks a landed Solana transaction against the account's key root on chain.
|
|
94
|
+
|
|
95
|
+
## Speed
|
|
96
|
+
|
|
97
|
+
Building a key tree takes about 2 seconds on Solana and about 10 seconds on EVM in Node on a laptop. Signing takes under a second. EVM `setup()` builds three trees, so it takes about a minute. In a browser, pass `new Vault({ compute })` with a `run(op, args)` that hands the job to a Web Worker running `OPS` from `@opeqlabs/sdk/compute`, so the page does not freeze.
|
|
98
|
+
|
|
99
|
+
## RPC
|
|
100
|
+
|
|
101
|
+
Bring your own, two per network from different providers, over https. `rpc.opeq.io` only serves the OPEQ apps.
|
|
102
|
+
|
|
103
|
+
## Known limits
|
|
104
|
+
|
|
105
|
+
- On EVM, a takeover (`useHere()`) picks its one-time key from the local record. If two devices take over the same account at the same moment, or a store is lost and restored, they can pick the same key. Take over from one device at a time and never restore a store from a backup.
|
|
106
|
+
|
|
107
|
+
## Tests
|
|
108
|
+
|
|
109
|
+
```
|
|
110
|
+
python3 build_sdk.py
|
|
111
|
+
node sdk/test/run.mjs
|
|
112
|
+
node sdk/test/e2e_anvil.mjs
|
|
113
|
+
node sdk/test/e2e_devnet.mjs <funded devnet keypair json>
|
|
114
|
+
node sdk/test/attack.mjs
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
- `run.mjs` checks the JS code against vectors from the Python reference implementation (`sdk/test/gen_vectors.py`): words, key trees, addresses, signatures and the exact signed message. Every result must be byte-identical.
|
|
118
|
+
- `e2e_anvil.mjs` runs a full EVM account on a local chain: setup, sends, offline verification, and a takeover from a second device.
|
|
119
|
+
- `e2e_devnet.mjs` does the same on Solana devnet: activate, send, verify, rekey.
|
|
120
|
+
- `attack.mjs` runs the attacks from the security review: lock races between processes, symlink and permission tricks on the wallet folder, signature forgeries against the verifiers, malformed amounts and data, shared fee keys, and packing a stray wallet file.
|
|
121
|
+
|
|
122
|
+
## License
|
|
123
|
+
|
|
124
|
+
Apache-2.0
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { Vault } from "@opeqlabs/sdk";
|
|
4
|
+
import { FileStore } from "@opeqlabs/sdk/filestore";
|
|
5
|
+
|
|
6
|
+
const [, , cmd, ...rest] = process.argv;
|
|
7
|
+
const chainId = Number(process.env.OPEQ_CHAIN || 8453);
|
|
8
|
+
const password = process.env.OPEQ_PASSWORD;
|
|
9
|
+
const rpcUrl = process.env.OPEQ_EVM_RPC, checkUrl = process.env.OPEQ_EVM_CHECK;
|
|
10
|
+
if (!password || !rpcUrl || !checkUrl) throw new Error("set OPEQ_PASSWORD, OPEQ_EVM_RPC and OPEQ_EVM_CHECK (two independent RPCs)");
|
|
11
|
+
|
|
12
|
+
const store = await FileStore.open(process.env.OPEQ_DIR || join(homedir(), ".opeq-sdk-wallet"));
|
|
13
|
+
const vault = new Vault({ rpcUrl: process.env.OPEQ_RPC || "https://api.mainnet-beta.solana.com", checkRpcUrl: process.env.OPEQ_CHECK_RPC || "https://solana-rpc.publicnode.com", network: process.env.OPEQ_NETWORK || "mainnet", store });
|
|
14
|
+
try {
|
|
15
|
+
const [w] = await vault.list();
|
|
16
|
+
if (!w) throw new Error("no wallet yet, create one with solana-send.mjs create");
|
|
17
|
+
const acct = await vault.open(w.id, password);
|
|
18
|
+
try {
|
|
19
|
+
const evm = await acct.evm(chainId, { rpcUrl, checkUrl });
|
|
20
|
+
console.log("account", evm.address, "fee key", evm.feeAddress);
|
|
21
|
+
if (cmd === "status") console.log(await evm.status());
|
|
22
|
+
else if (cmd === "setup") console.log(await evm.setup(null, (s) => console.log("-", s)));
|
|
23
|
+
else if (cmd === "send") console.log(await evm.send(rest[0], BigInt(rest[1]), (h) => console.log("tx", h)));
|
|
24
|
+
else if (cmd === "rekey") console.log(await evm.rekey());
|
|
25
|
+
else console.log("commands: status, setup, send <to> <wei>, rekey");
|
|
26
|
+
} finally { acct.close(); }
|
|
27
|
+
} finally { await store.close(); }
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { Vault, NETWORKS } from "@opeqlabs/sdk";
|
|
4
|
+
import { FileStore } from "@opeqlabs/sdk/filestore";
|
|
5
|
+
|
|
6
|
+
const [, , cmd, ...rest] = process.argv;
|
|
7
|
+
const network = process.env.OPEQ_NETWORK || "devnet";
|
|
8
|
+
const password = process.env.OPEQ_PASSWORD;
|
|
9
|
+
if (!password) throw new Error("set OPEQ_PASSWORD");
|
|
10
|
+
if (!process.env.OPEQ_RPC) throw new Error(`set OPEQ_RPC to a Solana ${network} RPC URL`);
|
|
11
|
+
if (network === "mainnet" && !process.env.OPEQ_CHECK_RPC) throw new Error("set OPEQ_CHECK_RPC to a second Solana RPC from another provider");
|
|
12
|
+
|
|
13
|
+
const store = await FileStore.open(process.env.OPEQ_DIR || join(homedir(), ".opeq-sdk-wallet"));
|
|
14
|
+
const vault = new Vault({ rpcUrl: process.env.OPEQ_RPC, checkRpcUrl: process.env.OPEQ_CHECK_RPC, network, store });
|
|
15
|
+
try {
|
|
16
|
+
if (cmd === "create") {
|
|
17
|
+
const { id, words } = await vault.create(password);
|
|
18
|
+
console.log("wallet", id);
|
|
19
|
+
console.log("write these 24 words down and keep them offline:\n" + words);
|
|
20
|
+
} else {
|
|
21
|
+
const [w] = await vault.list();
|
|
22
|
+
if (!w) throw new Error("no wallet yet, run: create");
|
|
23
|
+
const acct = await vault.open(w.id, password);
|
|
24
|
+
try {
|
|
25
|
+
console.log("wallet", acct.address, "fee key", await acct.feeKey());
|
|
26
|
+
if (cmd === "activate") console.log(await acct.activate());
|
|
27
|
+
else if (cmd === "status") console.log(await acct.status(), "balance", await acct.balance());
|
|
28
|
+
else if (cmd === "send") console.log("signature", await acct.send(rest[0], BigInt(rest[1])));
|
|
29
|
+
else if (cmd === "rekey") console.log(await acct.rekey());
|
|
30
|
+
else console.log("commands: create, activate, status, send <to> <lamports>, rekey");
|
|
31
|
+
} finally { acct.close(); }
|
|
32
|
+
}
|
|
33
|
+
} finally { await store.close(); }
|
|
34
|
+
console.log("program", NETWORKS[network].program);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { NETWORKS, verifySolanaInstruction, b58decode, b58encode, lowLevel } from "@opeqlabs/sdk";
|
|
2
|
+
|
|
3
|
+
const [, , rpcUrl, signature, network = "mainnet"] = process.argv;
|
|
4
|
+
if (!rpcUrl || !signature) throw new Error("usage: node verify.mjs <solana rpc> <transaction signature> [mainnet|devnet]");
|
|
5
|
+
const call = async (method, params) => {
|
|
6
|
+
const r = await fetch(rpcUrl, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }) });
|
|
7
|
+
const j = await r.json();
|
|
8
|
+
if (j.error) throw new Error(j.error.message);
|
|
9
|
+
return j.result;
|
|
10
|
+
};
|
|
11
|
+
const program = NETWORKS[network].program;
|
|
12
|
+
const tx = await call("getTransaction", [signature, { encoding: "json", maxSupportedTransactionVersion: 0 }]);
|
|
13
|
+
if (!tx) throw new Error("transaction not found");
|
|
14
|
+
if (tx.meta.err) console.log("note: this transaction failed on chain:", JSON.stringify(tx.meta.err));
|
|
15
|
+
const m = tx.transaction.message, la = tx.meta.loadedAddresses || {};
|
|
16
|
+
const keys = [...m.accountKeys, ...(la.writable || []), ...(la.readonly || [])];
|
|
17
|
+
const found = m.instructions.filter((x) => keys[x.programIdIndex] === program);
|
|
18
|
+
if (!found.length) throw new Error("this transaction has no top-level OPEQ instruction");
|
|
19
|
+
for (const ix of found) {
|
|
20
|
+
const accounts = ix.accounts.map((a) => keys[a]);
|
|
21
|
+
const state = await call("getAccountInfo", [accounts[0], { encoding: "base64" }]);
|
|
22
|
+
if (!state.value || state.value.owner !== program) { console.log("skipped: the first account is not an OPEQ state account"); continue; }
|
|
23
|
+
const st = lowLevel.protocol.unpackState(Uint8Array.from(atob(state.value.data[0]), (c) => c.charCodeAt(0)));
|
|
24
|
+
const P = lowLevel.protocol, prog = b58decode(program);
|
|
25
|
+
const acc = { state: b58encode(P.statePda(st.id, prog).key), wallet: b58encode(P.walletPda(st.id, prog).key) };
|
|
26
|
+
if (acc.state !== accounts[0] || acc.wallet !== accounts[1]) { console.log("skipped: state and wallet accounts do not belong together"); continue; }
|
|
27
|
+
const leaf = verifySolanaInstruction(b58decode(ix.data), accounts, { pubSeed: st.pubSeed, root: st.root, aid: st.id, epoch: st.epoch }, network);
|
|
28
|
+
console.log(`wallet ${acc.wallet}:`, leaf === null ? "does not verify against the current key tree (it may have been signed before a rekey)" : `valid quantum signature, one-time key #${leaf}`);
|
|
29
|
+
}
|
package/filestore.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { promises as fs, constants as C, openSync, writeSync, closeSync, readFileSync, unlinkSync, linkSync, lstatSync, fsyncSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
4
|
+
import { checkForward } from "./src/lib/vaultstore.js";
|
|
5
|
+
|
|
6
|
+
const LOCK = ".lock";
|
|
7
|
+
const TAKEOVER = ".takeover";
|
|
8
|
+
const NOFOLLOW = C.O_NOFOLLOW || 0;
|
|
9
|
+
|
|
10
|
+
const name = (id) => createHash("sha256").update(String(id)).digest("hex") + ".json";
|
|
11
|
+
|
|
12
|
+
const encode = (r) => JSON.stringify(r, (k, v) => (typeof v === "bigint" ? { $big: v.toString() } : v));
|
|
13
|
+
const decode = (s) => JSON.parse(s, (k, v) => (v && typeof v === "object" && Object.keys(v).length === 1 && typeof v.$big === "string" ? BigInt(v.$big) : v));
|
|
14
|
+
|
|
15
|
+
function alive(pid) {
|
|
16
|
+
try { process.kill(pid, 0); return true; } catch (e) { return e.code === "EPERM"; }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function readLock(path) {
|
|
20
|
+
try { return readFileSync(path, "utf8"); } catch (e) { if (e.code === "ENOENT") return null; throw e; }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function pidOf(text) {
|
|
24
|
+
const m = /^(\d+) [0-9a-f]{32}$/.exec(text || "");
|
|
25
|
+
return m ? Number(m[1]) : null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function exclusive(path, text) {
|
|
29
|
+
const fd = openSync(path, C.O_WRONLY | C.O_CREAT | C.O_EXCL | NOFOLLOW, 0o600);
|
|
30
|
+
try { writeSync(fd, text); fsyncSync(fd); } finally { closeSync(fd); }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function checkDir(dir) {
|
|
34
|
+
const st = lstatSync(dir);
|
|
35
|
+
if (st.isSymbolicLink() || !st.isDirectory()) throw new Error("the wallet folder must be a real folder, not a link");
|
|
36
|
+
if (typeof process.getuid === "function") {
|
|
37
|
+
if (st.uid !== process.getuid()) throw new Error("the wallet folder belongs to another user");
|
|
38
|
+
if (st.mode & 0o077) throw new Error(`the wallet folder can be read or changed by other users (mode ${(st.mode & 0o777).toString(8)}); run chmod 700 on it`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function takeLock(dir) {
|
|
43
|
+
const path = join(dir, LOCK);
|
|
44
|
+
const mine = `${process.pid} ${randomBytes(16).toString("hex")}`;
|
|
45
|
+
const tmp = join(dir, `.lock-${randomBytes(8).toString("hex")}`);
|
|
46
|
+
exclusive(tmp, mine);
|
|
47
|
+
try {
|
|
48
|
+
try { linkSync(tmp, path); return mine; } catch (e) { if (e.code !== "EEXIST") throw e; }
|
|
49
|
+
const held = readLock(path);
|
|
50
|
+
const pid = pidOf(held);
|
|
51
|
+
if (pid === null || alive(pid)) {
|
|
52
|
+
throw new Error(`another process${pid ? ` (pid ${pid})` : ""} is using this wallet folder; one-time keys must never be used from two places at once`);
|
|
53
|
+
}
|
|
54
|
+
const tk = join(dir, TAKEOVER);
|
|
55
|
+
try { exclusive(tk, mine); } catch (e) {
|
|
56
|
+
if (e.code === "EEXIST") throw new Error(`another process is taking over this wallet folder. If none is running, delete ${tk}`);
|
|
57
|
+
throw e;
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
const again = readLock(path);
|
|
61
|
+
if (again !== held) throw new Error("the wallet folder lock changed while taking it over; try again");
|
|
62
|
+
unlinkSync(path);
|
|
63
|
+
try { linkSync(tmp, path); } catch (e) {
|
|
64
|
+
if (e.code === "EEXIST") throw new Error("another process took this wallet folder first");
|
|
65
|
+
throw e;
|
|
66
|
+
}
|
|
67
|
+
return mine;
|
|
68
|
+
} finally { try { unlinkSync(tk); } catch {} }
|
|
69
|
+
} finally { try { unlinkSync(tmp); } catch {} }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export class FileStore {
|
|
73
|
+
static async open(dir) {
|
|
74
|
+
await fs.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
75
|
+
checkDir(dir);
|
|
76
|
+
return new FileStore(dir, takeLock(dir));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
constructor(dir, token) {
|
|
80
|
+
if (!token) throw new Error("use FileStore.open(dir)");
|
|
81
|
+
this.dir = dir;
|
|
82
|
+
this.token = token;
|
|
83
|
+
this.lockPath = join(dir, LOCK);
|
|
84
|
+
this.closed = false;
|
|
85
|
+
this.queue = Promise.resolve();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
live() {
|
|
89
|
+
if (this.closed) throw new Error("this wallet store is closed");
|
|
90
|
+
if (readLock(this.lockPath) !== this.token) {
|
|
91
|
+
this.closed = true;
|
|
92
|
+
throw new Error("this process no longer holds the wallet folder lock; stopping before any key is used");
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
serial(fn) {
|
|
97
|
+
const run = this.queue.then(() => { this.live(); return fn(); });
|
|
98
|
+
this.queue = run.catch(() => {});
|
|
99
|
+
return run;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async read(file) {
|
|
103
|
+
let h;
|
|
104
|
+
try { h = await fs.open(join(this.dir, file), C.O_RDONLY | NOFOLLOW); }
|
|
105
|
+
catch (e) { if (e.code === "ENOENT") return null; if (e.code === "ELOOP") throw new Error("a wallet record is a link, refusing to read it"); throw e; }
|
|
106
|
+
try {
|
|
107
|
+
if (!(await h.stat()).isFile()) throw new Error("a wallet record is not a regular file");
|
|
108
|
+
return decode(await h.readFile("utf8"));
|
|
109
|
+
} finally { await h.close(); }
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async all() {
|
|
113
|
+
this.live();
|
|
114
|
+
const out = [];
|
|
115
|
+
for (const f of await fs.readdir(this.dir)) if (/^[0-9a-f]{64}\.json$/.test(f)) { const r = await this.read(f); if (r) out.push(r); }
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async get(id) { this.live(); return this.read(name(id)); }
|
|
120
|
+
|
|
121
|
+
put(r) {
|
|
122
|
+
this.live();
|
|
123
|
+
return this.serial(async () => {
|
|
124
|
+
const file = name(r.id);
|
|
125
|
+
checkForward(await this.read(file), r);
|
|
126
|
+
const tmp = join(this.dir, `.${randomBytes(12).toString("hex")}.tmp`);
|
|
127
|
+
const h = await fs.open(tmp, C.O_WRONLY | C.O_CREAT | C.O_EXCL | NOFOLLOW, 0o600);
|
|
128
|
+
try { await h.writeFile(encode(r)); await h.sync(); } finally { await h.close(); }
|
|
129
|
+
try { await fs.rename(tmp, join(this.dir, file)); } catch (e) { await fs.rm(tmp, { force: true }); throw e; }
|
|
130
|
+
const d = await fs.open(this.dir, "r");
|
|
131
|
+
try { await d.sync(); } catch {} finally { await d.close(); }
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
del(id) {
|
|
136
|
+
this.live();
|
|
137
|
+
return this.serial(async () => { await fs.rm(join(this.dir, name(id)), { force: true }); });
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async close() {
|
|
141
|
+
if (this.closed) return;
|
|
142
|
+
const pending = this.queue;
|
|
143
|
+
this.queue = this.queue.then(() => { throw new Error("this wallet store is closed"); });
|
|
144
|
+
this.queue.catch(() => {});
|
|
145
|
+
await pending;
|
|
146
|
+
this.closed = true;
|
|
147
|
+
if (readLock(this.lockPath) === this.token) { try { unlinkSync(this.lockPath); } catch {} }
|
|
148
|
+
}
|
|
149
|
+
}
|