@universal-everything/sdk-core-backup 1.0.0-dev.50f8f9d
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 +201 -0
- package/README.md +59 -0
- package/dist/index.d.ts +224 -0
- package/dist/index.js +467 -0
- package/package.json +35 -0
package/LICENSE
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 2023 LUKSO Blockchain GmbH
|
|
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/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# @universal-everything/sdk-core-backup
|
|
2
|
+
|
|
3
|
+
Encode / decode the LUKSO **V2 backup-file format** (PBKDF2-SHA256 +
|
|
4
|
+
AES-256-GCM). Round-trips with the official LUKSO browser extension
|
|
5
|
+
and first-party mobile app — share backup files across apps or write
|
|
6
|
+
your own restore tool.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
pnpm add @universal-everything/sdk-core-backup
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Quick start — encrypt a wallet to a file
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { encodeBackup, type WalletData } from "@universal-everything/sdk-core-backup";
|
|
18
|
+
|
|
19
|
+
const wallet: WalletData = {
|
|
20
|
+
address: "0x1234…",
|
|
21
|
+
privateKey: "0xabcd…", // 32-byte hex
|
|
22
|
+
label: "main",
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const fileBytes = await encodeBackup(wallet, {
|
|
26
|
+
password: "user-chosen-password",
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Save fileBytes to disk, upload to iCloud, etc.
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Quick start — restore from a file
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { decodeBackup } from "@universal-everything/sdk-core-backup";
|
|
36
|
+
|
|
37
|
+
const wallet = await decodeBackup(fileBytes, {
|
|
38
|
+
password: "user-chosen-password",
|
|
39
|
+
});
|
|
40
|
+
// -> { address, privateKey, label }
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Format details
|
|
44
|
+
|
|
45
|
+
- **Key derivation**: PBKDF2-SHA256, configurable iterations (default
|
|
46
|
+
matches the LUKSO extension's current setting), 16-byte random salt.
|
|
47
|
+
- **Cipher**: AES-256-GCM with a 12-byte random IV, 16-byte auth tag.
|
|
48
|
+
- **Version byte** up front so future format changes stay
|
|
49
|
+
backwards-compatible on decode.
|
|
50
|
+
|
|
51
|
+
Wrong passwords fail at the AES-GCM auth-tag check — `decodeBackup`
|
|
52
|
+
throws with a typed error so you can distinguish "bad password" from
|
|
53
|
+
"file corrupted".
|
|
54
|
+
|
|
55
|
+
## Compatibility
|
|
56
|
+
|
|
57
|
+
- ✅ LUKSO browser extension V2 backups
|
|
58
|
+
- ✅ First-party LUKSO mobile app V2 backups
|
|
59
|
+
- ❌ V1 backup format (legacy, not supported here; use the old tooling)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* V2 backup file format for LUKSO wallets.
|
|
3
|
+
*
|
|
4
|
+
* **Interop contract**: this format is what the LUKSO browser extension
|
|
5
|
+
* and first-party mobile app read and write. When our SDK gets embedded
|
|
6
|
+
* inside those clients (or when users move keys across them), this is
|
|
7
|
+
* the wire format. Preserve round-trip compatibility over minimalism.
|
|
8
|
+
*
|
|
9
|
+
* Ported from service-auth-simple/packages/passkey-auth/src/backup.ts
|
|
10
|
+
* with two deliberate changes:
|
|
11
|
+
*
|
|
12
|
+
* 1. **READ side is full-fat**: `BackupFile` keeps every optional field
|
|
13
|
+
* the extension emits (accounts with networks, LSP23CrossChainDeployment,
|
|
14
|
+
* legacy `encryptedSecrets` V1). `validateBackup` / `importBackup` /
|
|
15
|
+
* `extractWalletData` work on all of them.
|
|
16
|
+
*
|
|
17
|
+
* 2. **WRITE side is simplified**: our `exportBackup` takes a single
|
|
18
|
+
* `{ privateKey, address, password? }` (plus optional profile/chain
|
|
19
|
+
* metadata) because that's what our Privy-backed SDK has. No
|
|
20
|
+
* `derivedKeys`/`usedKeyIndices` — those presume an HD wallet that
|
|
21
|
+
* Privy doesn't expose. No `LSP23CrossChainDeployment` — that lives
|
|
22
|
+
* in the Envio GraphQL indexer now; callers that need it in a file
|
|
23
|
+
* re-fetch and attach it at write time.
|
|
24
|
+
*
|
|
25
|
+
* The output still goes through the `type: "privateKey"` path in the
|
|
26
|
+
* extension's `extractWalletData` fallback (embedded-wallet case), so
|
|
27
|
+
* backups our SDK writes are readable by the extension.
|
|
28
|
+
*/
|
|
29
|
+
type BackupFile = {
|
|
30
|
+
version?: number;
|
|
31
|
+
backupDate?: string;
|
|
32
|
+
accounts?: BackupAccountV2[];
|
|
33
|
+
/** LUKSO LSP-23 cross-chain deployment payload emitted by the extension. */
|
|
34
|
+
LSP23CrossChainDeployment?: LSP23DeploymentData[];
|
|
35
|
+
secrets?: BackupSecrets;
|
|
36
|
+
/** V1 legacy shape. Readers accept, writers never emit. */
|
|
37
|
+
encryptedSecrets?: {
|
|
38
|
+
encryptionType: string;
|
|
39
|
+
secrets: {
|
|
40
|
+
data: string;
|
|
41
|
+
iv: string;
|
|
42
|
+
salt: string;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
type BackupAccountV2 = {
|
|
47
|
+
type: "LSP0-ERC725Account";
|
|
48
|
+
name?: string;
|
|
49
|
+
address: string;
|
|
50
|
+
networks: BackupNetwork[];
|
|
51
|
+
};
|
|
52
|
+
type BackupNetwork = {
|
|
53
|
+
chainID: number;
|
|
54
|
+
name: string;
|
|
55
|
+
controllers: LegacyBackupController[];
|
|
56
|
+
};
|
|
57
|
+
type LegacyBackupController = {
|
|
58
|
+
address: string;
|
|
59
|
+
type: "UniversalReceiver" | "Device" | "LSP0-ERC725Account";
|
|
60
|
+
name: string;
|
|
61
|
+
privateKeyIndex?: number;
|
|
62
|
+
};
|
|
63
|
+
type LSP23DeploymentData = {
|
|
64
|
+
profileAddress: string;
|
|
65
|
+
initialChainID: number;
|
|
66
|
+
factoryAddress: string;
|
|
67
|
+
deploymentCalldata: string;
|
|
68
|
+
initialControllers: BackupController[];
|
|
69
|
+
};
|
|
70
|
+
type BackupController = {
|
|
71
|
+
address: string;
|
|
72
|
+
privateKeyIndex?: number;
|
|
73
|
+
addressPermissions: BackupAddressPermissions;
|
|
74
|
+
};
|
|
75
|
+
type BackupAddressPermissions = {
|
|
76
|
+
permissions: string;
|
|
77
|
+
decodedPermissions: Record<string, boolean>;
|
|
78
|
+
allowedCalls: string;
|
|
79
|
+
allowedERC725YDataKeys: string;
|
|
80
|
+
};
|
|
81
|
+
type BackupSecrets = {
|
|
82
|
+
encrypted: boolean;
|
|
83
|
+
passwordHint?: string;
|
|
84
|
+
encryptionType?: string;
|
|
85
|
+
data: BackupSecretsData | EncryptedSecretsData;
|
|
86
|
+
};
|
|
87
|
+
type BackupSecretEntry = {
|
|
88
|
+
type: "privateKey" | "mnemonic";
|
|
89
|
+
index: number;
|
|
90
|
+
address: string;
|
|
91
|
+
secret: string;
|
|
92
|
+
/** Which mnemonic this key was derived from (null = standalone; undefined = legacy). */
|
|
93
|
+
mnemonicIndex?: number | null;
|
|
94
|
+
mnemonic?: string;
|
|
95
|
+
entropy?: string;
|
|
96
|
+
derivationPath?: string;
|
|
97
|
+
};
|
|
98
|
+
type BackupSecretsData = BackupSecretEntry[];
|
|
99
|
+
type EncryptedSecretsData = {
|
|
100
|
+
/** base64 ciphertext of the JSON-serialized `BackupSecretsData` array. */
|
|
101
|
+
secret: string;
|
|
102
|
+
iv: string;
|
|
103
|
+
salt: string;
|
|
104
|
+
};
|
|
105
|
+
/** What `extractWalletData` / `importBackup` yield on success. */
|
|
106
|
+
type WalletData = {
|
|
107
|
+
/** Optional — embedded-wallet exports don't carry a recovery phrase. */
|
|
108
|
+
mnemonic: string;
|
|
109
|
+
entropy: string;
|
|
110
|
+
privateKey: string;
|
|
111
|
+
address: string;
|
|
112
|
+
metadata?: {
|
|
113
|
+
createdAt: string;
|
|
114
|
+
network: string;
|
|
115
|
+
derivationPath?: string;
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* Structural validation of an arbitrary value against V2 (or legacy V1)
|
|
120
|
+
* shape. Throws a descriptive error; returns `true` as a type-narrowing
|
|
121
|
+
* assertion for use in TypeScript code paths.
|
|
122
|
+
*/
|
|
123
|
+
declare function validateBackup(backup: unknown): backup is BackupFile;
|
|
124
|
+
/**
|
|
125
|
+
* Pulls the primary wallet (private key + address + optional
|
|
126
|
+
* mnemonic/entropy/derivationPath) out of an *unencrypted* backup.
|
|
127
|
+
*
|
|
128
|
+
* Tries in order:
|
|
129
|
+
*
|
|
130
|
+
* 1. Mnemonic-style entry (or index-0 entry carrying mnemonic/entropy).
|
|
131
|
+
* 2. privateKey entry with a `mnemonicIndex` pointer → walk to the
|
|
132
|
+
* referenced mnemonic entry.
|
|
133
|
+
* 3. **Embedded-wallet fallback** (when `isImport=true`): standalone
|
|
134
|
+
* privateKey entries with no mnemonic reference — take the first.
|
|
135
|
+
* This is the path our own SDK's `exportBackup` output goes through
|
|
136
|
+
* on the receiving end.
|
|
137
|
+
*/
|
|
138
|
+
declare function extractWalletData(backup: BackupFile, isImport?: boolean): WalletData;
|
|
139
|
+
type ExportBackupOptions = {
|
|
140
|
+
/** 0x-prefixed 32-byte hex. */
|
|
141
|
+
privateKey: string;
|
|
142
|
+
/** 0x-prefixed 20-byte EIP-55-cased address. */
|
|
143
|
+
address: string;
|
|
144
|
+
/** When present, secrets are PBKDF2+AES-GCM encrypted. */
|
|
145
|
+
password?: string;
|
|
146
|
+
/**
|
|
147
|
+
* Optional profile address the key controls. When provided, an empty
|
|
148
|
+
* `accounts` entry is emitted so extension readers can link the key to
|
|
149
|
+
* the profile. LSP23 deployment metadata (factoryAddress,
|
|
150
|
+
* deploymentCalldata, etc.) is *not* written — it lives in the Envio
|
|
151
|
+
* GraphQL indexer; callers that need it in a file re-fetch and attach
|
|
152
|
+
* post-hoc.
|
|
153
|
+
*/
|
|
154
|
+
profileAddress?: string;
|
|
155
|
+
/** Chain id the profile lives on. Only meaningful alongside `profileAddress`. */
|
|
156
|
+
chainId?: number;
|
|
157
|
+
/** Optional human-readable profile name. */
|
|
158
|
+
profileName?: string;
|
|
159
|
+
};
|
|
160
|
+
/**
|
|
161
|
+
* Produce a minimal V2 backup file from a single (Privy-exported) key.
|
|
162
|
+
* Readable by the LUKSO browser extension and first-party mobile app via
|
|
163
|
+
* their embedded-wallet fallback path.
|
|
164
|
+
*/
|
|
165
|
+
declare function exportBackup(options: ExportBackupOptions): Promise<BackupFile>;
|
|
166
|
+
/** Stable filename convention: `lukso-wallet-backup-<ISO>[-plaintext].json`. */
|
|
167
|
+
declare function backupFilename(backup: BackupFile): string;
|
|
168
|
+
/**
|
|
169
|
+
* Triggers a browser download of the backup JSON. Skipped gracefully
|
|
170
|
+
* with a clear error when called outside a browser environment so test
|
|
171
|
+
* runs don't need to stub `document`.
|
|
172
|
+
*/
|
|
173
|
+
declare function downloadBackup(backup: BackupFile, filename?: string): void;
|
|
174
|
+
/** Parse + validate a JSON string. Node-safe (no File/FileReader). */
|
|
175
|
+
declare function parseBackup(json: string): BackupFile;
|
|
176
|
+
/** Browser helper that reads an uploaded File into a validated `BackupFile`. */
|
|
177
|
+
declare function readBackupFile(file: File): Promise<BackupFile>;
|
|
178
|
+
/**
|
|
179
|
+
* Decrypt (if needed) + extract the primary wallet from a backup.
|
|
180
|
+
* `isImport=true` enables the embedded-wallet fallback path.
|
|
181
|
+
*/
|
|
182
|
+
declare function importBackup(backup: BackupFile, password?: string, isImport?: boolean): Promise<WalletData>;
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* PBKDF2 (SHA-256, 100k iterations) + AES-256-GCM over the Web Crypto API.
|
|
186
|
+
* Ported from service-auth-simple/packages/passkey-auth/src/encryption.ts
|
|
187
|
+
* unchanged — parameters are the interop contract with the LUKSO browser
|
|
188
|
+
* extension and first-party mobile app, so tweaking them silently
|
|
189
|
+
* breaks round-trip compatibility.
|
|
190
|
+
*/
|
|
191
|
+
type EncryptionResult = {
|
|
192
|
+
/** base64 ciphertext (includes GCM auth tag at the end). */
|
|
193
|
+
ciphertext: string;
|
|
194
|
+
/** base64 salt used for PBKDF2 derivation. */
|
|
195
|
+
salt: string;
|
|
196
|
+
/** base64 12-byte IV used for AES-GCM. */
|
|
197
|
+
iv: string;
|
|
198
|
+
};
|
|
199
|
+
type DecryptionOptions = {
|
|
200
|
+
ciphertext: string;
|
|
201
|
+
salt: string;
|
|
202
|
+
iv: string;
|
|
203
|
+
password: string;
|
|
204
|
+
};
|
|
205
|
+
declare function deriveKey(password: string, salt: Uint8Array, iterations?: number): Promise<CryptoKey>;
|
|
206
|
+
declare function encrypt(plaintext: string, password: string): Promise<EncryptionResult>;
|
|
207
|
+
declare function decrypt(options: DecryptionOptions): Promise<string>;
|
|
208
|
+
declare function encryptBackupData(data: unknown, password: string): Promise<{
|
|
209
|
+
secret: string;
|
|
210
|
+
iv: string;
|
|
211
|
+
salt: string;
|
|
212
|
+
}>;
|
|
213
|
+
declare function decryptBackupData(encrypted: {
|
|
214
|
+
secret?: string;
|
|
215
|
+
data?: string;
|
|
216
|
+
iv: string;
|
|
217
|
+
salt: string;
|
|
218
|
+
}, password: string): Promise<unknown>;
|
|
219
|
+
declare function validatePasswordStrength(password: string): {
|
|
220
|
+
valid: boolean;
|
|
221
|
+
message?: string;
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
export { type BackupAccountV2, type BackupAddressPermissions, type BackupController, type BackupFile, type BackupNetwork, type BackupSecretEntry, type BackupSecrets, type BackupSecretsData, type DecryptionOptions, type EncryptedSecretsData, type EncryptionResult, type ExportBackupOptions, type LSP23DeploymentData, type LegacyBackupController, type WalletData, backupFilename, decrypt, decryptBackupData, deriveKey, downloadBackup, encrypt, encryptBackupData, exportBackup, extractWalletData, importBackup, parseBackup, readBackupFile, validateBackup, validatePasswordStrength };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
// src/encryption.ts
|
|
2
|
+
var PBKDF2_ITERATIONS = 1e5;
|
|
3
|
+
var SALT_LENGTH = 16;
|
|
4
|
+
var IV_LENGTH = 12;
|
|
5
|
+
var KEY_LENGTH = 256;
|
|
6
|
+
function arrayBufferToBase64(buffer) {
|
|
7
|
+
const bytes = new Uint8Array(buffer);
|
|
8
|
+
let binary = "";
|
|
9
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
10
|
+
binary += String.fromCharCode(bytes[i]);
|
|
11
|
+
}
|
|
12
|
+
return btoa(binary);
|
|
13
|
+
}
|
|
14
|
+
function base64ToArrayBuffer(base64) {
|
|
15
|
+
const binary = atob(base64);
|
|
16
|
+
const bytes = new Uint8Array(binary.length);
|
|
17
|
+
for (let i = 0; i < binary.length; i++) {
|
|
18
|
+
bytes[i] = binary.charCodeAt(i);
|
|
19
|
+
}
|
|
20
|
+
return bytes.buffer;
|
|
21
|
+
}
|
|
22
|
+
function generateSalt() {
|
|
23
|
+
return crypto.getRandomValues(new Uint8Array(SALT_LENGTH));
|
|
24
|
+
}
|
|
25
|
+
function generateIV() {
|
|
26
|
+
return crypto.getRandomValues(new Uint8Array(IV_LENGTH));
|
|
27
|
+
}
|
|
28
|
+
async function deriveKey(password, salt, iterations = PBKDF2_ITERATIONS) {
|
|
29
|
+
const passwordBuffer = new TextEncoder().encode(password);
|
|
30
|
+
const keyMaterial = await crypto.subtle.importKey(
|
|
31
|
+
"raw",
|
|
32
|
+
passwordBuffer,
|
|
33
|
+
"PBKDF2",
|
|
34
|
+
false,
|
|
35
|
+
["deriveBits", "deriveKey"]
|
|
36
|
+
);
|
|
37
|
+
return crypto.subtle.deriveKey(
|
|
38
|
+
{
|
|
39
|
+
name: "PBKDF2",
|
|
40
|
+
salt,
|
|
41
|
+
iterations,
|
|
42
|
+
hash: "SHA-256"
|
|
43
|
+
},
|
|
44
|
+
keyMaterial,
|
|
45
|
+
{ name: "AES-GCM", length: KEY_LENGTH },
|
|
46
|
+
false,
|
|
47
|
+
["encrypt", "decrypt"]
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
async function encrypt(plaintext, password) {
|
|
51
|
+
const salt = generateSalt();
|
|
52
|
+
const iv = generateIV();
|
|
53
|
+
const key = await deriveKey(password, salt);
|
|
54
|
+
const ciphertextBuffer = await crypto.subtle.encrypt(
|
|
55
|
+
{ name: "AES-GCM", iv },
|
|
56
|
+
key,
|
|
57
|
+
new TextEncoder().encode(plaintext)
|
|
58
|
+
);
|
|
59
|
+
return {
|
|
60
|
+
ciphertext: arrayBufferToBase64(ciphertextBuffer),
|
|
61
|
+
salt: arrayBufferToBase64(salt.buffer),
|
|
62
|
+
iv: arrayBufferToBase64(iv.buffer)
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
async function decrypt(options) {
|
|
66
|
+
const { ciphertext, salt, iv, password } = options;
|
|
67
|
+
const saltBuffer = new Uint8Array(base64ToArrayBuffer(salt));
|
|
68
|
+
const ivBuffer = new Uint8Array(base64ToArrayBuffer(iv));
|
|
69
|
+
const ciphertextBuffer = base64ToArrayBuffer(ciphertext);
|
|
70
|
+
const key = await deriveKey(password, saltBuffer);
|
|
71
|
+
try {
|
|
72
|
+
const plaintextBuffer = await crypto.subtle.decrypt(
|
|
73
|
+
{ name: "AES-GCM", iv: ivBuffer },
|
|
74
|
+
key,
|
|
75
|
+
ciphertextBuffer
|
|
76
|
+
);
|
|
77
|
+
return new TextDecoder().decode(plaintextBuffer);
|
|
78
|
+
} catch {
|
|
79
|
+
throw new Error("Decryption failed: incorrect password or corrupted data");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async function encryptBackupData(data, password) {
|
|
83
|
+
const plaintext = JSON.stringify(data);
|
|
84
|
+
const result = await encrypt(plaintext, password);
|
|
85
|
+
return { secret: result.ciphertext, iv: result.iv, salt: result.salt };
|
|
86
|
+
}
|
|
87
|
+
async function decryptBackupData(encrypted, password) {
|
|
88
|
+
const ciphertext = encrypted.secret ?? encrypted.data;
|
|
89
|
+
if (!ciphertext) {
|
|
90
|
+
throw new Error(
|
|
91
|
+
'Missing encrypted data: expected either "secret" or "data" field'
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
const plaintext = await decrypt({
|
|
95
|
+
ciphertext,
|
|
96
|
+
salt: encrypted.salt,
|
|
97
|
+
iv: encrypted.iv,
|
|
98
|
+
password
|
|
99
|
+
});
|
|
100
|
+
return JSON.parse(plaintext);
|
|
101
|
+
}
|
|
102
|
+
function validatePasswordStrength(password) {
|
|
103
|
+
if (password.length < 8) {
|
|
104
|
+
return {
|
|
105
|
+
valid: false,
|
|
106
|
+
message: "Password must be at least 8 characters long"
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
if (password.length > 128) {
|
|
110
|
+
return {
|
|
111
|
+
valid: false,
|
|
112
|
+
message: "Password must be less than 128 characters long"
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
if (!/\d/.test(password)) {
|
|
116
|
+
return {
|
|
117
|
+
valid: false,
|
|
118
|
+
message: "Password must contain at least one number"
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
if (!/[a-zA-Z]/.test(password)) {
|
|
122
|
+
return {
|
|
123
|
+
valid: false,
|
|
124
|
+
message: "Password must contain at least one letter"
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
return { valid: true };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// src/backup.ts
|
|
131
|
+
function validateBackup(backup) {
|
|
132
|
+
if (!backup || typeof backup !== "object") {
|
|
133
|
+
throw new Error("Invalid backup file: not an object");
|
|
134
|
+
}
|
|
135
|
+
const b = backup;
|
|
136
|
+
if (typeof b.version !== "number") {
|
|
137
|
+
throw new Error("Invalid backup file: missing or invalid version");
|
|
138
|
+
}
|
|
139
|
+
if (b.version !== 2) {
|
|
140
|
+
throw new Error(
|
|
141
|
+
`Unsupported backup version: ${b.version}. Only version 2 is supported.`
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
if (typeof b.backupDate !== "string") {
|
|
145
|
+
throw new Error("Invalid backup file: missing or invalid backupDate");
|
|
146
|
+
}
|
|
147
|
+
if (!Array.isArray(b.accounts)) {
|
|
148
|
+
throw new Error("Invalid backup file: accounts must be an array");
|
|
149
|
+
}
|
|
150
|
+
for (const account of b.accounts) {
|
|
151
|
+
if (account?.type && account.type !== "LSP0-ERC725Account") {
|
|
152
|
+
throw new Error(
|
|
153
|
+
"Invalid backup file: account must be of type LSP0-ERC725Account"
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
if (account?.type && "networks" in account && account.networks) {
|
|
157
|
+
if (!account.address || typeof account.address !== "string") {
|
|
158
|
+
throw new Error("Invalid backup file: account missing address");
|
|
159
|
+
}
|
|
160
|
+
if (!Array.isArray(account.networks)) {
|
|
161
|
+
throw new Error(
|
|
162
|
+
"Invalid backup file: account.networks must be an array"
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (b.LSP23CrossChainDeployment) {
|
|
168
|
+
if (!Array.isArray(b.LSP23CrossChainDeployment)) {
|
|
169
|
+
throw new Error(
|
|
170
|
+
"Invalid backup file: LSP23CrossChainDeployment must be an array"
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
for (const d of b.LSP23CrossChainDeployment) {
|
|
174
|
+
if (!d.profileAddress || typeof d.profileAddress !== "string") {
|
|
175
|
+
throw new Error("Invalid LSP23 deployment: missing profileAddress");
|
|
176
|
+
}
|
|
177
|
+
if (typeof d.initialChainID !== "number") {
|
|
178
|
+
throw new Error("Invalid LSP23 deployment: missing initialChainID");
|
|
179
|
+
}
|
|
180
|
+
if (!d.factoryAddress || typeof d.factoryAddress !== "string") {
|
|
181
|
+
throw new Error("Invalid LSP23 deployment: missing factoryAddress");
|
|
182
|
+
}
|
|
183
|
+
if (!d.deploymentCalldata || typeof d.deploymentCalldata !== "string") {
|
|
184
|
+
throw new Error("Invalid LSP23 deployment: missing deploymentCalldata");
|
|
185
|
+
}
|
|
186
|
+
if (!Array.isArray(d.initialControllers)) {
|
|
187
|
+
throw new Error(
|
|
188
|
+
"Invalid LSP23 deployment: initialControllers must be an array"
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
const hasCurrent = b.secrets && typeof b.secrets === "object";
|
|
194
|
+
const hasLegacy = b.encryptedSecrets && typeof b.encryptedSecrets === "object";
|
|
195
|
+
if (!hasCurrent && !hasLegacy) {
|
|
196
|
+
throw new Error(
|
|
197
|
+
"Invalid backup file: missing secrets or encryptedSecrets section"
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
if (hasCurrent && hasLegacy) {
|
|
201
|
+
throw new Error(
|
|
202
|
+
"Invalid backup file: contains both secrets and encryptedSecrets sections"
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
if (hasCurrent && b.secrets?.encrypted) {
|
|
206
|
+
const d = b.secrets.data;
|
|
207
|
+
if (!d.secret || !d.iv || !d.salt) {
|
|
208
|
+
throw new Error(
|
|
209
|
+
"Invalid backup file: encrypted secrets missing required fields"
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (hasLegacy) {
|
|
214
|
+
const e = b.encryptedSecrets;
|
|
215
|
+
if (!e?.secrets || typeof e.secrets !== "object") {
|
|
216
|
+
throw new Error(
|
|
217
|
+
"Invalid backup file: encryptedSecrets.secrets is required"
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
if (!e.secrets.data)
|
|
221
|
+
throw new Error(
|
|
222
|
+
"Invalid backup file: encryptedSecrets.secrets.data is required"
|
|
223
|
+
);
|
|
224
|
+
if (!e.secrets.iv)
|
|
225
|
+
throw new Error(
|
|
226
|
+
"Invalid backup file: encryptedSecrets.secrets.iv is required"
|
|
227
|
+
);
|
|
228
|
+
if (!e.secrets.salt)
|
|
229
|
+
throw new Error(
|
|
230
|
+
"Invalid backup file: encryptedSecrets.secrets.salt is required"
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
function extractWalletData(backup, isImport = false) {
|
|
236
|
+
if (backup.encryptedSecrets) {
|
|
237
|
+
throw new Error(
|
|
238
|
+
"Cannot extract wallet data from legacy encrypted backup. Decrypt first."
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
if (!backup.secrets || backup.secrets.encrypted) {
|
|
242
|
+
throw new Error(
|
|
243
|
+
"Cannot extract wallet data from encrypted backup. Decrypt first."
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
const secretsData = backup.secrets.data;
|
|
247
|
+
if (!Array.isArray(secretsData) || secretsData.length === 0) {
|
|
248
|
+
throw new Error(
|
|
249
|
+
"This backup file appears to be empty or corrupted. No wallet data was found."
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
let mnemonicSecret = secretsData.find(
|
|
253
|
+
(s) => s.type === "mnemonic" || s.index === 0 && (s.mnemonic || s.entropy) || s.mnemonicIndex === 0 && (s.mnemonic || s.entropy)
|
|
254
|
+
);
|
|
255
|
+
if (!mnemonicSecret) {
|
|
256
|
+
const privateKeyWithMnemonicIndex = secretsData.find(
|
|
257
|
+
(s) => s.type === "privateKey" && s.mnemonicIndex !== void 0
|
|
258
|
+
);
|
|
259
|
+
if (privateKeyWithMnemonicIndex) {
|
|
260
|
+
mnemonicSecret = secretsData.find(
|
|
261
|
+
(s) => s.index === privateKeyWithMnemonicIndex.mnemonicIndex && (s.mnemonic || s.entropy)
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (!mnemonicSecret) {
|
|
266
|
+
const hasPrivateKeys = secretsData.some(
|
|
267
|
+
(s) => s.type === "privateKey" && s.secret && s.address
|
|
268
|
+
);
|
|
269
|
+
const hasReferencesToMnemonic = secretsData.some(
|
|
270
|
+
(s) => s.mnemonicIndex !== void 0
|
|
271
|
+
);
|
|
272
|
+
if (hasPrivateKeys && !hasReferencesToMnemonic && isImport) {
|
|
273
|
+
const firstPrivateKey = secretsData.find(
|
|
274
|
+
(s) => s.type === "privateKey" && s.secret && s.address
|
|
275
|
+
);
|
|
276
|
+
if (firstPrivateKey) {
|
|
277
|
+
return {
|
|
278
|
+
mnemonic: "",
|
|
279
|
+
entropy: "",
|
|
280
|
+
privateKey: firstPrivateKey.secret,
|
|
281
|
+
address: firstPrivateKey.address,
|
|
282
|
+
metadata: {
|
|
283
|
+
createdAt: backup.backupDate ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
284
|
+
network: "mainnet",
|
|
285
|
+
derivationPath: firstPrivateKey.derivationPath
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
const context = isImport ? "import into existing wallet" : "restore as new wallet";
|
|
291
|
+
const requirement = isImport ? "For imports, use backup files with mnemonic/entropy or contact support." : "For wallet restore, a mnemonic or entropy is required.";
|
|
292
|
+
throw new Error(
|
|
293
|
+
`This backup file cannot be used to ${context}. The wallet recovery information (mnemonic or entropy) is missing. ${requirement}`
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
if (!mnemonicSecret.secret || !mnemonicSecret.address) {
|
|
297
|
+
throw new Error(
|
|
298
|
+
"This backup file is missing critical wallet information. The file may be corrupted or incomplete."
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
const { mnemonic, entropy } = mnemonicSecret;
|
|
302
|
+
if (!mnemonic && !entropy) {
|
|
303
|
+
throw new Error(
|
|
304
|
+
"This backup file is not compatible with this application. The wallet recovery information (mnemonic or entropy) is missing."
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
if (!mnemonic || !entropy) {
|
|
308
|
+
throw new Error(
|
|
309
|
+
"This backup file is missing either mnemonic or entropy. Both should be present in the backup."
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
return {
|
|
313
|
+
mnemonic,
|
|
314
|
+
entropy,
|
|
315
|
+
privateKey: mnemonicSecret.secret,
|
|
316
|
+
address: mnemonicSecret.address,
|
|
317
|
+
metadata: {
|
|
318
|
+
createdAt: backup.backupDate ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
319
|
+
network: "mainnet",
|
|
320
|
+
derivationPath: mnemonicSecret.derivationPath
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
async function exportBackup(options) {
|
|
325
|
+
const {
|
|
326
|
+
privateKey,
|
|
327
|
+
address,
|
|
328
|
+
password,
|
|
329
|
+
profileAddress,
|
|
330
|
+
chainId,
|
|
331
|
+
profileName
|
|
332
|
+
} = options;
|
|
333
|
+
const secretEntry = {
|
|
334
|
+
type: "privateKey",
|
|
335
|
+
index: 0,
|
|
336
|
+
address,
|
|
337
|
+
secret: privateKey
|
|
338
|
+
// mnemonicIndex intentionally omitted — this is a standalone key,
|
|
339
|
+
// not derived from a mnemonic we hold.
|
|
340
|
+
};
|
|
341
|
+
let secrets;
|
|
342
|
+
if (password) {
|
|
343
|
+
const encrypted = await encryptBackupData([secretEntry], password);
|
|
344
|
+
secrets = {
|
|
345
|
+
encrypted: true,
|
|
346
|
+
encryptionType: "Key from PBKDF2. Encrypted with AES-GCM.",
|
|
347
|
+
data: encrypted
|
|
348
|
+
};
|
|
349
|
+
} else {
|
|
350
|
+
secrets = { encrypted: false, data: [secretEntry] };
|
|
351
|
+
}
|
|
352
|
+
const accounts = [];
|
|
353
|
+
if (profileAddress && chainId !== void 0) {
|
|
354
|
+
accounts.push({
|
|
355
|
+
type: "LSP0-ERC725Account",
|
|
356
|
+
name: profileName,
|
|
357
|
+
address: profileAddress,
|
|
358
|
+
networks: [
|
|
359
|
+
{ chainID: chainId, name: `chain-${chainId}`, controllers: [] }
|
|
360
|
+
]
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
return {
|
|
364
|
+
version: 2,
|
|
365
|
+
backupDate: (/* @__PURE__ */ new Date()).toISOString(),
|
|
366
|
+
accounts,
|
|
367
|
+
secrets
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
function backupFilename(backup) {
|
|
371
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-").slice(0, -5);
|
|
372
|
+
return backup.secrets?.encrypted ? `lukso-wallet-backup-${timestamp}.json` : `lukso-wallet-backup-${timestamp}-plaintext.json`;
|
|
373
|
+
}
|
|
374
|
+
function downloadBackup(backup, filename) {
|
|
375
|
+
if (typeof document === "undefined" || typeof URL === "undefined") {
|
|
376
|
+
throw new Error(
|
|
377
|
+
"downloadBackup: requires a browser environment (document + URL)."
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
const json = JSON.stringify(backup, null, 2);
|
|
381
|
+
const blob = new Blob([json], { type: "application/json" });
|
|
382
|
+
const url = URL.createObjectURL(blob);
|
|
383
|
+
const a = document.createElement("a");
|
|
384
|
+
a.href = url;
|
|
385
|
+
a.download = filename ?? backupFilename(backup);
|
|
386
|
+
document.body.appendChild(a);
|
|
387
|
+
a.click();
|
|
388
|
+
document.body.removeChild(a);
|
|
389
|
+
URL.revokeObjectURL(url);
|
|
390
|
+
}
|
|
391
|
+
function parseBackup(json) {
|
|
392
|
+
let parsed;
|
|
393
|
+
try {
|
|
394
|
+
parsed = JSON.parse(json);
|
|
395
|
+
} catch (err) {
|
|
396
|
+
throw new Error(
|
|
397
|
+
`Failed to parse backup file: ${err instanceof Error ? err.message : String(err)}`
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
validateBackup(parsed);
|
|
401
|
+
return parsed;
|
|
402
|
+
}
|
|
403
|
+
async function readBackupFile(file) {
|
|
404
|
+
return new Promise((resolve, reject) => {
|
|
405
|
+
const reader = new FileReader();
|
|
406
|
+
reader.onload = (event) => {
|
|
407
|
+
try {
|
|
408
|
+
const text = event.target?.result;
|
|
409
|
+
resolve(parseBackup(text));
|
|
410
|
+
} catch (err) {
|
|
411
|
+
reject(
|
|
412
|
+
err instanceof Error ? err : new Error(`Failed to parse backup file: ${String(err)}`)
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
reader.onerror = () => reject(new Error("Failed to read backup file"));
|
|
417
|
+
reader.readAsText(file);
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
async function importBackup(backup, password, isImport = false) {
|
|
421
|
+
validateBackup(backup);
|
|
422
|
+
let decryptedSecretsData;
|
|
423
|
+
if (backup.encryptedSecrets) {
|
|
424
|
+
if (!password) throw new Error("Password required for encrypted backup");
|
|
425
|
+
decryptedSecretsData = await decryptBackupData(
|
|
426
|
+
{
|
|
427
|
+
secret: backup.encryptedSecrets.secrets.data,
|
|
428
|
+
iv: backup.encryptedSecrets.secrets.iv,
|
|
429
|
+
salt: backup.encryptedSecrets.secrets.salt
|
|
430
|
+
},
|
|
431
|
+
password
|
|
432
|
+
);
|
|
433
|
+
} else if (backup.secrets) {
|
|
434
|
+
if (backup.secrets.encrypted) {
|
|
435
|
+
if (!password) throw new Error("Password required for encrypted backup");
|
|
436
|
+
decryptedSecretsData = await decryptBackupData(
|
|
437
|
+
backup.secrets.data,
|
|
438
|
+
password
|
|
439
|
+
);
|
|
440
|
+
} else {
|
|
441
|
+
decryptedSecretsData = backup.secrets.data;
|
|
442
|
+
}
|
|
443
|
+
} else {
|
|
444
|
+
throw new Error("Invalid backup format: no secrets found");
|
|
445
|
+
}
|
|
446
|
+
const tempBackup = {
|
|
447
|
+
...backup,
|
|
448
|
+
secrets: { encrypted: false, data: decryptedSecretsData }
|
|
449
|
+
};
|
|
450
|
+
return extractWalletData(tempBackup, isImport);
|
|
451
|
+
}
|
|
452
|
+
export {
|
|
453
|
+
backupFilename,
|
|
454
|
+
decrypt,
|
|
455
|
+
decryptBackupData,
|
|
456
|
+
deriveKey,
|
|
457
|
+
downloadBackup,
|
|
458
|
+
encrypt,
|
|
459
|
+
encryptBackupData,
|
|
460
|
+
exportBackup,
|
|
461
|
+
extractWalletData,
|
|
462
|
+
importBackup,
|
|
463
|
+
parseBackup,
|
|
464
|
+
readBackupFile,
|
|
465
|
+
validateBackup,
|
|
466
|
+
validatePasswordStrength
|
|
467
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@universal-everything/sdk-core-backup",
|
|
3
|
+
"version": "1.0.0-dev.50f8f9d",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/universal-everything/universal-relayer.git",
|
|
7
|
+
"directory": "packages/sdk-core-backup"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "^26.2.0",
|
|
26
|
+
"typescript": "^5.7.0",
|
|
27
|
+
"vitest": "^4.1.10",
|
|
28
|
+
"tsup": "^8.5.0"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"typecheck": "tsc --noEmit",
|
|
32
|
+
"test": "vitest run",
|
|
33
|
+
"build": "tsup"
|
|
34
|
+
}
|
|
35
|
+
}
|