@arkade-os/sdk 0.4.27 → 0.4.28
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/README.md +31 -100
- package/dist/adapters/asyncStorage.cjs +48 -0
- package/dist/adapters/asyncStorage.cjs.map +1 -0
- package/dist/adapters/asyncStorage.d.cts +16 -0
- package/dist/{types/storage → adapters}/asyncStorage.d.ts +5 -2
- package/dist/adapters/asyncStorage.js +46 -0
- package/dist/adapters/asyncStorage.js.map +1 -0
- package/dist/adapters/expo.cjs +19 -0
- package/dist/adapters/expo.cjs.map +1 -0
- package/dist/adapters/expo.d.cts +48 -0
- package/dist/adapters/expo.d.ts +48 -0
- package/dist/adapters/expo.js +6 -0
- package/dist/adapters/expo.js.map +1 -0
- package/dist/adapters/fileSystem.cjs +116 -0
- package/dist/adapters/fileSystem.cjs.map +1 -0
- package/dist/adapters/fileSystem.d.cts +17 -0
- package/dist/{types/storage → adapters}/fileSystem.d.ts +5 -2
- package/dist/adapters/fileSystem.js +93 -0
- package/dist/adapters/fileSystem.js.map +1 -0
- package/dist/adapters/indexedDB.cjs +103 -0
- package/dist/adapters/indexedDB.cjs.map +1 -0
- package/dist/adapters/indexedDB.d.cts +18 -0
- package/dist/{types/storage → adapters}/indexedDB.d.ts +5 -2
- package/dist/adapters/indexedDB.js +101 -0
- package/dist/adapters/indexedDB.js.map +1 -0
- package/dist/adapters/localStorage.cjs +50 -0
- package/dist/adapters/localStorage.cjs.map +1 -0
- package/dist/{types/storage/inMemory.d.ts → adapters/localStorage.d.cts} +6 -3
- package/dist/{types/storage → adapters}/localStorage.d.ts +5 -2
- package/dist/adapters/localStorage.js +48 -0
- package/dist/adapters/localStorage.js.map +1 -0
- package/dist/ark-TZ1gXAXU.d.cts +3880 -0
- package/dist/ark-TZ1gXAXU.d.ts +3880 -0
- package/dist/{types/worker/expo/asyncStorageTaskQueue.d.ts → asyncStorageTaskQueue-Cb1F_Z9s.d.ts} +6 -3
- package/dist/asyncStorageTaskQueue-EFqSmYTg.d.cts +49 -0
- package/dist/chunk-5BLDMQED.cjs +18 -0
- package/dist/chunk-5BLDMQED.cjs.map +1 -0
- package/dist/chunk-5PG7DV7A.cjs +805 -0
- package/dist/chunk-5PG7DV7A.cjs.map +1 -0
- package/dist/chunk-A3EMF7RN.js +95 -0
- package/dist/chunk-A3EMF7RN.js.map +1 -0
- package/dist/chunk-ADV27S4N.cjs +2701 -0
- package/dist/chunk-ADV27S4N.cjs.map +1 -0
- package/dist/chunk-BQLHADL7.js +13805 -0
- package/dist/chunk-BQLHADL7.js.map +1 -0
- package/dist/chunk-CFZMTDWI.js +209 -0
- package/dist/chunk-CFZMTDWI.js.map +1 -0
- package/dist/chunk-FG5ACJJW.cjs +212 -0
- package/dist/chunk-FG5ACJJW.cjs.map +1 -0
- package/dist/chunk-HW3JJ323.js +768 -0
- package/dist/chunk-HW3JJ323.js.map +1 -0
- package/dist/chunk-I3DGUUCT.cjs +838 -0
- package/dist/chunk-I3DGUUCT.cjs.map +1 -0
- package/dist/chunk-IPX2R7FR.cjs +100 -0
- package/dist/chunk-IPX2R7FR.cjs.map +1 -0
- package/dist/chunk-NSBPE2FW.js +15 -0
- package/dist/chunk-NSBPE2FW.js.map +1 -0
- package/dist/chunk-T64LAI7L.js +829 -0
- package/dist/chunk-T64LAI7L.js.map +1 -0
- package/dist/chunk-ZBUDLTBO.js +2671 -0
- package/dist/chunk-ZBUDLTBO.js.map +1 -0
- package/dist/chunk-ZLO6NETT.cjs +13910 -0
- package/dist/chunk-ZLO6NETT.cjs.map +1 -0
- package/dist/contracts/handlers/index.cjs +26 -0
- package/dist/contracts/handlers/index.cjs.map +1 -0
- package/dist/contracts/handlers/index.d.cts +7 -0
- package/dist/contracts/handlers/index.d.ts +7 -0
- package/dist/contracts/handlers/index.js +5 -0
- package/dist/contracts/handlers/index.js.map +1 -0
- package/dist/delegate-BFZs69hp.d.cts +84 -0
- package/dist/delegate-aaVGfWsV.d.ts +84 -0
- package/dist/index-B22cA64m.d.cts +199 -0
- package/dist/{types/storage/index.d.ts → index-C0IanN1m.d.cts} +3 -1
- package/dist/index-C0IanN1m.d.ts +11 -0
- package/dist/index-NDla_UoJ.d.ts +199 -0
- package/dist/index.cjs +480 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +3343 -0
- package/dist/index.d.ts +3343 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/repositories/realm/index.cjs +513 -0
- package/dist/repositories/realm/index.cjs.map +1 -0
- package/dist/repositories/realm/index.d.cts +217 -0
- package/dist/{types/repositories/realm/schemas.d.ts → repositories/realm/index.d.ts} +80 -112
- package/dist/repositories/realm/index.js +507 -0
- package/dist/repositories/realm/index.js.map +1 -0
- package/dist/repositories/sqlite/index.cjs +588 -0
- package/dist/repositories/sqlite/index.cjs.map +1 -0
- package/dist/repositories/sqlite/index.d.cts +118 -0
- package/dist/{types/repositories/sqlite/walletRepository.d.ts → repositories/sqlite/index.d.ts} +58 -5
- package/dist/repositories/sqlite/index.js +585 -0
- package/dist/repositories/sqlite/index.js.map +1 -0
- package/dist/taskRunner-C6Ff4OaU.d.cts +114 -0
- package/dist/taskRunner-yvPN8Z0K.d.ts +114 -0
- package/dist/wallet/expo/background.cjs +93 -0
- package/dist/wallet/expo/background.cjs.map +1 -0
- package/dist/wallet/expo/background.d.cts +84 -0
- package/dist/wallet/expo/background.d.ts +84 -0
- package/dist/wallet/expo/background.js +68 -0
- package/dist/wallet/expo/background.js.map +1 -0
- package/dist/wallet/expo/index.cjs +171 -0
- package/dist/wallet/expo/index.cjs.map +1 -0
- package/dist/wallet/expo/index.d.cts +122 -0
- package/dist/{types/wallet/expo/wallet.d.ts → wallet/expo/index.d.ts} +45 -22
- package/dist/wallet/expo/index.js +169 -0
- package/dist/wallet/expo/index.js.map +1 -0
- package/dist/wallet-AF-p-OWj.d.cts +774 -0
- package/dist/wallet-D9NBRqvC.d.ts +774 -0
- package/dist/worker/expo/index.cjs +140 -0
- package/dist/worker/expo/index.cjs.map +1 -0
- package/dist/worker/expo/index.d.cts +29 -0
- package/dist/worker/expo/index.d.ts +29 -0
- package/dist/worker/expo/index.js +121 -0
- package/dist/worker/expo/index.js.map +1 -0
- package/package.json +110 -76
- package/dist/cjs/adapters/asyncStorage.js +0 -5
- package/dist/cjs/adapters/expo.js +0 -8
- package/dist/cjs/adapters/fileSystem.js +0 -5
- package/dist/cjs/adapters/indexedDB.js +0 -5
- package/dist/cjs/adapters/localStorage.js +0 -5
- package/dist/cjs/arkfee/celenv.js +0 -43
- package/dist/cjs/arkfee/estimator.js +0 -143
- package/dist/cjs/arkfee/index.js +0 -5
- package/dist/cjs/arkfee/types.js +0 -26
- package/dist/cjs/arknote/index.js +0 -128
- package/dist/cjs/bip322/index.js +0 -270
- package/dist/cjs/contracts/arkcontract.js +0 -147
- package/dist/cjs/contracts/contractManager.js +0 -649
- package/dist/cjs/contracts/contractWatcher.js +0 -598
- package/dist/cjs/contracts/handlers/default.js +0 -93
- package/dist/cjs/contracts/handlers/delegate.js +0 -90
- package/dist/cjs/contracts/handlers/helpers.js +0 -115
- package/dist/cjs/contracts/handlers/index.js +0 -19
- package/dist/cjs/contracts/handlers/registry.js +0 -89
- package/dist/cjs/contracts/handlers/vhtlc.js +0 -194
- package/dist/cjs/contracts/index.js +0 -41
- package/dist/cjs/contracts/types.js +0 -2
- package/dist/cjs/contracts/vtxoOwnership.js +0 -78
- package/dist/cjs/extension/asset/assetGroup.js +0 -228
- package/dist/cjs/extension/asset/assetId.js +0 -152
- package/dist/cjs/extension/asset/assetInput.js +0 -222
- package/dist/cjs/extension/asset/assetOutput.js +0 -174
- package/dist/cjs/extension/asset/assetRef.js +0 -148
- package/dist/cjs/extension/asset/index.js +0 -23
- package/dist/cjs/extension/asset/metadata.js +0 -187
- package/dist/cjs/extension/asset/packet.js +0 -114
- package/dist/cjs/extension/asset/types.js +0 -22
- package/dist/cjs/extension/asset/utils.js +0 -105
- package/dist/cjs/extension/index.js +0 -254
- package/dist/cjs/extension/packet.js +0 -20
- package/dist/cjs/forfeit.js +0 -45
- package/dist/cjs/identity/descriptor.js +0 -169
- package/dist/cjs/identity/descriptorProvider.js +0 -2
- package/dist/cjs/identity/hdCapableIdentity.js +0 -20
- package/dist/cjs/identity/index.js +0 -40
- package/dist/cjs/identity/seedIdentity.js +0 -477
- package/dist/cjs/identity/serialize.js +0 -171
- package/dist/cjs/identity/singleKey.js +0 -126
- package/dist/cjs/identity/staticDescriptorProvider.js +0 -65
- package/dist/cjs/index.js +0 -202
- package/dist/cjs/intent/index.js +0 -259
- package/dist/cjs/musig2/index.js +0 -11
- package/dist/cjs/musig2/keys.js +0 -57
- package/dist/cjs/musig2/nonces.js +0 -48
- package/dist/cjs/musig2/sign.js +0 -102
- package/dist/cjs/networks.js +0 -26
- package/dist/cjs/package.json +0 -3
- package/dist/cjs/providers/ark.js +0 -577
- package/dist/cjs/providers/delegator.js +0 -85
- package/dist/cjs/providers/electrum.js +0 -869
- package/dist/cjs/providers/errors.js +0 -59
- package/dist/cjs/providers/expoArk.js +0 -82
- package/dist/cjs/providers/expoIndexer.js +0 -111
- package/dist/cjs/providers/expoUtils.js +0 -124
- package/dist/cjs/providers/indexer.js +0 -630
- package/dist/cjs/providers/onchain.js +0 -262
- package/dist/cjs/providers/utils.js +0 -121
- package/dist/cjs/repositories/contractRepository.js +0 -2
- package/dist/cjs/repositories/inMemory/contractRepository.js +0 -55
- package/dist/cjs/repositories/inMemory/walletRepository.js +0 -115
- package/dist/cjs/repositories/index.js +0 -34
- package/dist/cjs/repositories/indexedDB/contractRepository.js +0 -187
- package/dist/cjs/repositories/indexedDB/db.js +0 -19
- package/dist/cjs/repositories/indexedDB/manager.js +0 -100
- package/dist/cjs/repositories/indexedDB/schema.js +0 -204
- package/dist/cjs/repositories/indexedDB/walletRepository.js +0 -474
- package/dist/cjs/repositories/indexedDB/websqlAdapter.js +0 -144
- package/dist/cjs/repositories/migrations/contractRepositoryImpl.js +0 -127
- package/dist/cjs/repositories/migrations/fromStorageAdapter.js +0 -66
- package/dist/cjs/repositories/migrations/walletRepositoryImpl.js +0 -184
- package/dist/cjs/repositories/realm/contractRepository.js +0 -116
- package/dist/cjs/repositories/realm/index.js +0 -11
- package/dist/cjs/repositories/realm/schemas.js +0 -157
- package/dist/cjs/repositories/realm/types.js +0 -7
- package/dist/cjs/repositories/realm/walletRepository.js +0 -305
- package/dist/cjs/repositories/scriptFromAddress.js +0 -16
- package/dist/cjs/repositories/serialization.js +0 -82
- package/dist/cjs/repositories/sqlite/contractRepository.js +0 -135
- package/dist/cjs/repositories/sqlite/index.js +0 -7
- package/dist/cjs/repositories/sqlite/types.js +0 -2
- package/dist/cjs/repositories/sqlite/walletRepository.js +0 -441
- package/dist/cjs/repositories/walletRepository.js +0 -2
- package/dist/cjs/script/address.js +0 -108
- package/dist/cjs/script/base.js +0 -185
- package/dist/cjs/script/default.js +0 -57
- package/dist/cjs/script/delegate.js +0 -53
- package/dist/cjs/script/tapscript.js +0 -619
- package/dist/cjs/script/vhtlc.js +0 -170
- package/dist/cjs/storage/asyncStorage.js +0 -50
- package/dist/cjs/storage/fileSystem.js +0 -141
- package/dist/cjs/storage/inMemory.js +0 -24
- package/dist/cjs/storage/index.js +0 -2
- package/dist/cjs/storage/indexedDB.js +0 -101
- package/dist/cjs/storage/localStorage.js +0 -51
- package/dist/cjs/tree/signingSession.js +0 -229
- package/dist/cjs/tree/txTree.js +0 -192
- package/dist/cjs/tree/validation.js +0 -107
- package/dist/cjs/utils/anchor.js +0 -35
- package/dist/cjs/utils/arkTransaction.js +0 -271
- package/dist/cjs/utils/bip21.js +0 -127
- package/dist/cjs/utils/syncCursors.js +0 -128
- package/dist/cjs/utils/timelock.js +0 -59
- package/dist/cjs/utils/transaction.js +0 -28
- package/dist/cjs/utils/transactionHistory.js +0 -183
- package/dist/cjs/utils/txSizeEstimator.js +0 -132
- package/dist/cjs/utils/unknownFields.js +0 -174
- package/dist/cjs/wallet/asset-manager.js +0 -330
- package/dist/cjs/wallet/asset.js +0 -119
- package/dist/cjs/wallet/batch.js +0 -183
- package/dist/cjs/wallet/delegator.js +0 -308
- package/dist/cjs/wallet/expo/background.js +0 -116
- package/dist/cjs/wallet/expo/index.js +0 -9
- package/dist/cjs/wallet/expo/wallet.js +0 -230
- package/dist/cjs/wallet/hdDescriptorProvider.js +0 -188
- package/dist/cjs/wallet/index.js +0 -82
- package/dist/cjs/wallet/inputSignerRouter.js +0 -98
- package/dist/cjs/wallet/onchain.js +0 -290
- package/dist/cjs/wallet/ramps.js +0 -216
- package/dist/cjs/wallet/serviceWorker/wallet-message-handler.js +0 -953
- package/dist/cjs/wallet/serviceWorker/wallet.js +0 -1174
- package/dist/cjs/wallet/signingErrors.js +0 -32
- package/dist/cjs/wallet/unroll.js +0 -293
- package/dist/cjs/wallet/utils.js +0 -111
- package/dist/cjs/wallet/validation.js +0 -154
- package/dist/cjs/wallet/vtxo-manager.js +0 -1142
- package/dist/cjs/wallet/wallet.js +0 -2195
- package/dist/cjs/wallet/walletReceiveRotator.js +0 -547
- package/dist/cjs/worker/browser/service-worker-manager.js +0 -183
- package/dist/cjs/worker/browser/utils.js +0 -67
- package/dist/cjs/worker/errors.js +0 -16
- package/dist/cjs/worker/expo/asyncStorageTaskQueue.js +0 -78
- package/dist/cjs/worker/expo/index.js +0 -13
- package/dist/cjs/worker/expo/processors/contractPollProcessor.js +0 -62
- package/dist/cjs/worker/expo/processors/index.js +0 -6
- package/dist/cjs/worker/expo/taskQueue.js +0 -41
- package/dist/cjs/worker/expo/taskRunner.js +0 -73
- package/dist/cjs/worker/messageBus.js +0 -474
- package/dist/esm/adapters/asyncStorage.js +0 -1
- package/dist/esm/adapters/expo.js +0 -3
- package/dist/esm/adapters/fileSystem.js +0 -1
- package/dist/esm/adapters/indexedDB.js +0 -1
- package/dist/esm/adapters/localStorage.js +0 -1
- package/dist/esm/arkfee/celenv.js +0 -40
- package/dist/esm/arkfee/estimator.js +0 -139
- package/dist/esm/arkfee/index.js +0 -1
- package/dist/esm/arkfee/types.js +0 -22
- package/dist/esm/arknote/index.js +0 -124
- package/dist/esm/bip322/index.js +0 -267
- package/dist/esm/contracts/arkcontract.js +0 -140
- package/dist/esm/contracts/contractManager.js +0 -645
- package/dist/esm/contracts/contractWatcher.js +0 -594
- package/dist/esm/contracts/handlers/default.js +0 -90
- package/dist/esm/contracts/handlers/delegate.js +0 -87
- package/dist/esm/contracts/handlers/helpers.js +0 -110
- package/dist/esm/contracts/handlers/index.js +0 -12
- package/dist/esm/contracts/handlers/registry.js +0 -86
- package/dist/esm/contracts/handlers/vhtlc.js +0 -191
- package/dist/esm/contracts/index.js +0 -13
- package/dist/esm/contracts/types.js +0 -1
- package/dist/esm/contracts/vtxoOwnership.js +0 -69
- package/dist/esm/extension/asset/assetGroup.js +0 -224
- package/dist/esm/extension/asset/assetId.js +0 -148
- package/dist/esm/extension/asset/assetInput.js +0 -217
- package/dist/esm/extension/asset/assetOutput.js +0 -169
- package/dist/esm/extension/asset/assetRef.js +0 -144
- package/dist/esm/extension/asset/index.js +0 -8
- package/dist/esm/extension/asset/metadata.js +0 -182
- package/dist/esm/extension/asset/packet.js +0 -110
- package/dist/esm/extension/asset/types.js +0 -19
- package/dist/esm/extension/asset/utils.js +0 -99
- package/dist/esm/extension/index.js +0 -248
- package/dist/esm/extension/packet.js +0 -16
- package/dist/esm/forfeit.js +0 -41
- package/dist/esm/identity/descriptor.js +0 -161
- package/dist/esm/identity/descriptorProvider.js +0 -1
- package/dist/esm/identity/hdCapableIdentity.js +0 -17
- package/dist/esm/identity/index.js +0 -13
- package/dist/esm/identity/seedIdentity.js +0 -469
- package/dist/esm/identity/serialize.js +0 -164
- package/dist/esm/identity/singleKey.js +0 -121
- package/dist/esm/identity/staticDescriptorProvider.js +0 -61
- package/dist/esm/index.js +0 -87
- package/dist/esm/intent/index.js +0 -255
- package/dist/esm/musig2/index.js +0 -3
- package/dist/esm/musig2/keys.js +0 -21
- package/dist/esm/musig2/nonces.js +0 -11
- package/dist/esm/musig2/sign.js +0 -63
- package/dist/esm/networks.js +0 -22
- package/dist/esm/package.json +0 -3
- package/dist/esm/providers/ark.js +0 -572
- package/dist/esm/providers/delegator.js +0 -81
- package/dist/esm/providers/electrum.js +0 -864
- package/dist/esm/providers/errors.js +0 -54
- package/dist/esm/providers/expoArk.js +0 -78
- package/dist/esm/providers/expoIndexer.js +0 -107
- package/dist/esm/providers/expoUtils.js +0 -87
- package/dist/esm/providers/indexer.js +0 -626
- package/dist/esm/providers/onchain.js +0 -258
- package/dist/esm/providers/utils.js +0 -117
- package/dist/esm/repositories/contractRepository.js +0 -1
- package/dist/esm/repositories/inMemory/contractRepository.js +0 -51
- package/dist/esm/repositories/inMemory/walletRepository.js +0 -111
- package/dist/esm/repositories/index.js +0 -10
- package/dist/esm/repositories/indexedDB/contractRepository.js +0 -183
- package/dist/esm/repositories/indexedDB/db.js +0 -4
- package/dist/esm/repositories/indexedDB/manager.js +0 -95
- package/dist/esm/repositories/indexedDB/schema.js +0 -199
- package/dist/esm/repositories/indexedDB/walletRepository.js +0 -470
- package/dist/esm/repositories/indexedDB/websqlAdapter.js +0 -138
- package/dist/esm/repositories/migrations/contractRepositoryImpl.js +0 -121
- package/dist/esm/repositories/migrations/fromStorageAdapter.js +0 -58
- package/dist/esm/repositories/migrations/walletRepositoryImpl.js +0 -180
- package/dist/esm/repositories/realm/contractRepository.js +0 -112
- package/dist/esm/repositories/realm/index.js +0 -3
- package/dist/esm/repositories/realm/schemas.js +0 -153
- package/dist/esm/repositories/realm/types.js +0 -6
- package/dist/esm/repositories/realm/walletRepository.js +0 -301
- package/dist/esm/repositories/scriptFromAddress.js +0 -13
- package/dist/esm/repositories/serialization.js +0 -67
- package/dist/esm/repositories/sqlite/contractRepository.js +0 -131
- package/dist/esm/repositories/sqlite/index.js +0 -2
- package/dist/esm/repositories/sqlite/types.js +0 -1
- package/dist/esm/repositories/sqlite/walletRepository.js +0 -437
- package/dist/esm/repositories/walletRepository.js +0 -1
- package/dist/esm/script/address.js +0 -104
- package/dist/esm/script/base.js +0 -179
- package/dist/esm/script/default.js +0 -54
- package/dist/esm/script/delegate.js +0 -50
- package/dist/esm/script/tapscript.js +0 -615
- package/dist/esm/script/vhtlc.js +0 -167
- package/dist/esm/storage/asyncStorage.js +0 -46
- package/dist/esm/storage/fileSystem.js +0 -104
- package/dist/esm/storage/inMemory.js +0 -20
- package/dist/esm/storage/index.js +0 -1
- package/dist/esm/storage/indexedDB.js +0 -97
- package/dist/esm/storage/localStorage.js +0 -47
- package/dist/esm/tree/signingSession.js +0 -191
- package/dist/esm/tree/txTree.js +0 -188
- package/dist/esm/tree/validation.js +0 -101
- package/dist/esm/utils/anchor.js +0 -31
- package/dist/esm/utils/arkTransaction.js +0 -264
- package/dist/esm/utils/bip21.js +0 -123
- package/dist/esm/utils/syncCursors.js +0 -119
- package/dist/esm/utils/timelock.js +0 -22
- package/dist/esm/utils/transaction.js +0 -24
- package/dist/esm/utils/transactionHistory.js +0 -180
- package/dist/esm/utils/txSizeEstimator.js +0 -128
- package/dist/esm/utils/unknownFields.js +0 -169
- package/dist/esm/wallet/asset-manager.js +0 -325
- package/dist/esm/wallet/asset.js +0 -113
- package/dist/esm/wallet/batch.js +0 -180
- package/dist/esm/wallet/delegator.js +0 -303
- package/dist/esm/wallet/expo/background.js +0 -111
- package/dist/esm/wallet/expo/index.js +0 -2
- package/dist/esm/wallet/expo/wallet.js +0 -193
- package/dist/esm/wallet/hdDescriptorProvider.js +0 -184
- package/dist/esm/wallet/index.js +0 -75
- package/dist/esm/wallet/inputSignerRouter.js +0 -94
- package/dist/esm/wallet/onchain.js +0 -285
- package/dist/esm/wallet/ramps.js +0 -212
- package/dist/esm/wallet/serviceWorker/wallet-message-handler.js +0 -946
- package/dist/esm/wallet/serviceWorker/wallet.js +0 -1169
- package/dist/esm/wallet/signingErrors.js +0 -27
- package/dist/esm/wallet/unroll.js +0 -289
- package/dist/esm/wallet/utils.js +0 -103
- package/dist/esm/wallet/validation.js +0 -142
- package/dist/esm/wallet/vtxo-manager.js +0 -1136
- package/dist/esm/wallet/wallet.js +0 -2186
- package/dist/esm/wallet/walletReceiveRotator.js +0 -540
- package/dist/esm/worker/browser/service-worker-manager.js +0 -177
- package/dist/esm/worker/browser/utils.js +0 -63
- package/dist/esm/worker/errors.js +0 -11
- package/dist/esm/worker/expo/asyncStorageTaskQueue.js +0 -74
- package/dist/esm/worker/expo/index.js +0 -4
- package/dist/esm/worker/expo/processors/contractPollProcessor.js +0 -59
- package/dist/esm/worker/expo/processors/index.js +0 -1
- package/dist/esm/worker/expo/taskQueue.js +0 -37
- package/dist/esm/worker/expo/taskRunner.js +0 -69
- package/dist/esm/worker/messageBus.js +0 -470
- package/dist/types/adapters/asyncStorage.d.ts +0 -2
- package/dist/types/adapters/expo.d.ts +0 -4
- package/dist/types/adapters/fileSystem.d.ts +0 -2
- package/dist/types/adapters/indexedDB.d.ts +0 -2
- package/dist/types/adapters/localStorage.d.ts +0 -2
- package/dist/types/arkfee/celenv.d.ts +0 -25
- package/dist/types/arkfee/estimator.d.ts +0 -49
- package/dist/types/arkfee/index.d.ts +0 -2
- package/dist/types/arkfee/types.d.ts +0 -38
- package/dist/types/arknote/index.d.ts +0 -84
- package/dist/types/bip322/index.d.ts +0 -55
- package/dist/types/contracts/arkcontract.d.ts +0 -99
- package/dist/types/contracts/contractManager.d.ts +0 -411
- package/dist/types/contracts/contractWatcher.d.ts +0 -217
- package/dist/types/contracts/handlers/default.d.ts +0 -19
- package/dist/types/contracts/handlers/delegate.d.ts +0 -21
- package/dist/types/contracts/handlers/helpers.d.ts +0 -19
- package/dist/types/contracts/handlers/index.d.ts +0 -7
- package/dist/types/contracts/handlers/registry.d.ts +0 -65
- package/dist/types/contracts/handlers/vhtlc.d.ts +0 -32
- package/dist/types/contracts/index.d.ts +0 -14
- package/dist/types/contracts/types.d.ts +0 -250
- package/dist/types/contracts/vtxoOwnership.d.ts +0 -33
- package/dist/types/extension/asset/assetGroup.d.ts +0 -119
- package/dist/types/extension/asset/assetId.d.ts +0 -83
- package/dist/types/extension/asset/assetInput.d.ts +0 -64
- package/dist/types/extension/asset/assetOutput.d.ts +0 -54
- package/dist/types/extension/asset/assetRef.d.ts +0 -91
- package/dist/types/extension/asset/index.d.ts +0 -8
- package/dist/types/extension/asset/metadata.d.ts +0 -52
- package/dist/types/extension/asset/packet.d.ts +0 -41
- package/dist/types/extension/asset/types.d.ts +0 -16
- package/dist/types/extension/asset/utils.d.ts +0 -21
- package/dist/types/extension/index.d.ts +0 -56
- package/dist/types/extension/packet.d.ts +0 -21
- package/dist/types/forfeit.d.ts +0 -18
- package/dist/types/identity/descriptor.d.ts +0 -61
- package/dist/types/identity/descriptorProvider.d.ts +0 -42
- package/dist/types/identity/hdCapableIdentity.d.ts +0 -71
- package/dist/types/identity/index.d.ts +0 -57
- package/dist/types/identity/seedIdentity.d.ts +0 -270
- package/dist/types/identity/serialize.d.ts +0 -96
- package/dist/types/identity/singleKey.d.ts +0 -62
- package/dist/types/identity/staticDescriptorProvider.d.ts +0 -18
- package/dist/types/index.d.ts +0 -59
- package/dist/types/intent/index.d.ts +0 -86
- package/dist/types/musig2/index.d.ts +0 -4
- package/dist/types/musig2/keys.d.ts +0 -9
- package/dist/types/musig2/nonces.d.ts +0 -14
- package/dist/types/musig2/sign.d.ts +0 -27
- package/dist/types/networks.d.ts +0 -16
- package/dist/types/providers/ark.d.ts +0 -369
- package/dist/types/providers/delegator.d.ts +0 -82
- package/dist/types/providers/electrum.d.ts +0 -312
- package/dist/types/providers/errors.d.ts +0 -13
- package/dist/types/providers/expoArk.d.ts +0 -22
- package/dist/types/providers/expoIndexer.d.ts +0 -18
- package/dist/types/providers/expoUtils.d.ts +0 -18
- package/dist/types/providers/indexer.d.ts +0 -301
- package/dist/types/providers/onchain.d.ts +0 -148
- package/dist/types/providers/utils.d.ts +0 -12
- package/dist/types/repositories/contractRepository.d.ts +0 -32
- package/dist/types/repositories/inMemory/contractRepository.d.ts +0 -17
- package/dist/types/repositories/inMemory/walletRepository.d.ts +0 -29
- package/dist/types/repositories/index.d.ts +0 -9
- package/dist/types/repositories/indexedDB/contractRepository.d.ts +0 -21
- package/dist/types/repositories/indexedDB/db.d.ts +0 -4
- package/dist/types/repositories/indexedDB/manager.d.ts +0 -25
- package/dist/types/repositories/indexedDB/schema.d.ts +0 -9
- package/dist/types/repositories/indexedDB/walletRepository.d.ts +0 -28
- package/dist/types/repositories/indexedDB/websqlAdapter.d.ts +0 -49
- package/dist/types/repositories/migrations/contractRepositoryImpl.d.ts +0 -24
- package/dist/types/repositories/migrations/fromStorageAdapter.d.ts +0 -19
- package/dist/types/repositories/migrations/walletRepositoryImpl.d.ts +0 -27
- package/dist/types/repositories/realm/contractRepository.d.ts +0 -24
- package/dist/types/repositories/realm/index.d.ts +0 -4
- package/dist/types/repositories/realm/types.d.ts +0 -16
- package/dist/types/repositories/realm/walletRepository.d.ts +0 -34
- package/dist/types/repositories/scriptFromAddress.d.ts +0 -9
- package/dist/types/repositories/serialization.d.ts +0 -65
- package/dist/types/repositories/sqlite/contractRepository.d.ts +0 -33
- package/dist/types/repositories/sqlite/index.d.ts +0 -3
- package/dist/types/repositories/sqlite/types.d.ts +0 -18
- package/dist/types/repositories/walletRepository.d.ts +0 -72
- package/dist/types/script/address.d.ts +0 -67
- package/dist/types/script/base.d.ts +0 -105
- package/dist/types/script/default.d.ts +0 -44
- package/dist/types/script/delegate.d.ts +0 -40
- package/dist/types/script/tapscript.d.ts +0 -169
- package/dist/types/script/vhtlc.d.ts +0 -66
- package/dist/types/tree/signingSession.d.ts +0 -37
- package/dist/types/tree/txTree.d.ts +0 -28
- package/dist/types/tree/validation.d.ts +0 -15
- package/dist/types/utils/anchor.d.ts +0 -19
- package/dist/types/utils/arkTransaction.d.ts +0 -49
- package/dist/types/utils/bip21.d.ts +0 -38
- package/dist/types/utils/syncCursors.d.ts +0 -60
- package/dist/types/utils/timelock.d.ts +0 -9
- package/dist/types/utils/transaction.d.ts +0 -13
- package/dist/types/utils/transactionHistory.d.ts +0 -15
- package/dist/types/utils/txSizeEstimator.d.ts +0 -40
- package/dist/types/utils/unknownFields.d.ts +0 -83
- package/dist/types/wallet/asset-manager.d.ts +0 -69
- package/dist/types/wallet/asset.d.ts +0 -21
- package/dist/types/wallet/batch.d.ts +0 -107
- package/dist/types/wallet/delegator.d.ts +0 -48
- package/dist/types/wallet/expo/background.d.ts +0 -66
- package/dist/types/wallet/expo/index.d.ts +0 -4
- package/dist/types/wallet/hdDescriptorProvider.d.ts +0 -114
- package/dist/types/wallet/index.d.ts +0 -789
- package/dist/types/wallet/inputSignerRouter.d.ts +0 -35
- package/dist/types/wallet/onchain.d.ts +0 -109
- package/dist/types/wallet/ramps.d.ts +0 -64
- package/dist/types/wallet/serviceWorker/wallet-message-handler.d.ts +0 -543
- package/dist/types/wallet/serviceWorker/wallet.d.ts +0 -248
- package/dist/types/wallet/signingErrors.d.ts +0 -19
- package/dist/types/wallet/unroll.d.ts +0 -114
- package/dist/types/wallet/utils.d.ts +0 -36
- package/dist/types/wallet/validation.d.ts +0 -24
- package/dist/types/wallet/vtxo-manager.d.ts +0 -476
- package/dist/types/wallet/wallet.d.ts +0 -409
- package/dist/types/wallet/walletReceiveRotator.d.ts +0 -306
- package/dist/types/worker/browser/service-worker-manager.d.ts +0 -32
- package/dist/types/worker/browser/utils.d.ts +0 -17
- package/dist/types/worker/errors.d.ts +0 -7
- package/dist/types/worker/expo/index.d.ts +0 -7
- package/dist/types/worker/expo/processors/contractPollProcessor.d.ts +0 -19
- package/dist/types/worker/expo/processors/index.d.ts +0 -1
- package/dist/types/worker/expo/taskQueue.d.ts +0 -50
- package/dist/types/worker/expo/taskRunner.d.ts +0 -66
- package/dist/types/worker/messageBus.d.ts +0 -189
|
@@ -1,864 +0,0 @@
|
|
|
1
|
-
import { Address, OutScript, Transaction } from "@scure/btc-signer";
|
|
2
|
-
import { sha256 } from "@scure/btc-signer/utils.js";
|
|
3
|
-
import { hex } from "@scure/base";
|
|
4
|
-
/**
|
|
5
|
-
* Default WebSocket Electrum endpoints. Mainnet, mutinynet, and signet
|
|
6
|
-
* point at Ark Labs–operated Fulcrum 2.1 deployments (which support
|
|
7
|
-
* `blockchain.transaction.broadcast_package` for atomic 1P1C TRUC
|
|
8
|
-
* relay; see `ElectrumOnchainProvider.broadcastTransaction`). Testnet
|
|
9
|
-
* defaults to Blockstream's public Fulcrum because Ark doesn't host
|
|
10
|
-
* it. Regtest assumes the `electrum-ws` websocat bridge from
|
|
11
|
-
* `vulpemventures/nigiri`.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```typescript
|
|
15
|
-
* import { ElectrumWS } from "ws-electrumx-client";
|
|
16
|
-
* import { ELECTRUM_WS_URL, ElectrumOnchainProvider, networks } from "@arkade-os/sdk";
|
|
17
|
-
*
|
|
18
|
-
* const ws = new ElectrumWS(ELECTRUM_WS_URL.bitcoin);
|
|
19
|
-
* const provider = new ElectrumOnchainProvider(ws, networks.bitcoin);
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
|
-
export const ELECTRUM_WS_URL = {
|
|
23
|
-
bitcoin: "wss://electrum.arkade.sh",
|
|
24
|
-
testnet: "wss://electrum.blockstream.info:60004",
|
|
25
|
-
signet: "wss://electrum.signet.arkade.sh",
|
|
26
|
-
mutinynet: "wss://electrum.mutinynet.arkade.sh",
|
|
27
|
-
regtest: "ws://localhost:50003",
|
|
28
|
-
};
|
|
29
|
-
/**
|
|
30
|
-
* Hostnames for Electrum endpoints reachable over raw TCP. Provided as
|
|
31
|
-
* a reference for Node-side consumers — the SDK's
|
|
32
|
-
* {@link ElectrumOnchainProvider} only speaks WebSocket because it has
|
|
33
|
-
* to run in browsers, so this map is informational only and not
|
|
34
|
-
* consumed by any built-in provider.
|
|
35
|
-
*
|
|
36
|
-
* Public Ark Labs Fulcrum instances expose:
|
|
37
|
-
* - port 50001 — plain TCP (Electrum protocol)
|
|
38
|
-
* - port 50002 — TCP + TLS (Electrum protocol)
|
|
39
|
-
* - port 50003 — WebSocket (Electrum-over-WS, see {@link ELECTRUM_WS_URL})
|
|
40
|
-
*/
|
|
41
|
-
export const ELECTRUM_TCP_HOST = {
|
|
42
|
-
bitcoin: "electrum.arkade.sh",
|
|
43
|
-
testnet: null,
|
|
44
|
-
signet: "electrum.signet.arkade.sh",
|
|
45
|
-
mutinynet: "electrum.mutinynet.arkade.sh",
|
|
46
|
-
regtest: "localhost",
|
|
47
|
-
};
|
|
48
|
-
// Electrum protocol method names
|
|
49
|
-
const BroadcastTransaction = "blockchain.transaction.broadcast";
|
|
50
|
-
const BroadcastPackageMethod = "blockchain.transaction.broadcast_package";
|
|
51
|
-
const EstimateFee = "blockchain.estimatefee";
|
|
52
|
-
const GetBlockHeader = "blockchain.block.header";
|
|
53
|
-
const GetHistoryMethod = "blockchain.scripthash.get_history";
|
|
54
|
-
const GetTransactionMethod = "blockchain.transaction.get";
|
|
55
|
-
const GetTransactionMerkleMethod = "blockchain.transaction.get_merkle";
|
|
56
|
-
const SubscribeStatusMethod = "blockchain.scripthash";
|
|
57
|
-
const SubscribeHeadersMethod = "blockchain.headers";
|
|
58
|
-
const GetRelayFeeMethod = "blockchain.relayfee";
|
|
59
|
-
const ListUnspentMethod = "blockchain.scripthash.listunspent";
|
|
60
|
-
const MISSING_TRANSACTION = "missingtransaction";
|
|
61
|
-
const MAX_FETCH_TRANSACTIONS_ATTEMPTS = 5;
|
|
62
|
-
// Bitcoin block header is 80 bytes
|
|
63
|
-
const BLOCK_HEADER_SIZE = 80;
|
|
64
|
-
/**
|
|
65
|
-
* Parse a raw block header (80 bytes hex = 160 chars) to extract fields.
|
|
66
|
-
* Bitcoin block header layout:
|
|
67
|
-
* - version: 4 bytes (LE)
|
|
68
|
-
* - prevHash: 32 bytes
|
|
69
|
-
* - merkleRoot: 32 bytes
|
|
70
|
-
* - timestamp: 4 bytes (LE)
|
|
71
|
-
* - bits: 4 bytes
|
|
72
|
-
* - nonce: 4 bytes
|
|
73
|
-
*/
|
|
74
|
-
function parseBlockHeader(headerHex) {
|
|
75
|
-
const headerBytes = hex.decode(headerHex);
|
|
76
|
-
if (headerBytes.length !== BLOCK_HEADER_SIZE) {
|
|
77
|
-
throw new Error(`Invalid block header size: ${headerBytes.length}, expected ${BLOCK_HEADER_SIZE}`);
|
|
78
|
-
}
|
|
79
|
-
// timestamp is at offset 68 (4+32+32), 4 bytes little-endian
|
|
80
|
-
const view = new DataView(headerBytes.buffer, headerBytes.byteOffset);
|
|
81
|
-
const timestamp = view.getUint32(68, true);
|
|
82
|
-
// block hash = double SHA256 of header, reversed
|
|
83
|
-
const hash1 = sha256(headerBytes);
|
|
84
|
-
const hash2 = sha256(hash1);
|
|
85
|
-
const hashStr = hex.encode(new Uint8Array(hash2).reverse());
|
|
86
|
-
return { hash: hashStr, timestamp };
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* WebSocket-based Electrum chain source using ws-electrumx-client.
|
|
90
|
-
* Provides low-level methods for the Electrum protocol.
|
|
91
|
-
*
|
|
92
|
-
* @example
|
|
93
|
-
* ```typescript
|
|
94
|
-
* import { ElectrumWS } from "ws-electrumx-client";
|
|
95
|
-
* import { WsElectrumChainSource } from "./providers/electrum.js";
|
|
96
|
-
* import { networks } from "./networks.js";
|
|
97
|
-
*
|
|
98
|
-
* const ws = new ElectrumWS("wss://electrum.blockstream.info:50004");
|
|
99
|
-
* const chain = new WsElectrumChainSource(ws, networks.bitcoin);
|
|
100
|
-
*
|
|
101
|
-
* const history = await chain.fetchHistories([script]);
|
|
102
|
-
* await chain.close();
|
|
103
|
-
* ```
|
|
104
|
-
*/
|
|
105
|
-
export class WsElectrumChainSource {
|
|
106
|
-
constructor(ws, network) {
|
|
107
|
-
this.ws = ws;
|
|
108
|
-
this.network = network;
|
|
109
|
-
// Cached chain tip kept fresh by the headers subscription. Initialized
|
|
110
|
-
// lazily on first call to subscribeHeaders().
|
|
111
|
-
this.cachedTip = null;
|
|
112
|
-
this.headersSubscribePromise = null;
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Send N requests in parallel and aggregate the results, replacement
|
|
116
|
-
* for `ws.batchRequest`. The library's batchRequest is implemented as
|
|
117
|
-
* `Promise.all` over individual request promises — when one element
|
|
118
|
-
* rejects, the others remain pending. When their (often error)
|
|
119
|
-
* responses arrive later, the library rejects them too, and nobody is
|
|
120
|
-
* awaiting them: the rejections become unhandled and crash the test
|
|
121
|
-
* runner / pollute production logs.
|
|
122
|
-
*
|
|
123
|
-
* `safeBatchRequest` issues each request through `ws.request` (so each
|
|
124
|
-
* has its own request-promise lifecycle), waits for all of them via
|
|
125
|
-
* `Promise.allSettled` (every promise gets an explicit handler), and
|
|
126
|
-
* then surfaces the first error if any failed. Same wall-clock cost
|
|
127
|
-
* as the library's batch (parallel send), no orphan rejections.
|
|
128
|
-
*
|
|
129
|
-
* Use this in place of `ws.batchRequest` for any call where one or
|
|
130
|
-
* more elements may legitimately error (e.g. electrs index lag
|
|
131
|
-
* surfacing as `missingheight` for a subset of heights/txids).
|
|
132
|
-
*/
|
|
133
|
-
async safeBatchRequest(requests) {
|
|
134
|
-
if (requests.length === 0)
|
|
135
|
-
return [];
|
|
136
|
-
const settled = await Promise.allSettled(requests.map((req) => this.ws.request(req.method, ...req.params)));
|
|
137
|
-
for (const r of settled) {
|
|
138
|
-
if (r.status === "rejected")
|
|
139
|
-
throw r.reason;
|
|
140
|
-
}
|
|
141
|
-
return settled.map((r) => r.value);
|
|
142
|
-
}
|
|
143
|
-
async fetchTransactions(txids) {
|
|
144
|
-
const requests = txids.map((txid) => ({
|
|
145
|
-
method: GetTransactionMethod,
|
|
146
|
-
params: [txid],
|
|
147
|
-
}));
|
|
148
|
-
for (let i = 0; i < MAX_FETCH_TRANSACTIONS_ATTEMPTS; i++) {
|
|
149
|
-
try {
|
|
150
|
-
const responses = await this.safeBatchRequest(requests);
|
|
151
|
-
return responses.map((hexStr, i) => ({
|
|
152
|
-
txID: txids[i],
|
|
153
|
-
hex: hexStr,
|
|
154
|
-
}));
|
|
155
|
-
}
|
|
156
|
-
catch (e) {
|
|
157
|
-
const msg = e instanceof Error ? e.message : String(e);
|
|
158
|
-
if (msg.toLowerCase().includes(MISSING_TRANSACTION)) {
|
|
159
|
-
console.warn("missing transaction error, retrying");
|
|
160
|
-
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
161
|
-
continue;
|
|
162
|
-
}
|
|
163
|
-
throw e;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
throw new Error("Unable to fetch transactions: " + txids);
|
|
167
|
-
}
|
|
168
|
-
async fetchVerboseTransaction(txid) {
|
|
169
|
-
return this.ws.request(GetTransactionMethod, txid, true);
|
|
170
|
-
}
|
|
171
|
-
async fetchVerboseTransactions(txids) {
|
|
172
|
-
if (txids.length === 0)
|
|
173
|
-
return [];
|
|
174
|
-
const requests = txids.map((txid) => ({
|
|
175
|
-
method: GetTransactionMethod,
|
|
176
|
-
params: [txid, true],
|
|
177
|
-
}));
|
|
178
|
-
return this.safeBatchRequest(requests);
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Look up the block height of a confirmed transaction without relying
|
|
182
|
-
* on the verbose-tx endpoint. `blockchain.transaction.get_merkle` is
|
|
183
|
-
* part of the standard SPV protocol and is supported by both Fulcrum
|
|
184
|
-
* and electrs (whereas `blockchain.transaction.get` with verbose=true
|
|
185
|
-
* is Fulcrum-only). Returns `null` when the tx is in the mempool —
|
|
186
|
-
* electrs in that case rejects with a "not yet in a block" error.
|
|
187
|
-
*/
|
|
188
|
-
async fetchTxMerkle(txid) {
|
|
189
|
-
let result;
|
|
190
|
-
try {
|
|
191
|
-
result = await this.ws.request(GetTransactionMerkleMethod, txid);
|
|
192
|
-
}
|
|
193
|
-
catch (err) {
|
|
194
|
-
// electrs/Fulcrum raise specific errors when the tx isn't yet in
|
|
195
|
-
// a block — either "not in a block" wording, or "missingheight"
|
|
196
|
-
// when get_merkle hits the same index-lag race that block.header
|
|
197
|
-
// hits (the tx is reportedly confirmed but the height isn't
|
|
198
|
-
// queryable yet). Map both to mempool/unknown; everything else
|
|
199
|
-
// (auth failure, network outage, malformed response) must surface
|
|
200
|
-
// so callers can fail rather than silently treat the tx as
|
|
201
|
-
// unconfirmed forever.
|
|
202
|
-
if (isTxNotInBlockError(err) || isMissingHeightError(err))
|
|
203
|
-
return null;
|
|
204
|
-
throw err;
|
|
205
|
-
}
|
|
206
|
-
if (!result ||
|
|
207
|
-
typeof result.block_height !== "number" ||
|
|
208
|
-
result.block_height <= 0) {
|
|
209
|
-
return null;
|
|
210
|
-
}
|
|
211
|
-
return { blockHeight: result.block_height };
|
|
212
|
-
}
|
|
213
|
-
async unsubscribeScriptStatus(script) {
|
|
214
|
-
await this.ws
|
|
215
|
-
.unsubscribe(SubscribeStatusMethod, toScriptHash(script))
|
|
216
|
-
.catch(() => { });
|
|
217
|
-
}
|
|
218
|
-
async subscribeScriptStatus(script, callback) {
|
|
219
|
-
const scriptHash = toScriptHash(script);
|
|
220
|
-
await this.ws.subscribe(SubscribeStatusMethod, (scripthash, status) => {
|
|
221
|
-
if (scripthash === scriptHash) {
|
|
222
|
-
callback(scripthash, status);
|
|
223
|
-
}
|
|
224
|
-
}, scriptHash);
|
|
225
|
-
}
|
|
226
|
-
async fetchHistories(scripts) {
|
|
227
|
-
const scriptsHashes = scripts.map((s) => toScriptHash(s));
|
|
228
|
-
return this.safeBatchRequest(scriptsHashes.map((s) => ({
|
|
229
|
-
method: GetHistoryMethod,
|
|
230
|
-
params: [s],
|
|
231
|
-
})));
|
|
232
|
-
}
|
|
233
|
-
async fetchHistory(script) {
|
|
234
|
-
const scriptHash = toScriptHash(script);
|
|
235
|
-
return this.ws.request(GetHistoryMethod, scriptHash);
|
|
236
|
-
}
|
|
237
|
-
async fetchBlockHeaders(heights) {
|
|
238
|
-
const responses = await this.safeBatchRequest(heights.map((h) => ({ method: GetBlockHeader, params: [h] })));
|
|
239
|
-
return responses.map((hexStr, i) => ({
|
|
240
|
-
height: heights[i],
|
|
241
|
-
hex: hexStr,
|
|
242
|
-
}));
|
|
243
|
-
}
|
|
244
|
-
async fetchBlockHeader(height) {
|
|
245
|
-
const headerHex = await this.ws.request(GetBlockHeader, height);
|
|
246
|
-
return { height, hex: headerHex };
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Returns the current chain tip and keeps it fresh via a single
|
|
250
|
-
* server-side subscription. Subsequent calls return the cached tip
|
|
251
|
-
* (updated by background notifications) without round-tripping to the
|
|
252
|
-
* server. Previously each call issued `blockchain.headers.subscribe` as
|
|
253
|
-
* a regular request, leaving a stale subscription on the server every
|
|
254
|
-
* time — under polling that adds up. ws-electrumx-client deduplicates
|
|
255
|
-
* `subscribe()` by method+params, so registering once is enough.
|
|
256
|
-
*/
|
|
257
|
-
async subscribeHeaders() {
|
|
258
|
-
if (this.cachedTip)
|
|
259
|
-
return this.cachedTip;
|
|
260
|
-
if (this.headersSubscribePromise)
|
|
261
|
-
return this.headersSubscribePromise;
|
|
262
|
-
this.headersSubscribePromise = new Promise((resolve, reject) => {
|
|
263
|
-
let resolved = false;
|
|
264
|
-
this.ws
|
|
265
|
-
.subscribe(SubscribeHeadersMethod, (header) => {
|
|
266
|
-
if (!isHeaderSubscribeResult(header))
|
|
267
|
-
return;
|
|
268
|
-
this.cachedTip = header;
|
|
269
|
-
if (!resolved) {
|
|
270
|
-
resolved = true;
|
|
271
|
-
resolve(header);
|
|
272
|
-
}
|
|
273
|
-
})
|
|
274
|
-
.catch((err) => {
|
|
275
|
-
if (!resolved) {
|
|
276
|
-
resolved = true;
|
|
277
|
-
reject(err);
|
|
278
|
-
}
|
|
279
|
-
});
|
|
280
|
-
});
|
|
281
|
-
try {
|
|
282
|
-
return await this.headersSubscribePromise;
|
|
283
|
-
}
|
|
284
|
-
catch (err) {
|
|
285
|
-
// Allow the next call to retry from scratch.
|
|
286
|
-
this.headersSubscribePromise = null;
|
|
287
|
-
throw err;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
async estimateFees(targetNumberBlocks) {
|
|
291
|
-
const feeRate = await this.ws.request(EstimateFee, targetNumberBlocks);
|
|
292
|
-
return feeRate;
|
|
293
|
-
}
|
|
294
|
-
async broadcastTransaction(txHex) {
|
|
295
|
-
return this.ws.request(BroadcastTransaction, txHex);
|
|
296
|
-
}
|
|
297
|
-
/**
|
|
298
|
-
* Submit a package of raw transactions atomically via Fulcrum's
|
|
299
|
-
* `blockchain.transaction.broadcast_package` method, the on-the-wire
|
|
300
|
-
* equivalent of bitcoind's `submitpackage` RPC.
|
|
301
|
-
*
|
|
302
|
-
* Required for TRUC (BIP 431) 1P1C relay where the parent has zero
|
|
303
|
-
* (or below-minfee) fee and depends on the child to pay for both via
|
|
304
|
-
* CPFP — sequential broadcast cannot work in that case because the
|
|
305
|
-
* parent would be rejected from the mempool on its own.
|
|
306
|
-
*
|
|
307
|
-
* @param txHexes - Topologically sorted raw transactions; child must
|
|
308
|
-
* be the last element. Currently must be a 1P1C pair
|
|
309
|
-
* (length 2). Parents may not depend on each other.
|
|
310
|
-
* @returns The child transaction id (the last entry in the array),
|
|
311
|
-
* computed locally — `broadcast_package` itself returns
|
|
312
|
-
* `{success, errors}` rather than a txid.
|
|
313
|
-
* @throws If the server does not implement `broadcast_package` (e.g.
|
|
314
|
-
* ElectrumX, or older Fulcrum, or Fulcrum backed by bitcoind
|
|
315
|
-
* < v28.0.0). Callers must surface this clearly to users —
|
|
316
|
-
* this method does NOT silently fall back to sequential
|
|
317
|
-
* broadcasts because doing so would let TRUC packages fail
|
|
318
|
-
* in subtle ways.
|
|
319
|
-
* @throws If the server returns `success=false`, surfacing the
|
|
320
|
-
* underlying mempool rejection in the error message.
|
|
321
|
-
*/
|
|
322
|
-
async broadcastPackage(txHexes) {
|
|
323
|
-
const result = await this.ws.request(BroadcastPackageMethod, txHexes, false);
|
|
324
|
-
if (!result.success) {
|
|
325
|
-
const detail = result.errors
|
|
326
|
-
? JSON.stringify(result.errors)
|
|
327
|
-
: "unknown error";
|
|
328
|
-
throw new Error(`Package broadcast rejected: ${detail}`);
|
|
329
|
-
}
|
|
330
|
-
// The child txid is not in the response — derive it from the raw
|
|
331
|
-
// bytes (double-SHA256 of the serialized tx, reversed).
|
|
332
|
-
return childTxidFromHex(txHexes[txHexes.length - 1]);
|
|
333
|
-
}
|
|
334
|
-
async getRelayFee() {
|
|
335
|
-
return this.ws.request(GetRelayFeeMethod);
|
|
336
|
-
}
|
|
337
|
-
async close() {
|
|
338
|
-
try {
|
|
339
|
-
await this.ws.close("close");
|
|
340
|
-
}
|
|
341
|
-
catch (e) {
|
|
342
|
-
console.debug("error closing ws:", e);
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
waitForAddressReceivesTx(addr) {
|
|
346
|
-
return new Promise((resolve, reject) => {
|
|
347
|
-
const script = OutScript.encode(Address(this.network).decode(addr));
|
|
348
|
-
this.subscribeScriptStatus(script, (_, status) => {
|
|
349
|
-
if (status !== null) {
|
|
350
|
-
resolve();
|
|
351
|
-
}
|
|
352
|
-
}).catch(reject);
|
|
353
|
-
});
|
|
354
|
-
}
|
|
355
|
-
async listUnspents(addr) {
|
|
356
|
-
const script = OutScript.encode(Address(this.network).decode(addr));
|
|
357
|
-
const scriptHash = toScriptHash(script);
|
|
358
|
-
const unspentsFromElectrum = await this.ws.request(ListUnspentMethod, scriptHash);
|
|
359
|
-
const txs = await this.fetchTransactions(unspentsFromElectrum.map((u) => u.tx_hash));
|
|
360
|
-
return unspentsFromElectrum.map((u, index) => {
|
|
361
|
-
const tx = Transaction.fromRaw(hex.decode(txs[index].hex), {
|
|
362
|
-
allowUnknownOutputs: true,
|
|
363
|
-
});
|
|
364
|
-
const output = tx.getOutput(u.tx_pos);
|
|
365
|
-
if (!output.script || output.amount === undefined) {
|
|
366
|
-
throw new Error(`Missing output data for ${u.tx_hash}:${u.tx_pos}`);
|
|
367
|
-
}
|
|
368
|
-
return {
|
|
369
|
-
txid: u.tx_hash,
|
|
370
|
-
vout: u.tx_pos,
|
|
371
|
-
witnessUtxo: {
|
|
372
|
-
script: output.script,
|
|
373
|
-
value: output.amount,
|
|
374
|
-
},
|
|
375
|
-
};
|
|
376
|
-
});
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* Get the address string for a script output, if decodable.
|
|
380
|
-
*/
|
|
381
|
-
addressForScript(scriptHex) {
|
|
382
|
-
try {
|
|
383
|
-
const script = hex.decode(scriptHex);
|
|
384
|
-
return Address(this.network).encode(OutScript.decode(script));
|
|
385
|
-
}
|
|
386
|
-
catch {
|
|
387
|
-
return undefined;
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
/**
|
|
392
|
-
* Electrum-based implementation of the {@link OnchainProvider} interface.
|
|
393
|
-
*
|
|
394
|
-
* Built around the subset of the Electrum protocol that both **Fulcrum**
|
|
395
|
-
* and **electrs** support — listunspent, get_history, transaction.get
|
|
396
|
-
* (non-verbose), transaction.get_merkle, block.header,
|
|
397
|
-
* headers.subscribe, scripthash.subscribe, estimatefee, relayfee, and
|
|
398
|
-
* broadcast. The verbose form of `transaction.get` is **not** used (it's
|
|
399
|
-
* Fulcrum-only and rejected by electrs); confirmation status is derived
|
|
400
|
-
* from `transaction.get_merkle` plus parsed block headers.
|
|
401
|
-
*
|
|
402
|
-
* Output amounts are derived from parsed raw transaction bytes (exact
|
|
403
|
-
* bigints), never the floating-point `value` fields some servers return.
|
|
404
|
-
*
|
|
405
|
-
* Atomic 1P1C package broadcast (TRUC / BIP 431) is supported via
|
|
406
|
-
* Fulcrum's `blockchain.transaction.broadcast_package`. There is **no
|
|
407
|
-
* fallback** to sequential parent-then-child broadcasts — TRUC packages
|
|
408
|
-
* with a zero-fee parent would silently fail, so the call surfaces an
|
|
409
|
-
* error against servers that don't support the method.
|
|
410
|
-
*
|
|
411
|
-
* @example Default URL via {@link ELECTRUM_WS_URL}
|
|
412
|
-
* ```typescript
|
|
413
|
-
* import { ElectrumWS } from "ws-electrumx-client";
|
|
414
|
-
* import {
|
|
415
|
-
* ElectrumOnchainProvider,
|
|
416
|
-
* ELECTRUM_WS_URL,
|
|
417
|
-
* networks,
|
|
418
|
-
* } from "@arkade-os/sdk";
|
|
419
|
-
*
|
|
420
|
-
* const ws = new ElectrumWS(ELECTRUM_WS_URL.bitcoin);
|
|
421
|
-
* const provider = new ElectrumOnchainProvider(ws, networks.bitcoin);
|
|
422
|
-
*
|
|
423
|
-
* const coins = await provider.getCoins("bc1q...");
|
|
424
|
-
* await provider.close();
|
|
425
|
-
* ```
|
|
426
|
-
*
|
|
427
|
-
* @example Custom server
|
|
428
|
-
* ```typescript
|
|
429
|
-
* const ws = new ElectrumWS("wss://my-fulcrum.example:50004");
|
|
430
|
-
* const provider = new ElectrumOnchainProvider(ws, networks.bitcoin);
|
|
431
|
-
* ```
|
|
432
|
-
*/
|
|
433
|
-
export class ElectrumOnchainProvider {
|
|
434
|
-
constructor(ws, network) {
|
|
435
|
-
this.ws = ws;
|
|
436
|
-
this.network = network;
|
|
437
|
-
this.chain = new WsElectrumChainSource(ws, network);
|
|
438
|
-
}
|
|
439
|
-
async getCoins(address) {
|
|
440
|
-
const script = this.encodeAddress(address);
|
|
441
|
-
const scriptHash = toScriptHash(script);
|
|
442
|
-
const unspents = await this.ws.request(ListUnspentMethod, scriptHash);
|
|
443
|
-
return unspents.map((u) => ({
|
|
444
|
-
txid: u.tx_hash,
|
|
445
|
-
vout: u.tx_pos,
|
|
446
|
-
value: u.value,
|
|
447
|
-
status: {
|
|
448
|
-
confirmed: u.height > 0,
|
|
449
|
-
block_height: u.height > 0 ? u.height : undefined,
|
|
450
|
-
},
|
|
451
|
-
}));
|
|
452
|
-
}
|
|
453
|
-
async getFeeRate() {
|
|
454
|
-
// electrum returns BTC/kB, we need sat/vB
|
|
455
|
-
// 1 BTC = 100_000_000 sat, 1 kB = 1000 bytes
|
|
456
|
-
// sat/vB = (BTC/kB) * 100_000_000 / 1000 = (BTC/kB) * 100_000
|
|
457
|
-
const feePerKb = await this.chain.estimateFees(1);
|
|
458
|
-
if (feePerKb < 0) {
|
|
459
|
-
// -1 means the daemon cannot estimate
|
|
460
|
-
return undefined;
|
|
461
|
-
}
|
|
462
|
-
return Math.max(1, Math.ceil(feePerKb * 100000));
|
|
463
|
-
}
|
|
464
|
-
/**
|
|
465
|
-
* Broadcast a single transaction or a TRUC (BIP 431) 1P1C package
|
|
466
|
-
* atomically.
|
|
467
|
-
*
|
|
468
|
-
* **Server requirements for 1P1C packages:** the backing Electrum
|
|
469
|
-
* server must implement `blockchain.transaction.broadcast_package`
|
|
470
|
-
* (Fulcrum ≥ 1.10) and be backed by bitcoind ≥ v28.0.0. ElectrumX
|
|
471
|
-
* does not implement this method. There is **no fallback** to
|
|
472
|
-
* sequential parent-then-child broadcast: TRUC packages typically
|
|
473
|
-
* have a zero-fee parent and would be rejected from the mempool on
|
|
474
|
-
* their own, so a fallback would silently fail in subtle ways.
|
|
475
|
-
* Callers receiving a "method not found" error here should route
|
|
476
|
-
* through a different provider for that submission.
|
|
477
|
-
*
|
|
478
|
-
* @param txs - One transaction (single broadcast) or two
|
|
479
|
-
* topologically-sorted transactions (parent first,
|
|
480
|
-
* child last) for 1P1C package relay.
|
|
481
|
-
* @returns The broadcast txid (or the child txid for 1P1C packages).
|
|
482
|
-
*/
|
|
483
|
-
async broadcastTransaction(...txs) {
|
|
484
|
-
if (txs.length === 1) {
|
|
485
|
-
return this.chain.broadcastTransaction(txs[0]);
|
|
486
|
-
}
|
|
487
|
-
if (txs.length === 2) {
|
|
488
|
-
return this.chain.broadcastPackage(txs);
|
|
489
|
-
}
|
|
490
|
-
throw new Error("Only 1 or 1P1C package can be broadcast");
|
|
491
|
-
}
|
|
492
|
-
async getTxOutspends(txid) {
|
|
493
|
-
// Step 1: fetch the creating tx to get its output scripts (1 round trip)
|
|
494
|
-
const [txResult] = await this.chain.fetchTransactions([txid]);
|
|
495
|
-
const tx = Transaction.fromRaw(hex.decode(txResult.hex), {
|
|
496
|
-
allowUnknownOutputs: true,
|
|
497
|
-
});
|
|
498
|
-
const outputCount = tx.outputsLength;
|
|
499
|
-
const outputScriptHashes = [];
|
|
500
|
-
for (let i = 0; i < outputCount; i++) {
|
|
501
|
-
const output = tx.getOutput(i);
|
|
502
|
-
outputScriptHashes.push(output.script ? toScriptHash(output.script) : undefined);
|
|
503
|
-
}
|
|
504
|
-
const validScriptHashes = outputScriptHashes.filter((h) => h !== undefined);
|
|
505
|
-
const results = Array.from({ length: outputCount }, () => ({ spent: false, txid: "" }));
|
|
506
|
-
if (validScriptHashes.length === 0)
|
|
507
|
-
return results;
|
|
508
|
-
// Step 2: batch listunspent for all output scripthashes (1 round trip)
|
|
509
|
-
// This tells us exactly which txid:vout pairs are still unspent.
|
|
510
|
-
const unspentBatch = await this.chain.safeBatchRequest(validScriptHashes.map((sh) => ({
|
|
511
|
-
method: ListUnspentMethod,
|
|
512
|
-
params: [sh],
|
|
513
|
-
})));
|
|
514
|
-
const unspentSet = new Set();
|
|
515
|
-
let validIdx = 0;
|
|
516
|
-
for (let i = 0; i < outputCount; i++) {
|
|
517
|
-
if (outputScriptHashes[i] !== undefined) {
|
|
518
|
-
for (const u of unspentBatch[validIdx]) {
|
|
519
|
-
unspentSet.add(`${u.tx_hash}:${u.tx_pos}`);
|
|
520
|
-
}
|
|
521
|
-
validIdx++;
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
// Step 3: batch get_history only for spent outputs (1 round trip)
|
|
525
|
-
const spentIndices = [];
|
|
526
|
-
const spentScriptHashes = [];
|
|
527
|
-
for (let i = 0; i < outputCount; i++) {
|
|
528
|
-
const sh = outputScriptHashes[i];
|
|
529
|
-
if (sh && !unspentSet.has(`${txid}:${i}`)) {
|
|
530
|
-
spentIndices.push(i);
|
|
531
|
-
spentScriptHashes.push(sh);
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
if (spentIndices.length === 0)
|
|
535
|
-
return results;
|
|
536
|
-
const histories = await this.chain.safeBatchRequest(spentScriptHashes.map((sh) => ({
|
|
537
|
-
method: GetHistoryMethod,
|
|
538
|
-
params: [sh],
|
|
539
|
-
})));
|
|
540
|
-
// For each spent output find the spender in its history.
|
|
541
|
-
// Common case: history has exactly 2 entries (creating + spending tx).
|
|
542
|
-
// Ambiguous case (same script reused): batch-fetch all candidates at once.
|
|
543
|
-
const ambiguousIndices = [];
|
|
544
|
-
const ambiguousCandidates = [];
|
|
545
|
-
for (let j = 0; j < spentIndices.length; j++) {
|
|
546
|
-
const i = spentIndices[j];
|
|
547
|
-
const candidates = histories[j]
|
|
548
|
-
.map((h) => h.tx_hash)
|
|
549
|
-
.filter((hash) => hash !== txid);
|
|
550
|
-
if (candidates.length === 1) {
|
|
551
|
-
// Fast path: one candidate = the spender
|
|
552
|
-
results[i] = { spent: true, txid: candidates[0] };
|
|
553
|
-
}
|
|
554
|
-
else if (candidates.length > 1) {
|
|
555
|
-
ambiguousIndices.push(i);
|
|
556
|
-
ambiguousCandidates.push(candidates);
|
|
557
|
-
}
|
|
558
|
-
// candidates.length === 0 → mempool eviction, treat as unspent
|
|
559
|
-
}
|
|
560
|
-
// Step 4 (rare): batch-fetch all ambiguous candidate txs at once
|
|
561
|
-
if (ambiguousIndices.length > 0) {
|
|
562
|
-
const allCandidateTxids = [...new Set(ambiguousCandidates.flat())];
|
|
563
|
-
const fetched = await this.chain.fetchTransactions(allCandidateTxids);
|
|
564
|
-
const txMap = new Map(fetched.map((t) => [t.txID, t.hex]));
|
|
565
|
-
for (let j = 0; j < ambiguousIndices.length; j++) {
|
|
566
|
-
const i = ambiguousIndices[j];
|
|
567
|
-
for (const candidateTxid of ambiguousCandidates[j]) {
|
|
568
|
-
const rawHex = txMap.get(candidateTxid);
|
|
569
|
-
if (!rawHex)
|
|
570
|
-
continue;
|
|
571
|
-
const candidateTx = Transaction.fromRaw(hex.decode(rawHex), { allowUnknownOutputs: true, allowUnknownInputs: true });
|
|
572
|
-
let found = false;
|
|
573
|
-
for (let k = 0; k < candidateTx.inputsLength; k++) {
|
|
574
|
-
const input = candidateTx.getInput(k);
|
|
575
|
-
if (input.txid &&
|
|
576
|
-
hex.encode(input.txid) === txid &&
|
|
577
|
-
input.index === i) {
|
|
578
|
-
results[i] = { spent: true, txid: candidateTxid };
|
|
579
|
-
found = true;
|
|
580
|
-
break;
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
if (found)
|
|
584
|
-
break;
|
|
585
|
-
}
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
return results;
|
|
589
|
-
}
|
|
590
|
-
async getTransactions(address) {
|
|
591
|
-
const script = this.encodeAddress(address);
|
|
592
|
-
const history = await this.chain.fetchHistory(script);
|
|
593
|
-
if (history.length === 0)
|
|
594
|
-
return [];
|
|
595
|
-
return this.historyToExplorerTxs(history);
|
|
596
|
-
}
|
|
597
|
-
/**
|
|
598
|
-
* Resolve a list of `{tx_hash, height}` entries (as returned by the
|
|
599
|
-
* scripthash history endpoint) into ExplorerTransaction shape **without
|
|
600
|
-
* using the verbose-tx endpoint**, which only Fulcrum implements. We
|
|
601
|
-
* reconstruct everything the verbose response would have given us:
|
|
602
|
-
* - vouts ← parse the raw tx (exact sat amounts, no float precision risk)
|
|
603
|
-
* - block_time ← batch-fetch the block headers for the heights present
|
|
604
|
-
* - addresses ← decode each output's scriptPubKey via @scure/btc-signer
|
|
605
|
-
*/
|
|
606
|
-
async historyToExplorerTxs(history) {
|
|
607
|
-
const txids = history.map((h) => h.tx_hash);
|
|
608
|
-
const rawTxs = await this.chain.fetchTransactions(txids);
|
|
609
|
-
const rawHexByTxid = new Map(rawTxs.map((t) => [t.txID, t.hex]));
|
|
610
|
-
// De-duplicated batch lookup of block headers (now safe — see
|
|
611
|
-
// safeBatchRequest above). Heights whose headers fail to resolve
|
|
612
|
-
// surface via the wrapper's first-error throw; we tolerate that
|
|
613
|
-
// here by falling back to per-height calls under Promise.allSettled
|
|
614
|
-
// so one missing header doesn't poison the whole history mapping.
|
|
615
|
-
// The old verbose-tx code had the same tolerance via
|
|
616
|
-
// `vtx.blocktime || vtx.time || 0`.
|
|
617
|
-
const confirmedHeights = [
|
|
618
|
-
...new Set(history.map((h) => h.height).filter((h) => h > 0)),
|
|
619
|
-
];
|
|
620
|
-
const blockTimeByHeight = new Map();
|
|
621
|
-
if (confirmedHeights.length > 0) {
|
|
622
|
-
try {
|
|
623
|
-
const headers = await this.chain.fetchBlockHeaders(confirmedHeights);
|
|
624
|
-
for (const header of headers) {
|
|
625
|
-
blockTimeByHeight.set(header.height, parseBlockHeader(header.hex).timestamp);
|
|
626
|
-
}
|
|
627
|
-
}
|
|
628
|
-
catch {
|
|
629
|
-
const settled = await Promise.allSettled(confirmedHeights.map((h) => this.chain.fetchBlockHeader(h)));
|
|
630
|
-
settled.forEach((res) => {
|
|
631
|
-
if (res.status === "fulfilled") {
|
|
632
|
-
blockTimeByHeight.set(res.value.height, parseBlockHeader(res.value.hex).timestamp);
|
|
633
|
-
}
|
|
634
|
-
// Rejections leave the height absent → block_time = 0.
|
|
635
|
-
});
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
return history.map((entry) => this.buildExplorerTx(entry, rawHexByTxid.get(entry.tx_hash), blockTimeByHeight));
|
|
639
|
-
}
|
|
640
|
-
/**
|
|
641
|
-
* Build an ExplorerTransaction from a history entry plus the raw tx hex
|
|
642
|
-
* (when known) and a height→block_time map. Parse errors propagate —
|
|
643
|
-
* silently returning an empty vout would hide real outputs (e.g. a
|
|
644
|
-
* deposit) and is far worse for protocol-level money handling than
|
|
645
|
-
* failing the whole batch.
|
|
646
|
-
*/
|
|
647
|
-
buildExplorerTx(entry, rawHex, blockTimeByHeight) {
|
|
648
|
-
const vout = [];
|
|
649
|
-
if (rawHex) {
|
|
650
|
-
let tx;
|
|
651
|
-
try {
|
|
652
|
-
tx = Transaction.fromRaw(hex.decode(rawHex), {
|
|
653
|
-
allowUnknownOutputs: true,
|
|
654
|
-
allowUnknownInputs: true,
|
|
655
|
-
});
|
|
656
|
-
}
|
|
657
|
-
catch (err) {
|
|
658
|
-
throw new Error(`Failed to parse raw tx for ${entry.tx_hash}: ${err instanceof Error ? err.message : String(err)}`);
|
|
659
|
-
}
|
|
660
|
-
for (let i = 0; i < tx.outputsLength; i++) {
|
|
661
|
-
const output = tx.getOutput(i);
|
|
662
|
-
const scriptHex = output.script
|
|
663
|
-
? hex.encode(output.script)
|
|
664
|
-
: "";
|
|
665
|
-
vout.push({
|
|
666
|
-
scriptpubkey_address: scriptHex
|
|
667
|
-
? (this.chain.addressForScript(scriptHex) ?? "")
|
|
668
|
-
: "",
|
|
669
|
-
value: (output.amount ?? 0n).toString(),
|
|
670
|
-
});
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
return {
|
|
674
|
-
txid: entry.tx_hash,
|
|
675
|
-
vout,
|
|
676
|
-
status: {
|
|
677
|
-
confirmed: entry.height > 0,
|
|
678
|
-
block_time: blockTimeByHeight.get(entry.height) ?? 0,
|
|
679
|
-
},
|
|
680
|
-
};
|
|
681
|
-
}
|
|
682
|
-
/**
|
|
683
|
-
* Decode `address` into its scriptPubKey, throwing a clear error if the
|
|
684
|
-
* input is malformed. @scure/btc-signer raises a generic decode error
|
|
685
|
-
* which is hard to map back to user input — this wraps it.
|
|
686
|
-
*/
|
|
687
|
-
encodeAddress(address) {
|
|
688
|
-
try {
|
|
689
|
-
return OutScript.encode(Address(this.network).decode(address));
|
|
690
|
-
}
|
|
691
|
-
catch (err) {
|
|
692
|
-
const reason = err instanceof Error ? err.message : String(err);
|
|
693
|
-
throw new Error(`Invalid address ${address}: ${reason}`);
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
async getTxStatus(txid) {
|
|
697
|
-
// Use `transaction.get_merkle` rather than the verbose `transaction.get`
|
|
698
|
-
// because electrs (used by mempool.space, blockstream.info, and the
|
|
699
|
-
// nigiri regtest) doesn't implement verbose. get_merkle is part of the
|
|
700
|
-
// standard SPV protocol and supported by every Electrum server.
|
|
701
|
-
const merkle = await this.chain.fetchTxMerkle(txid);
|
|
702
|
-
if (!merkle)
|
|
703
|
-
return { confirmed: false };
|
|
704
|
-
// Header lookup can transiently race with electrs's index right
|
|
705
|
-
// after a fresh block — listunspent/get_merkle expose the new
|
|
706
|
-
// height before block.header(N) is queryable. Tolerate that the
|
|
707
|
-
// same way historyToExplorerTxs does: confirmation status and
|
|
708
|
-
// height are still authoritative; only block_time degrades.
|
|
709
|
-
let blockTime = 0;
|
|
710
|
-
try {
|
|
711
|
-
const header = await this.chain.fetchBlockHeader(merkle.blockHeight);
|
|
712
|
-
blockTime = parseBlockHeader(header.hex).timestamp;
|
|
713
|
-
}
|
|
714
|
-
catch (err) {
|
|
715
|
-
if (!isMissingHeightError(err))
|
|
716
|
-
throw err;
|
|
717
|
-
}
|
|
718
|
-
return {
|
|
719
|
-
confirmed: true,
|
|
720
|
-
blockHeight: merkle.blockHeight,
|
|
721
|
-
blockTime,
|
|
722
|
-
};
|
|
723
|
-
}
|
|
724
|
-
async getChainTip() {
|
|
725
|
-
const tip = await this.chain.subscribeHeaders();
|
|
726
|
-
const { hash, timestamp } = parseBlockHeader(tip.hex);
|
|
727
|
-
return {
|
|
728
|
-
height: tip.height,
|
|
729
|
-
time: timestamp,
|
|
730
|
-
hash,
|
|
731
|
-
};
|
|
732
|
-
}
|
|
733
|
-
async watchAddresses(addresses, eventCallback) {
|
|
734
|
-
const scripts = addresses.map((addr) => this.encodeAddress(addr));
|
|
735
|
-
const scriptHashes = scripts.map(toScriptHash);
|
|
736
|
-
// O(1) scripthash → script lookup, kept in sync with the
|
|
737
|
-
// scripts/scriptHashes arrays. Server notifications hit this on
|
|
738
|
-
// every push, so the previous indexOf was O(n) per event.
|
|
739
|
-
const scriptByHash = new Map(scriptHashes.map((h, i) => [h, scripts[i]]));
|
|
740
|
-
// Track known history per script to detect new txs.
|
|
741
|
-
const knownTxids = new Map();
|
|
742
|
-
// Initialize known-set in parallel — for a wallet watching many
|
|
743
|
-
// addresses this avoids n sequential round trips on first call.
|
|
744
|
-
const initialHistories = await Promise.all(scripts.map((s) => this.chain.fetchHistory(s)));
|
|
745
|
-
initialHistories.forEach((history, i) => {
|
|
746
|
-
knownTxids.set(scriptHashes[i], new Set(history.map((h) => h.tx_hash)));
|
|
747
|
-
});
|
|
748
|
-
// Per-scripthash mutex serializing concurrent notifications so
|
|
749
|
-
// two pushes for the same address can't fetch history in parallel
|
|
750
|
-
// and emit duplicate events. Each call chains onto the previous
|
|
751
|
-
// one's tail; failures are swallowed to keep the chain alive.
|
|
752
|
-
const inFlight = new Map();
|
|
753
|
-
const processStatusChange = async (scripthash) => {
|
|
754
|
-
const script = scriptByHash.get(scripthash);
|
|
755
|
-
if (!script)
|
|
756
|
-
return;
|
|
757
|
-
const history = await this.chain.fetchHistory(script);
|
|
758
|
-
const known = knownTxids.get(scripthash) ?? new Set();
|
|
759
|
-
const newEntries = history.filter((entry) => !known.has(entry.tx_hash));
|
|
760
|
-
if (newEntries.length === 0)
|
|
761
|
-
return;
|
|
762
|
-
// Map the new history entries through the same non-verbose
|
|
763
|
-
// pipeline getTransactions uses, so subscribe-driven and
|
|
764
|
-
// poll-driven callers see ExplorerTransactions of identical shape.
|
|
765
|
-
// The dedupe set is updated ONLY after delivery succeeds —
|
|
766
|
-
// otherwise a failed fetch or callback would permanently mark
|
|
767
|
-
// these txids as seen and the next notification wouldn't
|
|
768
|
-
// re-deliver them.
|
|
769
|
-
const explorerTxs = await this.historyToExplorerTxs(newEntries);
|
|
770
|
-
eventCallback(explorerTxs);
|
|
771
|
-
for (const entry of newEntries)
|
|
772
|
-
known.add(entry.tx_hash);
|
|
773
|
-
knownTxids.set(scripthash, known);
|
|
774
|
-
};
|
|
775
|
-
const handleStatusChange = (scripthash) => {
|
|
776
|
-
const previous = inFlight.get(scripthash) ?? Promise.resolve();
|
|
777
|
-
const next = previous.then(() => processStatusChange(scripthash));
|
|
778
|
-
// Keep the chain alive even when one link rejects.
|
|
779
|
-
inFlight.set(scripthash, next.catch(() => undefined));
|
|
780
|
-
return next;
|
|
781
|
-
};
|
|
782
|
-
// Register all subscriptions in parallel; if any one fails, tear
|
|
783
|
-
// down the others so we don't leak server-side subscriptions on
|
|
784
|
-
// a connection the caller never gets a stop() handle for.
|
|
785
|
-
const subscribed = [];
|
|
786
|
-
try {
|
|
787
|
-
await Promise.all(scripts.map(async (script) => {
|
|
788
|
-
await this.chain.subscribeScriptStatus(script, (scripthash, status) => {
|
|
789
|
-
if (status !== null) {
|
|
790
|
-
handleStatusChange(scripthash).catch(console.error);
|
|
791
|
-
}
|
|
792
|
-
});
|
|
793
|
-
subscribed.push(script);
|
|
794
|
-
}));
|
|
795
|
-
}
|
|
796
|
-
catch (err) {
|
|
797
|
-
await Promise.allSettled(subscribed.map((s) => this.chain.unsubscribeScriptStatus(s)));
|
|
798
|
-
throw err;
|
|
799
|
-
}
|
|
800
|
-
return () => {
|
|
801
|
-
for (const script of scripts) {
|
|
802
|
-
this.chain.unsubscribeScriptStatus(script).catch(() => { });
|
|
803
|
-
}
|
|
804
|
-
};
|
|
805
|
-
}
|
|
806
|
-
/** Close the underlying WebSocket connection. */
|
|
807
|
-
async close() {
|
|
808
|
-
await this.chain.close();
|
|
809
|
-
}
|
|
810
|
-
}
|
|
811
|
-
function toScriptHash(script) {
|
|
812
|
-
return hex.encode(sha256(script).reverse());
|
|
813
|
-
}
|
|
814
|
-
function isHeaderSubscribeResult(v) {
|
|
815
|
-
if (typeof v !== "object" || v === null)
|
|
816
|
-
return false;
|
|
817
|
-
const obj = v;
|
|
818
|
-
return typeof obj.height === "number" && typeof obj.hex === "string";
|
|
819
|
-
}
|
|
820
|
-
/**
|
|
821
|
-
* Recognise the "block header not yet indexable" failure shape returned by
|
|
822
|
-
* electrum servers (electrs in particular) when `block.header(N)` runs
|
|
823
|
-
* against a height that's already in `listunspent`/`get_merkle` but hasn't
|
|
824
|
-
* been indexed yet. Surfaced as `missingheight`. Tolerated by callers so
|
|
825
|
-
* the index-lag race doesn't poison confirmed-status reads; genuine
|
|
826
|
-
* failures (auth/network) propagate.
|
|
827
|
-
*/
|
|
828
|
-
function isMissingHeightError(err) {
|
|
829
|
-
const msg = err instanceof Error ? err.message : typeof err === "string" ? err : "";
|
|
830
|
-
return msg.toLowerCase().includes("missingheight");
|
|
831
|
-
}
|
|
832
|
-
/**
|
|
833
|
-
* Recognise the "transaction not in a block yet" failure shape returned by
|
|
834
|
-
* electrum servers when `blockchain.transaction.get_merkle` is asked about a
|
|
835
|
-
* mempool tx. electrs surfaces this as the strings below; Fulcrum mirrors
|
|
836
|
-
* the wording. We match conservatively so genuine errors (auth, network,
|
|
837
|
-
* malformed response) still propagate.
|
|
838
|
-
*/
|
|
839
|
-
function isTxNotInBlockError(err) {
|
|
840
|
-
const msg = err instanceof Error ? err.message : typeof err === "string" ? err : "";
|
|
841
|
-
const normalized = msg.toLowerCase();
|
|
842
|
-
return (normalized.includes("not yet in a block") ||
|
|
843
|
-
normalized.includes("not in a block") ||
|
|
844
|
-
normalized.includes("not in block") ||
|
|
845
|
-
normalized.includes("no confirmed transaction"));
|
|
846
|
-
}
|
|
847
|
-
/**
|
|
848
|
-
* Compute the txid of a serialized transaction. For segwit transactions
|
|
849
|
-
* (every Ark transaction), the broadcast hex includes witness data, but
|
|
850
|
-
* the txid is the double-SHA256 of the legacy (witness-stripped)
|
|
851
|
-
* serialization. Hashing the raw broadcast bytes directly would yield
|
|
852
|
-
* the wtxid instead — silently breaking any caller that tracks the tx
|
|
853
|
-
* by id (round settlement, forfeit monitoring, exit paths).
|
|
854
|
-
*
|
|
855
|
-
* Delegating to `Transaction.fromRaw(...).id` lets @scure/btc-signer
|
|
856
|
-
* handle the witness-stripping correctly.
|
|
857
|
-
*/
|
|
858
|
-
function childTxidFromHex(txHex) {
|
|
859
|
-
const tx = Transaction.fromRaw(hex.decode(txHex), {
|
|
860
|
-
allowUnknownOutputs: true,
|
|
861
|
-
allowUnknownInputs: true,
|
|
862
|
-
});
|
|
863
|
-
return tx.id;
|
|
864
|
-
}
|