@exodus/bitcoin-plugin 1.0.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/package.json +30 -0
- package/src/create-asset.js +140 -0
- package/src/fee-data.js +16 -0
- package/src/fee-monitor.js +44 -0
- package/src/index.js +6 -0
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@exodus/bitcoin-plugin",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Exodus bitcoin-plugin",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"src",
|
|
8
|
+
"!src/**/__tests__"
|
|
9
|
+
],
|
|
10
|
+
"author": "Exodus",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "restricted"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"test": "jest",
|
|
17
|
+
"lint": "eslint ./src",
|
|
18
|
+
"lint:fix": "yarn lint --fix"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@exodus/asset-lib": "^3.7.1",
|
|
22
|
+
"@exodus/bitcoin-api": "^2.3.4",
|
|
23
|
+
"@exodus/bitcoin-lib": "^2.2.3",
|
|
24
|
+
"minimalistic-assert": "^1.0.1"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@exodus/keychain": "^3.0.0"
|
|
28
|
+
},
|
|
29
|
+
"gitHead": "ae212da85b29d90a3656bcfa034608950f883df1"
|
|
30
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { connectAsset } from '@exodus/assets'
|
|
2
|
+
import _coinInfo from 'coininfo'
|
|
3
|
+
import * as bitcoinjsLib from '@exodus/bitcoinjs-lib'
|
|
4
|
+
import createGetKeyIdentifier from '@exodus/bitcoin-lib/lib/key-identifier'
|
|
5
|
+
|
|
6
|
+
import bip44Constants from '@exodus/bip44-constants/by-ticker'
|
|
7
|
+
import {
|
|
8
|
+
createAccountState,
|
|
9
|
+
createBitcoinMonitor,
|
|
10
|
+
createBtcLikeAddress,
|
|
11
|
+
createBtcLikeKeys,
|
|
12
|
+
eccFactory,
|
|
13
|
+
getBalancesFactory,
|
|
14
|
+
getAddressesFromPrivateKeyFactory,
|
|
15
|
+
getBtcVersions,
|
|
16
|
+
createAndBroadcastTXFactory,
|
|
17
|
+
getFeeEstimatorFactory,
|
|
18
|
+
GetFeeResolver,
|
|
19
|
+
moveFundsFactory,
|
|
20
|
+
InsightAPIClient,
|
|
21
|
+
parseUnsignedTx,
|
|
22
|
+
signTxFactory,
|
|
23
|
+
} from '@exodus/bitcoin-api'
|
|
24
|
+
import { bitcoinFeeDataFactory } from './fee-data'
|
|
25
|
+
import BitcoinFeeMonitor from './fee-monitor'
|
|
26
|
+
import assert from 'minimalistic-assert'
|
|
27
|
+
|
|
28
|
+
export const createBitcoinAssetFactory = ({ base, apiUrl, coinInfoNetwork }) => ({
|
|
29
|
+
allowUnconfirmedRbfEnabledUtxos = false,
|
|
30
|
+
overrideCallback = ({ asset }) => asset,
|
|
31
|
+
} = {}) => {
|
|
32
|
+
assert(base, 'base is required')
|
|
33
|
+
assert(apiUrl, 'apiUrl is required')
|
|
34
|
+
const asset = connectAsset(base)
|
|
35
|
+
const coinInfo = _coinInfo(coinInfoNetwork || base.name)
|
|
36
|
+
|
|
37
|
+
const insightClient = new InsightAPIClient(apiUrl)
|
|
38
|
+
const useBip84 = true
|
|
39
|
+
const useBip86 = true
|
|
40
|
+
|
|
41
|
+
const ecc = eccFactory()
|
|
42
|
+
|
|
43
|
+
const versions = getBtcVersions(coinInfo)
|
|
44
|
+
const address = createBtcLikeAddress({
|
|
45
|
+
versions,
|
|
46
|
+
coinInfo,
|
|
47
|
+
useBip86,
|
|
48
|
+
bitcoinjsLib,
|
|
49
|
+
ecc,
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
const keys = createBtcLikeKeys({ coinInfo, versions, useBip86, ecc })
|
|
53
|
+
const bip44 = bip44Constants['BTC']
|
|
54
|
+
|
|
55
|
+
const useMultipleAddresses = true
|
|
56
|
+
|
|
57
|
+
const features = {
|
|
58
|
+
accountState: true,
|
|
59
|
+
feeMonitor: true,
|
|
60
|
+
feesApi: true,
|
|
61
|
+
multipleAddresses: true,
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const getFeeEstimator = getFeeEstimatorFactory({
|
|
65
|
+
ecc,
|
|
66
|
+
defaultOutputType: 'P2WSH',
|
|
67
|
+
addressApi: address,
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
const { getFee, getAvailableBalance, canBumpTx, getSpendableBalance } = new GetFeeResolver({
|
|
71
|
+
getFeeEstimator,
|
|
72
|
+
allowUnconfirmedRbfEnabledUtxos,
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
const sendTx = createAndBroadcastTXFactory({
|
|
76
|
+
getFeeEstimator,
|
|
77
|
+
allowUnconfirmedRbfEnabledUtxos,
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
const signTx = signTxFactory({
|
|
81
|
+
assetName: asset.name,
|
|
82
|
+
resolvePurpose: address.resolvePurpose,
|
|
83
|
+
keys,
|
|
84
|
+
coinInfo,
|
|
85
|
+
ecc,
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
const moveFunds = moveFundsFactory({
|
|
89
|
+
asset: asset,
|
|
90
|
+
insightClient,
|
|
91
|
+
getFeeEstimator,
|
|
92
|
+
address,
|
|
93
|
+
signTx,
|
|
94
|
+
getAddressesFromPrivateKey: getAddressesFromPrivateKeyFactory({
|
|
95
|
+
purposes: [44, 49, 84, 86],
|
|
96
|
+
keys,
|
|
97
|
+
coinInfo,
|
|
98
|
+
}),
|
|
99
|
+
})
|
|
100
|
+
const feeData = bitcoinFeeDataFactory(asset.currency)
|
|
101
|
+
|
|
102
|
+
const getBalances = getBalancesFactory({ feeData, getSpendableBalance })
|
|
103
|
+
|
|
104
|
+
const api = {
|
|
105
|
+
defaultAddressPath: 'm/0/0',
|
|
106
|
+
broadcastTx: (...args) => insightClient.broadcastTx(...args),
|
|
107
|
+
createFeeMonitor: (args) =>
|
|
108
|
+
new BitcoinFeeMonitor({ ...args, insight: () => insightClient, assetName: asset.name }),
|
|
109
|
+
createHistoryMonitor: (args) => createBitcoinMonitor({ insightClient, apiUrl, ...args }),
|
|
110
|
+
getFeeData: () => feeData,
|
|
111
|
+
getBalances,
|
|
112
|
+
getKeyIdentifier: createGetKeyIdentifier({ bip44 }),
|
|
113
|
+
hasFeature: (feature) => !!features[feature],
|
|
114
|
+
createAccountState: () => createAccountState({ asset: asset }),
|
|
115
|
+
parseUnsignedTx,
|
|
116
|
+
parseTx: ({ unsignedTx }) => parseUnsignedTx(unsignedTx),
|
|
117
|
+
signUnsignedTx: (unsignedTx, privateKey, opts) => signTx({ unsignedTx, privateKey, ...opts }), // legacy
|
|
118
|
+
sendTx,
|
|
119
|
+
signTx,
|
|
120
|
+
getFee,
|
|
121
|
+
moveFunds,
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const fullAsset = {
|
|
125
|
+
...asset,
|
|
126
|
+
address,
|
|
127
|
+
api,
|
|
128
|
+
bip44,
|
|
129
|
+
coinInfo,
|
|
130
|
+
keys,
|
|
131
|
+
useBip84,
|
|
132
|
+
useBip86,
|
|
133
|
+
useMultipleAddresses,
|
|
134
|
+
insightClient,
|
|
135
|
+
canBumpTx,
|
|
136
|
+
getAvailableBalance,
|
|
137
|
+
getSpendableBalance,
|
|
138
|
+
}
|
|
139
|
+
return overrideCallback({ asset: fullAsset })
|
|
140
|
+
}
|
package/src/fee-data.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { FeeData } from '@exodus/asset-lib'
|
|
2
|
+
|
|
3
|
+
export const _defaults = {
|
|
4
|
+
feePerKB: '68000 satoshis',
|
|
5
|
+
fastestFee: '68 satoshis',
|
|
6
|
+
halfHourFee: '60 satoshis',
|
|
7
|
+
hourFee: '50 satoshis',
|
|
8
|
+
nextBlockMinimumFee: '60 satoshis',
|
|
9
|
+
minimumFee: '10 satoshis',
|
|
10
|
+
maxExtraCpfpFee: 100000, // 100k statoshis, this is not a UnitType because it could be overridden with remote configs
|
|
11
|
+
rbfBumpFeeBlocks: 3,
|
|
12
|
+
rbfBumpFeeThreshold: 0.8,
|
|
13
|
+
rbfEnabled: true,
|
|
14
|
+
}
|
|
15
|
+
export const bitcoinFeeDataFactory = (currency) =>
|
|
16
|
+
new FeeData({ config: _defaults, mainKey: 'feePerKB', currency })
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { FeeMonitor } from '@exodus/asset-lib'
|
|
2
|
+
|
|
3
|
+
// clamp to these numbers, sat/B, not sat/KB (!)
|
|
4
|
+
const MIN_FEE = 2 // 2 sat/B, 0.00002 BTC sat/KB
|
|
5
|
+
const MAX_FEE = 2e3 // 2000 sat/B, 0.0200 BTC sat/KB
|
|
6
|
+
|
|
7
|
+
export default class BitcoinFeeMonitor extends FeeMonitor {
|
|
8
|
+
constructor({ updateFee, interval = '1m', assetName = 'bitcoin', insight }) {
|
|
9
|
+
super({ updateFee, interval, assetName })
|
|
10
|
+
this.insightFn = insight
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
get insight() {
|
|
14
|
+
return this.insightFn()
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async fetchFee() {
|
|
18
|
+
const {
|
|
19
|
+
fastestFee,
|
|
20
|
+
halfHourFee,
|
|
21
|
+
hourFee,
|
|
22
|
+
minimumFee,
|
|
23
|
+
nextBlockMinimumFee,
|
|
24
|
+
} = await this.insight.fetchFeeRate()
|
|
25
|
+
return {
|
|
26
|
+
feePerKB: this.toPerKB(fastestFee),
|
|
27
|
+
fastestFee: `${fastestFee} satoshis`,
|
|
28
|
+
halfHourFee: `${halfHourFee} satoshis`,
|
|
29
|
+
hourFee: `${hourFee} satoshis`,
|
|
30
|
+
minimumFee: `${minimumFee} satoshis`,
|
|
31
|
+
nextBlockMinimumFee: nextBlockMinimumFee ? `${nextBlockMinimumFee} satoshis` : undefined,
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
toPerKB(fee) {
|
|
36
|
+
if (fee < MIN_FEE) {
|
|
37
|
+
fee = MIN_FEE
|
|
38
|
+
} else if (fee > MAX_FEE) {
|
|
39
|
+
fee = MAX_FEE
|
|
40
|
+
}
|
|
41
|
+
fee = fee * 1e3
|
|
42
|
+
return `${fee} satoshis`
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { asset as base } from '@exodus/bitcoin-meta'
|
|
2
|
+
import { createBitcoinAssetFactory } from './create-asset'
|
|
3
|
+
export * from './create-asset' // for bitcoinregtest and bitcointestnet
|
|
4
|
+
|
|
5
|
+
const apiUrl = 'https://bitcoin.a.exodus.io/insight/'
|
|
6
|
+
export const createAsset = createBitcoinAssetFactory({ base, apiUrl })
|