@bsv/expo-wallet-toolbox 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.txt +28 -0
- package/README.md +512 -0
- package/assets/sounds/payment-receive.wav +0 -0
- package/assets/sounds/payment-send.wav +0 -0
- package/assets/sounds/vault-close.wav +0 -0
- package/assets/sounds/vault-open.wav +0 -0
- package/core/amountFormatHelpers.ts +330 -0
- package/core/backup/RemoteSyncReader.ts +94 -0
- package/core/backup/client.ts +111 -0
- package/core/backup/codec.ts +218 -0
- package/core/backup/constants.ts +88 -0
- package/core/backup/cursor.ts +138 -0
- package/core/backup/derive.ts +46 -0
- package/core/backup/deviceId.ts +33 -0
- package/core/backup/erase.ts +67 -0
- package/core/backup/preference.ts +33 -0
- package/core/backup/push.ts +295 -0
- package/core/backup/restore.ts +152 -0
- package/core/backup/restoreOnImport.ts +119 -0
- package/core/backup/status.ts +46 -0
- package/core/config.tsx +47 -0
- package/core/constants.ts +35 -0
- package/core/context/ExchangeRateContext.tsx +69 -0
- package/core/context/LocalStorageProvider.tsx +225 -0
- package/core/context/UserContext.tsx +129 -0
- package/core/context/VaultContext.tsx +79 -0
- package/core/context/WalletConnectionContext.tsx +454 -0
- package/core/context/WalletContext.tsx +2938 -0
- package/core/context/buildGeneration.ts +37 -0
- package/core/deviceTier.ts +89 -0
- package/core/diskSpace.ts +62 -0
- package/core/displayCurrencies.ts +56 -0
- package/core/headers/OfflineFirstChaintracks.ts +176 -0
- package/core/headers/checkpoints.ts +39 -0
- package/core/headers/fs.ts +119 -0
- package/core/headers/headerStore.ts +333 -0
- package/core/headers/prewarm.ts +36 -0
- package/core/headers/syncHeaders.ts +73 -0
- package/core/hooks/useConfirmationSound.ts +135 -0
- package/core/hooks/useHaptics.ts +51 -0
- package/core/hooks/usePermissionQueue.ts +69 -0
- package/core/i18n/translations.tsx +6692 -0
- package/core/index.ts +295 -0
- package/core/localpay/blePermissions.ts +32 -0
- package/core/localpay/build.ts +392 -0
- package/core/localpay/codec.ts +338 -0
- package/core/localpay/deviceCaps.ts +175 -0
- package/core/localpay/nearbyAdvisory.ts +32 -0
- package/core/localpay/nearbyPermissions.ts +34 -0
- package/core/localpay/pending.ts +385 -0
- package/core/localpay/pendingAborts.ts +67 -0
- package/core/localpay/qr.ts +16 -0
- package/core/localpay/session.ts +277 -0
- package/core/localpay/sessionPolicy.ts +5 -0
- package/core/localpay/transport/awdl.ts +11 -0
- package/core/localpay/transport/ble.ts +159 -0
- package/core/localpay/transport/nearby.ts +12 -0
- package/core/localpay/transport/race.ts +110 -0
- package/core/localpay/transport/select.ts +128 -0
- package/core/localpay/transport/socket.ts +244 -0
- package/core/localpay/types.ts +82 -0
- package/core/localpay/verify.ts +204 -0
- package/core/logging.config.ts +10 -0
- package/core/logging.ts +77 -0
- package/core/mnemonicWallet.ts +180 -0
- package/core/monitor/TaskBackupPush.ts +168 -0
- package/core/monitor/TaskCreditInbox.ts +116 -0
- package/core/monitor/TaskDrainOutbox.ts +122 -0
- package/core/monitor/TaskSendOffline.ts +123 -0
- package/core/monitor/unfailRetry.ts +116 -0
- package/core/net/chainMatch.ts +34 -0
- package/core/net/online.ts +34 -0
- package/core/offline/cancelParked.ts +56 -0
- package/core/offline/hold.ts +127 -0
- package/core/offline/order.ts +109 -0
- package/core/offline/payerHold.ts +144 -0
- package/core/offline/plan.ts +264 -0
- package/core/offline/queueGrace.ts +52 -0
- package/core/parsePeerPayURI.ts +119 -0
- package/core/pay/beefRepair.ts +106 -0
- package/core/pay/creditErrors.ts +46 -0
- package/core/pay/creditInbox.ts +202 -0
- package/core/pay/damagedInbox.ts +64 -0
- package/core/pay/proofNudge.ts +23 -0
- package/core/pay/provenTxFromBump.ts +31 -0
- package/core/pay/rails/address.ts +329 -0
- package/core/pay/rails/handle.ts +617 -0
- package/core/pay/rails/index.ts +197 -0
- package/core/pay/rails/nearby.ts +79 -0
- package/core/pay/receiveFocus.ts +13 -0
- package/core/pay/recordProof.ts +91 -0
- package/core/pay/refreshProofGuard.ts +34 -0
- package/core/pay/sweeper.ts +75 -0
- package/core/pay/tokenAmount.ts +18 -0
- package/core/pay/userError.ts +18 -0
- package/core/pay/watchlist.ts +126 -0
- package/core/peerpay/control.ts +122 -0
- package/core/peerpay/handleResendRequests.ts +475 -0
- package/core/peerpay/inboxAttempts.ts +33 -0
- package/core/peerpay/metadataDecryptor.ts +42 -0
- package/core/peerpay/offlineNacks.ts +96 -0
- package/core/peerpay/outbox.ts +209 -0
- package/core/peerpay/rebuildToken.ts +76 -0
- package/core/peerpay/resendBeef.ts +115 -0
- package/core/services/arcadeBroadcastProvider.ts +214 -0
- package/core/services/capWalletArgs.ts +99 -0
- package/core/services/exchangeRate.ts +63 -0
- package/core/services/secrets/envelope.ts +102 -0
- package/core/services/secrets/hkdf.ts +37 -0
- package/core/services/secrets/index.ts +63 -0
- package/core/services/secrets/kek.ts +274 -0
- package/core/services/secrets/migration.ts +188 -0
- package/core/services/secrets/policy.ts +120 -0
- package/core/services/secrets/storage.ts +54 -0
- package/core/services/secrets/store.ts +125 -0
- package/core/services/secrets/types.ts +85 -0
- package/core/services/usdFxRates.ts +101 -0
- package/core/services/vault/VaultKeyService.ts +312 -0
- package/core/services/vault/backupAttestation.ts +136 -0
- package/core/services/vault/ceremony.ts +972 -0
- package/core/services/vault/ceremonyHost.ts +46 -0
- package/core/services/vault/devMock.ts +22 -0
- package/core/services/vault/driver.ts +197 -0
- package/core/services/vault/guard.ts +90 -0
- package/core/services/vault/k1.ts +75 -0
- package/core/services/vault/mockYubiKey.ts +217 -0
- package/core/services/vault/random.ts +20 -0
- package/core/services/vault/sealing.ts +84 -0
- package/core/services/vault/session.ts +59 -0
- package/core/services/vault/transfers.ts +1151 -0
- package/core/services/vault/types.ts +109 -0
- package/core/services/vault/vaultDerivation.ts +152 -0
- package/core/services/vault/vaultPassphrase.ts +287 -0
- package/core/services/vault/vaultStore.ts +104 -0
- package/core/services/walletArgLimits.ts +234 -0
- package/core/services/walletServiceConfig.ts +170 -0
- package/core/storage/LocalStorageAdapter.ts +58 -0
- package/core/storage/README.md +391 -0
- package/core/storage/StorageExpoSQLite.ts +2008 -0
- package/core/storage/errors.ts +73 -0
- package/core/storage/index.ts +3 -0
- package/core/storage/methods/findSql.ts +286 -0
- package/core/storage/methods/historyNotes.ts +61 -0
- package/core/storage/methods/listActionsSql.ts +212 -0
- package/core/storage/methods/listOutputsSql.ts +211 -0
- package/core/storage/methods/offlineActions.ts +156 -0
- package/core/storage/methods/processOfflineActions.ts +503 -0
- package/core/storage/methods/reclaim.ts +135 -0
- package/core/storage/methods/reviewStatusSql.ts +167 -0
- package/core/storage/methods/unprovenWithoutReqSql.ts +26 -0
- package/core/storage/methods/walletBalanceSql.ts +55 -0
- package/core/storage/schema/createTables.ts +385 -0
- package/core/storage/skipQueuedAncestors.ts +29 -0
- package/core/storage/sqlUpdateValue.ts +12 -0
- package/core/stores/ConnectionStore.ts +71 -0
- package/core/theme/ThemeContext.tsx +182 -0
- package/core/theme/assertThemeContrast.ts +109 -0
- package/core/theme/motion.ts +50 -0
- package/core/theme/tokens.ts +232 -0
- package/core/theme/useThemeStyles.tsx +156 -0
- package/core/walletDbRegistry.ts +110 -0
- package/core/walletMonitor.ts +149 -0
- package/core/walletRepair/runWalletCheck.ts +195 -0
- package/core/walletRepair/shouldReleaseUtxo.ts +18 -0
- package/package.json +86 -0
- package/ui/backupShares.ts +367 -0
- package/ui/components/QRScanner.tsx +353 -0
- package/ui/components/pay/AddressReceive.tsx +507 -0
- package/ui/components/pay/AvailableBalance.tsx +61 -0
- package/ui/components/pay/HandleReceive.tsx +896 -0
- package/ui/components/pay/MessageBoxConfig.tsx +256 -0
- package/ui/components/pay/NearbyAdvisoryModal.tsx +111 -0
- package/ui/components/pay/NearbyFlow.tsx +2542 -0
- package/ui/components/pay/OfflineNotice.tsx +337 -0
- package/ui/components/pay/PayCellRow.tsx +101 -0
- package/ui/components/pay/PayForm.tsx +223 -0
- package/ui/components/pay/PaymentQrDisplay.tsx +105 -0
- package/ui/components/pay/PaymentSuccessOverlay.tsx +266 -0
- package/ui/components/pay/RecipientField.tsx +308 -0
- package/ui/components/pay/RequestHub.tsx +75 -0
- package/ui/components/pay/ResultBanner.tsx +69 -0
- package/ui/components/pay/UniversalSend.tsx +687 -0
- package/ui/components/pay/useRecipientInput.ts +270 -0
- package/ui/components/security/WalletLockNotice.tsx +138 -0
- package/ui/components/ui/AlertCard.tsx +223 -0
- package/ui/components/ui/AppLogo.tsx +166 -0
- package/ui/components/ui/BlurChrome.tsx +72 -0
- package/ui/components/ui/Celebration.tsx +84 -0
- package/ui/components/ui/ChoiceSheet.ts +69 -0
- package/ui/components/ui/CustomSafeArea.tsx +74 -0
- package/ui/components/ui/ErrorBoundary.tsx +154 -0
- package/ui/components/ui/GroupedList.tsx +125 -0
- package/ui/components/ui/IconButton.tsx +105 -0
- package/ui/components/ui/ListRow.tsx +197 -0
- package/ui/components/ui/PermissionSheet.tsx +920 -0
- package/ui/components/ui/PresenceRow.tsx +215 -0
- package/ui/components/ui/PressableScale.tsx +105 -0
- package/ui/components/ui/ScreenGradient.tsx +47 -0
- package/ui/components/ui/ScrollFade.tsx +76 -0
- package/ui/components/ui/Sheet.tsx +342 -0
- package/ui/components/ui/Toast.tsx +142 -0
- package/ui/components/ui/index.ts +8 -0
- package/ui/components/vault/EnrollWizard.tsx +627 -0
- package/ui/components/vault/PassphraseField.tsx +306 -0
- package/ui/components/vault/PhraseBackupSheet.tsx +227 -0
- package/ui/components/vault/VaultBackdrop.tsx +354 -0
- package/ui/components/vault/VaultCeremonySheet.tsx +327 -0
- package/ui/components/wallet/ActivityRow.tsx +408 -0
- package/ui/components/wallet/AmountDisplay.tsx +48 -0
- package/ui/components/wallet/AmountInput.tsx +256 -0
- package/ui/components/wallet/BackupReminderSheet.tsx +168 -0
- package/ui/components/wallet/Balance.tsx +144 -0
- package/ui/components/wallet/BiometricAdvisoryModal.tsx +133 -0
- package/ui/components/wallet/ImportFromBackupPrompt.tsx +93 -0
- package/ui/exportDatabases.ts +122 -0
- package/ui/exportTransactions.ts +129 -0
- package/ui/hooks/useOfflineNoticeActions.ts +136 -0
- package/ui/hooks/useOnline.ts +70 -0
- package/ui/hooks/useSpendableBalance.ts +58 -0
- package/ui/hooks/useVaultBalance.ts +50 -0
- package/ui/importDatabases.ts +170 -0
- package/ui/index.ts +136 -0
- package/ui/isImageUrl.ts +10 -0
- package/ui/printRecoveryShares.ts +93 -0
- package/ui/resolveIdentity.ts +125 -0
- package/ui/screens/ConnectionsScreen.tsx +438 -0
- package/ui/screens/LogsScreen.tsx +432 -0
- package/ui/screens/PairScreen.tsx +288 -0
- package/ui/screens/PayScreen.tsx +479 -0
- package/ui/screens/SettingsScreen.tsx +180 -0
- package/ui/screens/TrustScreen.tsx +831 -0
- package/ui/screens/VaultRecoverScreen.tsx +226 -0
- package/ui/screens/VaultScreen.tsx +423 -0
- package/ui/screens/VaultTransferScreen.tsx +293 -0
- package/ui/screens/WalletCheckScreen.tsx +539 -0
- package/ui/screens/WalletConfigScreen.tsx +1095 -0
- package/ui/screens/WalletHomeScreen.tsx +1597 -0
- package/ui/screens/homeBadges.ts +18 -0
- package/ui/txStatus.ts +79 -0
- package/ui/validateTrust.ts +44 -0
package/package.json
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bsv/expo-wallet-toolbox",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Wallet screens and P2P local-payment backend for Expo/React Native apps, extracted from BSV Wallet.",
|
|
6
|
+
"license": "SEE LICENSE IN LICENSE.txt",
|
|
7
|
+
"author": "BSV Association",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/bsv-blockchain/bsv-wallet.git",
|
|
11
|
+
"directory": "packages/expo-wallet-toolbox"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"bsv",
|
|
15
|
+
"bitcoin",
|
|
16
|
+
"wallet",
|
|
17
|
+
"expo",
|
|
18
|
+
"react-native",
|
|
19
|
+
"p2p",
|
|
20
|
+
"local-payment",
|
|
21
|
+
"peerpay"
|
|
22
|
+
],
|
|
23
|
+
"main": "core/index.ts",
|
|
24
|
+
"types": "core/index.ts",
|
|
25
|
+
"files": ["core", "ui", "assets"],
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"react": "^19.2.0",
|
|
28
|
+
"react-native": "^0.83.6",
|
|
29
|
+
"expo-router": "~55.0.16",
|
|
30
|
+
|
|
31
|
+
"@bsv/sdk": "^2.4.1",
|
|
32
|
+
"@bsv/wallet-toolbox-mobile": "^2.4.3",
|
|
33
|
+
"@bsv/btms-permission-module": "^1.1.0",
|
|
34
|
+
"@bsv/air-gap": "^0.1.1",
|
|
35
|
+
"@bsv/backup-cache-client": "^0.1.0",
|
|
36
|
+
"@bsv/message-box-client": "^2.2.1",
|
|
37
|
+
"@bsv/templates": "^1.10.0",
|
|
38
|
+
"@noble/curves": "^2.3.0",
|
|
39
|
+
"qrcode": "^1.5.4",
|
|
40
|
+
|
|
41
|
+
"expo-sqlite": "~55.0.16",
|
|
42
|
+
"expo-secure-store": "~55.0.14",
|
|
43
|
+
"expo-camera": "~55.0.19",
|
|
44
|
+
"expo-audio": "~55.0.16",
|
|
45
|
+
"expo-clipboard": "~55.0.13",
|
|
46
|
+
"expo-crypto": "~55.0.15",
|
|
47
|
+
"expo-device": "~55.0.17",
|
|
48
|
+
"expo-document-picker": "~55.0.13",
|
|
49
|
+
"expo-file-system": "~55.0.20",
|
|
50
|
+
"expo-haptics": "~55.0.14",
|
|
51
|
+
"expo-local-authentication": "~55.0.14",
|
|
52
|
+
"expo-localization": "~55.0.15",
|
|
53
|
+
"expo-print": "~55.0.15",
|
|
54
|
+
"expo-sharing": "~55.0.19",
|
|
55
|
+
"expo-status-bar": "~55.0.6",
|
|
56
|
+
"expo-blur": "~55.0.14",
|
|
57
|
+
"@expo/vector-icons": "^15.0.3",
|
|
58
|
+
|
|
59
|
+
"@react-native-async-storage/async-storage": "^2.2.0",
|
|
60
|
+
"@react-native-clipboard/clipboard": "^1.16.2",
|
|
61
|
+
"@react-native-community/netinfo": "^11.5.2",
|
|
62
|
+
"react-native-gesture-handler": "~2.30.0",
|
|
63
|
+
"react-native-localize": "^3.4.1",
|
|
64
|
+
"react-native-qrcode-svg": "^6.3.15",
|
|
65
|
+
"react-native-reanimated": "^4.2.1",
|
|
66
|
+
"react-native-safe-area-context": "~5.6.0",
|
|
67
|
+
"react-native-svg": "^15.15.3",
|
|
68
|
+
"react-native-sse": "^1.2.1",
|
|
69
|
+
|
|
70
|
+
"mobx": "^6.13.7",
|
|
71
|
+
"mobx-react-lite": "^4.1.0",
|
|
72
|
+
"i18next": "^25.2.1",
|
|
73
|
+
"react-i18next": "^15.5.3",
|
|
74
|
+
"date-fns": "^4.1.0",
|
|
75
|
+
|
|
76
|
+
"react-native-nitro-modules": "^0.35.2",
|
|
77
|
+
"react-native-localpay-transport": "*",
|
|
78
|
+
"react-native-engine-native": "*",
|
|
79
|
+
"react-native-secp-native": "*",
|
|
80
|
+
|
|
81
|
+
"react-native-yubikey": "*"
|
|
82
|
+
},
|
|
83
|
+
"peerDependenciesMeta": {
|
|
84
|
+
"react-native-yubikey": { "optional": true }
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Backup shares — Shamir 2-of-3 recovery paper.
|
|
3
|
+
*
|
|
4
|
+
* The secret being split is the MNEMONIC ENTROPY, per BRC-157, not the primary
|
|
5
|
+
* key at m/0'/0'. That derivation is hardened and one-way, so shares of it can
|
|
6
|
+
* restore spending authority but can never rebuild the phrase — and the vault
|
|
7
|
+
* key needs the phrase. Splitting the entropy makes paper and phrase two
|
|
8
|
+
* encodings of one secret.
|
|
9
|
+
*
|
|
10
|
+
* The split payload is always exactly 32 bytes:
|
|
11
|
+
*
|
|
12
|
+
* entropy(16) || sha256(entropy)[0..16]
|
|
13
|
+
*
|
|
14
|
+
* The tag exists so recovery can tell new paper from old WITHOUT a version
|
|
15
|
+
* marker on the printed page, which would break every sheet already in a
|
|
16
|
+
* drawer. Length cannot do that job: PrivateKey is a BigNumber and drops
|
|
17
|
+
* leading zero bytes (~1 payload in 256), and an imported 24-word phrase
|
|
18
|
+
* yields 32 bytes of entropy — the exact width of a legacy primary key.
|
|
19
|
+
*
|
|
20
|
+
* Do NOT try to validate the entropy branch by rebuilding a mnemonic and
|
|
21
|
+
* checking its BIP39 checksum. Mnemonic.fromEntropy COMPUTES that checksum, so
|
|
22
|
+
* it accepts any 16 bytes and can never reject a misclassification.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { PrivateKey, Hash } from '@bsv/sdk'
|
|
26
|
+
import QRCode from 'qrcode'
|
|
27
|
+
|
|
28
|
+
// ── Payload framing ──────────────────────────────────────────────────────────
|
|
29
|
+
|
|
30
|
+
/** Entropy of a 12-word BIP39 phrase. The only width v2 shares support. */
|
|
31
|
+
export const ENTROPY_BYTES = 16
|
|
32
|
+
/** Fixed width of the split secret. Never varies, never inferred. */
|
|
33
|
+
export const PAYLOAD_BYTES = 32
|
|
34
|
+
|
|
35
|
+
/** What a recombined payload turned out to be. */
|
|
36
|
+
export type RecoveredSecret =
|
|
37
|
+
| { kind: 'entropy'; entropy: number[] }
|
|
38
|
+
| { kind: 'legacy'; primaryKey: number[] }
|
|
39
|
+
|
|
40
|
+
/** Wrap 16 bytes of entropy in the tagged 32-byte payload. */
|
|
41
|
+
export function frameEntropy(entropy: number[]): number[] {
|
|
42
|
+
if (entropy.length !== ENTROPY_BYTES) {
|
|
43
|
+
throw new Error(`frameEntropy: expected ${ENTROPY_BYTES} bytes, got ${entropy.length}`)
|
|
44
|
+
}
|
|
45
|
+
return [...entropy, ...Hash.sha256(entropy).slice(0, PAYLOAD_BYTES - ENTROPY_BYTES)]
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Restore a recombined payload to full width.
|
|
50
|
+
*
|
|
51
|
+
* Shamir recombination yields a PrivateKey, whose toArray() drops leading zero
|
|
52
|
+
* bytes. Without this pad, roughly one payload in 256 decodes short and fails
|
|
53
|
+
* to match anything.
|
|
54
|
+
*/
|
|
55
|
+
export function padPayload(bytes: number[]): number[] {
|
|
56
|
+
if (bytes.length > PAYLOAD_BYTES) {
|
|
57
|
+
throw new Error(`padPayload: payload exceeds ${PAYLOAD_BYTES} bytes`)
|
|
58
|
+
}
|
|
59
|
+
return [...new Array(PAYLOAD_BYTES - bytes.length).fill(0), ...bytes]
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Decide whether a recombined payload is framed entropy or a legacy key. */
|
|
63
|
+
export function classifyPayload(raw: number[]): RecoveredSecret {
|
|
64
|
+
const payload = padPayload(raw)
|
|
65
|
+
const entropy = payload.slice(0, ENTROPY_BYTES)
|
|
66
|
+
const tag = payload.slice(ENTROPY_BYTES)
|
|
67
|
+
const expected = Hash.sha256(entropy).slice(0, PAYLOAD_BYTES - ENTROPY_BYTES)
|
|
68
|
+
|
|
69
|
+
return tag.every((b, i) => b === expected[i])
|
|
70
|
+
? { kind: 'entropy', entropy }
|
|
71
|
+
: { kind: 'legacy', primaryKey: payload }
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// ── Share generation ─────────────────────────────────────────────────────────
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Split mnemonic entropy into backup shares (the current format).
|
|
78
|
+
* @param entropy 16 bytes, from Mnemonic.toEntropy()
|
|
79
|
+
* @returns Share strings in the format base58(x).base58(y).threshold.integrity
|
|
80
|
+
*/
|
|
81
|
+
export function generateEntropyShares(
|
|
82
|
+
entropy: number[],
|
|
83
|
+
threshold: number = 2,
|
|
84
|
+
totalShares: number = 3
|
|
85
|
+
): string[] {
|
|
86
|
+
return new PrivateKey(frameEntropy(entropy)).toBackupShares(threshold, totalShares)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Split a raw private key (the legacy format).
|
|
91
|
+
*
|
|
92
|
+
* Still reachable for wallets that were themselves restored from legacy paper
|
|
93
|
+
* and therefore have no mnemonic to frame. Removing it would leave that cohort
|
|
94
|
+
* with no way to back up at all; the tag check routes their shares back to the
|
|
95
|
+
* legacy branch on recovery, so this stays self-consistent.
|
|
96
|
+
*/
|
|
97
|
+
export function generateLegacyKeyShares(
|
|
98
|
+
privateKeyBytes: number[],
|
|
99
|
+
threshold: number = 2,
|
|
100
|
+
totalShares: number = 3
|
|
101
|
+
): string[] {
|
|
102
|
+
return new PrivateKey(privateKeyBytes).toBackupShares(threshold, totalShares)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ── Share validation ─────────────────────────────────────────────────────────
|
|
106
|
+
|
|
107
|
+
export interface ParsedShare {
|
|
108
|
+
raw: string
|
|
109
|
+
x: string
|
|
110
|
+
y: string
|
|
111
|
+
threshold: number
|
|
112
|
+
integrity: string
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Parse and validate a single backup share string.
|
|
117
|
+
* @returns Parsed share or null if invalid format
|
|
118
|
+
*/
|
|
119
|
+
export function parseShare(shareString: string): ParsedShare | null {
|
|
120
|
+
const parts = shareString.trim().split('.')
|
|
121
|
+
if (parts.length !== 4) return null
|
|
122
|
+
|
|
123
|
+
const [x, y, thresholdStr, integrity] = parts
|
|
124
|
+
const threshold = Number(thresholdStr)
|
|
125
|
+
|
|
126
|
+
if (!x || !y || isNaN(threshold) || threshold < 2 || !integrity) return null
|
|
127
|
+
|
|
128
|
+
return { raw: shareString.trim(), x, y, threshold, integrity }
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Validate that a new share is compatible with previously collected shares.
|
|
133
|
+
* @returns Error message string or null if valid
|
|
134
|
+
*/
|
|
135
|
+
export function validateShareCompatibility(newShare: ParsedShare, existingShares: ParsedShare[]): string | null {
|
|
136
|
+
if (existingShares.length === 0) return null
|
|
137
|
+
|
|
138
|
+
const first = existingShares[0]
|
|
139
|
+
|
|
140
|
+
if (newShare.threshold !== first.threshold) {
|
|
141
|
+
return 'Threshold does not match previous shares'
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (newShare.integrity !== first.integrity) {
|
|
145
|
+
return 'Integrity hash does not match — shares are from different keys'
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Check for duplicate (same x.y point)
|
|
149
|
+
const isDuplicate = existingShares.some(s => s.x === newShare.x && s.y === newShare.y)
|
|
150
|
+
if (isDuplicate) {
|
|
151
|
+
return 'This share has already been scanned'
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return null
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Recombine shares and say what came out.
|
|
159
|
+
* @param shareStrings At least `threshold` raw share strings
|
|
160
|
+
* @throws If the shares are invalid or their integrity hashes disagree
|
|
161
|
+
*/
|
|
162
|
+
export function recoverSecretFromShares(shareStrings: string[]): RecoveredSecret {
|
|
163
|
+
return classifyPayload(Array.from(PrivateKey.fromBackupShares(shareStrings).toArray()))
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// ── Print HTML generation ────────────────────────────────────────────────────
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Generate a QR code as an inline SVG string.
|
|
170
|
+
* Uses the `qrcode` package which does pure-JS SVG string generation.
|
|
171
|
+
*/
|
|
172
|
+
async function generateQRCodeSVG(data: string, size: number = 180): Promise<string> {
|
|
173
|
+
const svgString = await QRCode.toString(data, {
|
|
174
|
+
type: 'svg',
|
|
175
|
+
width: size,
|
|
176
|
+
margin: 1,
|
|
177
|
+
errorCorrectionLevel: 'M'
|
|
178
|
+
})
|
|
179
|
+
return svgString
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Generate printable HTML with one page per backup share.
|
|
184
|
+
*
|
|
185
|
+
* Layout matches the reference implementation (secure-key-backup-and-recovery):
|
|
186
|
+
* - Header: "Share N of M" + date stamp
|
|
187
|
+
* - Share QR code + share text
|
|
188
|
+
* - Identity Key QR code + identity key text
|
|
189
|
+
* - Recovery instructions footer
|
|
190
|
+
*
|
|
191
|
+
* Pages are separated by CSS page-break-after for print dialogue.
|
|
192
|
+
*/
|
|
193
|
+
export async function generatePrintHTML(
|
|
194
|
+
shares: string[],
|
|
195
|
+
identityKey: string,
|
|
196
|
+
format: 'entropy' | 'legacy' = 'entropy',
|
|
197
|
+
appName: string = 'your wallet app'
|
|
198
|
+
): Promise<string> {
|
|
199
|
+
const now = new Date()
|
|
200
|
+
const date = now.toISOString().split('T')[0]
|
|
201
|
+
const time = now.toISOString().split('T')[1].split('.')[0]
|
|
202
|
+
const dateStamp = `${date} ${time}`
|
|
203
|
+
|
|
204
|
+
// Pre-generate all QR codes
|
|
205
|
+
const shareQRs = await Promise.all(shares.map(s => generateQRCodeSVG(s, 180)))
|
|
206
|
+
const identityQR = await generateQRCodeSVG(identityKey, 150)
|
|
207
|
+
|
|
208
|
+
const pages = shares.map(
|
|
209
|
+
(share, i) => `
|
|
210
|
+
<div class="page${i < shares.length - 1 ? '' : ' last'}">
|
|
211
|
+
<div class="header">
|
|
212
|
+
<span class="share-label">Share ${i + 1} of ${shares.length}</span>
|
|
213
|
+
<span class="date-stamp">${dateStamp}</span>
|
|
214
|
+
</div>
|
|
215
|
+
|
|
216
|
+
<div class="section">
|
|
217
|
+
<div class="qr-container identity-qr">
|
|
218
|
+
${identityQR}
|
|
219
|
+
</div>
|
|
220
|
+
<div class="data-label">Identity Key</div>
|
|
221
|
+
<div class="data-value">${identityKey}</div>
|
|
222
|
+
<div class="identity-caption">Scan this QR code to send BSV payments to this wallet.</div>
|
|
223
|
+
</div>
|
|
224
|
+
|
|
225
|
+
<div class="divider"></div>
|
|
226
|
+
|
|
227
|
+
<div class="section">
|
|
228
|
+
<div class="qr-container">
|
|
229
|
+
${shareQRs[i]}
|
|
230
|
+
</div>
|
|
231
|
+
<div class="data-label">Backup Share</div>
|
|
232
|
+
<div class="data-value share-text">${share}</div>
|
|
233
|
+
</div>
|
|
234
|
+
|
|
235
|
+
<div class="divider"></div>
|
|
236
|
+
|
|
237
|
+
<div class="instructions">
|
|
238
|
+
<strong>Recovery Instructions</strong>
|
|
239
|
+
<p>This is 1 of ${shares.length} backup shares. You need any ${shares[0].split('.')[2]} shares to recover your wallet.</p>
|
|
240
|
+
<p>Store each share in a separate, secure location. Do not store shares together.</p>
|
|
241
|
+
<p>${
|
|
242
|
+
format === 'entropy'
|
|
243
|
+
? 'Any two of these pages rebuild your twelve-word recovery phrase, and therefore your entire wallet — everyday balance and vault alike. Treat two pages together as you would the phrase itself.'
|
|
244
|
+
: 'These shares are an older format. They restore your everyday balance but cannot open a vault.'
|
|
245
|
+
}</p>
|
|
246
|
+
<p>To recover: In ${appName}, go to Import Existing Wallet → Scan Backup Shares.</p>
|
|
247
|
+
</div>
|
|
248
|
+
</div>
|
|
249
|
+
`
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
return `
|
|
253
|
+
<!DOCTYPE html>
|
|
254
|
+
<html>
|
|
255
|
+
<head>
|
|
256
|
+
<meta charset="utf-8">
|
|
257
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
258
|
+
<style>
|
|
259
|
+
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
260
|
+
|
|
261
|
+
body {
|
|
262
|
+
font-family: 'Courier New', Courier, monospace;
|
|
263
|
+
font-weight: 700;
|
|
264
|
+
color: #000;
|
|
265
|
+
background: #fff;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
.page {
|
|
269
|
+
width: 100%;
|
|
270
|
+
padding: 12mm 15mm;
|
|
271
|
+
page-break-after: always;
|
|
272
|
+
}
|
|
273
|
+
.page.last {
|
|
274
|
+
page-break-after: auto;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
.header {
|
|
278
|
+
display: flex;
|
|
279
|
+
justify-content: space-between;
|
|
280
|
+
align-items: baseline;
|
|
281
|
+
margin-bottom: 6mm;
|
|
282
|
+
padding-bottom: 3mm;
|
|
283
|
+
border-bottom: 1px solid #ccc;
|
|
284
|
+
}
|
|
285
|
+
.share-label {
|
|
286
|
+
font-size: 16pt;
|
|
287
|
+
font-weight: 700;
|
|
288
|
+
}
|
|
289
|
+
.date-stamp {
|
|
290
|
+
font-size: 9pt;
|
|
291
|
+
color: #444;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
.section {
|
|
295
|
+
margin-bottom: 4mm;
|
|
296
|
+
}
|
|
297
|
+
.qr-container {
|
|
298
|
+
margin-bottom: 3mm;
|
|
299
|
+
}
|
|
300
|
+
.qr-container svg {
|
|
301
|
+
width: 50mm;
|
|
302
|
+
height: 50mm;
|
|
303
|
+
}
|
|
304
|
+
.identity-qr svg {
|
|
305
|
+
width: 40mm;
|
|
306
|
+
height: 40mm;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
.data-label {
|
|
310
|
+
font-size: 11pt;
|
|
311
|
+
font-weight: 700;
|
|
312
|
+
color: #000;
|
|
313
|
+
margin-bottom: 1.5mm;
|
|
314
|
+
}
|
|
315
|
+
.data-value {
|
|
316
|
+
font-size: 7pt;
|
|
317
|
+
font-weight: 700;
|
|
318
|
+
word-break: break-all;
|
|
319
|
+
line-height: 1.4;
|
|
320
|
+
color: #000;
|
|
321
|
+
}
|
|
322
|
+
.share-text {
|
|
323
|
+
font-size: 8.2pt;
|
|
324
|
+
word-break: normal;
|
|
325
|
+
white-space: nowrap;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
.identity-caption {
|
|
329
|
+
margin-top: 2mm;
|
|
330
|
+
font-size: 9pt;
|
|
331
|
+
font-weight: 700;
|
|
332
|
+
color: #000;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
.divider {
|
|
336
|
+
border-top: 1px solid #e0e0e0;
|
|
337
|
+
margin: 4mm 0;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
.instructions {
|
|
341
|
+
margin-top: 4mm;
|
|
342
|
+
font-size: 10pt;
|
|
343
|
+
font-weight: 700;
|
|
344
|
+
line-height: 1.6;
|
|
345
|
+
color: #000;
|
|
346
|
+
}
|
|
347
|
+
.instructions strong {
|
|
348
|
+
display: block;
|
|
349
|
+
font-size: 11pt;
|
|
350
|
+
margin-bottom: 2mm;
|
|
351
|
+
}
|
|
352
|
+
.instructions p {
|
|
353
|
+
margin-bottom: 1.5mm;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
@media print {
|
|
357
|
+
body { background: #fff; }
|
|
358
|
+
.page { padding: 10mm 12mm; }
|
|
359
|
+
}
|
|
360
|
+
</style>
|
|
361
|
+
</head>
|
|
362
|
+
<body>
|
|
363
|
+
${pages.join('\n')}
|
|
364
|
+
</body>
|
|
365
|
+
</html>
|
|
366
|
+
`
|
|
367
|
+
}
|