edge-currency-accountbased 2.2.4 → 2.4.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/CHANGELOG.md +11 -0
- package/android/src/main/assets/edge-currency-accountbased/234.chunk.js +35 -0
- package/android/src/main/assets/edge-currency-accountbased/248.chunk.js +35 -0
- package/android/src/main/assets/edge-currency-accountbased/336.chunk.js +9 -0
- package/android/src/main/assets/edge-currency-accountbased/504.chunk.js +2 -0
- package/android/src/main/assets/edge-currency-accountbased/612.chunk.js +1 -0
- package/android/src/main/assets/edge-currency-accountbased/{945.chunk.js → 814.chunk.js} +8 -8
- package/android/src/main/assets/edge-currency-accountbased/935.chunk.js +1 -1
- package/android/src/main/assets/edge-currency-accountbased/94.chunk.js +8 -0
- package/android/src/main/assets/edge-currency-accountbased/algorand.chunk.js +2 -2
- package/android/src/main/assets/edge-currency-accountbased/bnb.chunk.js +4 -4
- package/android/src/main/assets/edge-currency-accountbased/edge-currency-accountbased.js +10 -10
- package/android/src/main/assets/edge-currency-accountbased/eos.chunk.js +12 -12
- package/android/src/main/assets/edge-currency-accountbased/ethereum.chunk.js +5 -5
- package/android/src/main/assets/edge-currency-accountbased/filecoin.chunk.js +1 -1
- package/android/src/main/assets/edge-currency-accountbased/fio.chunk.js +2 -2
- package/android/src/main/assets/edge-currency-accountbased/hedera.chunk.js +2 -2
- package/android/src/main/assets/edge-currency-accountbased/polkadot.chunk.js +2 -2
- package/android/src/main/assets/edge-currency-accountbased/ripple.chunk.js +5 -5
- package/android/src/main/assets/edge-currency-accountbased/solana.chunk.js +3 -3
- package/android/src/main/assets/edge-currency-accountbased/stellar.chunk.js +4 -4
- package/android/src/main/assets/edge-currency-accountbased/tezos.chunk.js +3 -3
- package/android/src/main/assets/edge-currency-accountbased/tron.chunk.js +1 -1
- package/android/src/main/assets/edge-currency-accountbased/zcash.chunk.js +2 -2
- package/lib/algorand/AlgorandEngine.js +46 -24
- package/lib/algorand/algorandInfo.js +8 -7
- package/lib/algorand/algorandTestnetInfo.js +8 -7
- package/lib/binance/BinanceEngine.js +19 -17
- package/lib/binance/binanceInfo.js +10 -7
- package/lib/common/types.js +10 -0
- package/lib/common/upgradeMemos.js +52 -0
- package/lib/common/utf8.js +32 -0
- package/lib/common/validateMemos.js +107 -0
- package/lib/eos/EosEngine.js +42 -41
- package/lib/eos/info/eosCommonInfo.js +10 -0
- package/lib/eos/info/eosInfo.js +15 -17
- package/lib/eos/info/telosInfo.js +15 -15
- package/lib/eos/info/waxInfo.js +15 -16
- package/lib/ethereum/EthereumEngine.js +13 -12
- package/lib/ethereum/EthereumNetwork.js +16 -14
- package/lib/ethereum/info/avalancheInfo.js +9 -7
- package/lib/ethereum/info/binancesmartchainInfo.js +9 -7
- package/lib/ethereum/info/celoInfo.js +9 -7
- package/lib/ethereum/info/ethDevInfo.js +9 -7
- package/lib/ethereum/info/ethereumCommonInfo.js +10 -0
- package/lib/ethereum/info/ethereumInfo.js +9 -7
- package/lib/ethereum/info/ethereumclassicInfo.js +9 -7
- package/lib/ethereum/info/ethereumpowInfo.js +9 -7
- package/lib/ethereum/info/fantomInfo.js +9 -7
- package/lib/ethereum/info/goerliInfo.js +9 -7
- package/lib/ethereum/info/kovanInfo.js +9 -7
- package/lib/ethereum/info/optimismInfo.js +9 -7
- package/lib/ethereum/info/polygonInfo.js +9 -7
- package/lib/ethereum/info/pulsechainInfo.js +11 -9
- package/lib/ethereum/info/rinkebyInfo.js +9 -7
- package/lib/ethereum/info/ropstenInfo.js +9 -7
- package/lib/ethereum/info/rskInfo.js +9 -7
- package/lib/ethereum/info/zksyncInfo.js +9 -7
- package/lib/filecoin/FilecoinEngine.js +21 -15
- package/lib/filecoin/filecoinInfo.js +5 -7
- package/lib/fio/FioEngine.js +25 -17
- package/lib/fio/fioInfo.js +8 -6
- package/lib/hedera/HederaEngine.js +22 -18
- package/lib/hedera/hederaInfo.js +11 -8
- package/lib/hedera/hederaTestnetInfo.js +6 -6
- package/lib/index.js +1 -1
- package/lib/piratechain/PiratechainEngine.js +469 -0
- package/lib/piratechain/PiratechainTools.js +239 -0
- package/lib/{zcash → piratechain}/piratechainInfo.js +13 -9
- package/lib/piratechain/piratechainTypes.js +177 -0
- package/lib/polkadot/PolkadotEngine.js +15 -9
- package/lib/polkadot/info/liberlandInfo.js +3 -2
- package/lib/polkadot/info/liberlandTestnetInfo.js +3 -2
- package/lib/polkadot/info/polkadotInfo.js +8 -5
- package/lib/react-native.js +93 -69
- package/lib/ripple/RippleEngine.js +53 -75
- package/lib/ripple/rippleInfo.js +16 -8
- package/lib/solana/SolanaEngine.js +17 -12
- package/lib/solana/solanaInfo.js +9 -5
- package/lib/stellar/StellarEngine.js +31 -22
- package/lib/stellar/stellarInfo.js +16 -7
- package/lib/tezos/TezosEngine.js +17 -11
- package/lib/tezos/tezosInfo.js +8 -5
- package/lib/tron/TronEngine.js +85 -68
- package/lib/tron/tronInfo.js +8 -7
- package/lib/zcash/ZcashEngine.js +86 -48
- package/lib/zcash/ZcashTools.js +10 -34
- package/lib/zcash/zcashInfo.js +8 -7
- package/lib/zcash/zcashTypes.js +2 -13
- package/package.json +4 -6
- package/android/src/main/assets/edge-currency-accountbased/124.chunk.js +0 -35
- package/android/src/main/assets/edge-currency-accountbased/223.chunk.js +0 -9
- package/android/src/main/assets/edge-currency-accountbased/650.chunk.js +0 -1
- package/android/src/main/assets/edge-currency-accountbased/689.chunk.js +0 -8
- package/android/src/main/assets/edge-currency-accountbased/826.chunk.js +0 -35
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _biggystring = require('biggystring');
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
var _types = require('edge-core-js/types');
|
|
14
|
+
|
|
15
|
+
var _CurrencyEngine = require('../common/CurrencyEngine');
|
|
16
|
+
|
|
17
|
+
var _upgradeMemos = require('../common/upgradeMemos');
|
|
18
|
+
var _utils = require('../common/utils');
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
var _piratechainTypes = require('./piratechainTypes');
|
|
33
|
+
|
|
34
|
+
class PiratechainEngine extends _CurrencyEngine.CurrencyEngine
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
{
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
constructor(
|
|
54
|
+
env,
|
|
55
|
+
tools,
|
|
56
|
+
walletInfo,
|
|
57
|
+
opts,
|
|
58
|
+
makeSynchronizer
|
|
59
|
+
) {
|
|
60
|
+
super(env, tools, walletInfo, opts)
|
|
61
|
+
const { networkInfo } = env
|
|
62
|
+
this.pluginId = this.currencyInfo.pluginId
|
|
63
|
+
this.networkInfo = networkInfo
|
|
64
|
+
this.makeSynchronizer = makeSynchronizer
|
|
65
|
+
this.queryMutex = false
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
setOtherData(raw) {
|
|
69
|
+
this.otherData = _piratechainTypes.asPiratechainWalletOtherData.call(void 0, raw)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
initData() {
|
|
73
|
+
const { birthdayHeight, alias } = this.initializer
|
|
74
|
+
|
|
75
|
+
// walletLocalData
|
|
76
|
+
if (this.otherData.blockRange.first === 0) {
|
|
77
|
+
this.otherData.blockRange = {
|
|
78
|
+
first: birthdayHeight,
|
|
79
|
+
last: birthdayHeight
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Engine variables
|
|
84
|
+
this.alias = alias
|
|
85
|
+
this.initialNumBlocksToDownload = -1
|
|
86
|
+
this.synchronizerStatus = 'DISCONNECTED'
|
|
87
|
+
this.availableZatoshi = '0'
|
|
88
|
+
this.progressRatio = 0
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
initSubscriptions() {
|
|
92
|
+
this.synchronizer.on('update', async payload => {
|
|
93
|
+
const { lastDownloadedHeight, scanProgress, networkBlockHeight } = payload
|
|
94
|
+
this.onUpdateBlockHeight(networkBlockHeight)
|
|
95
|
+
this.onUpdateProgress(
|
|
96
|
+
lastDownloadedHeight,
|
|
97
|
+
scanProgress,
|
|
98
|
+
networkBlockHeight
|
|
99
|
+
)
|
|
100
|
+
await this.queryAll()
|
|
101
|
+
})
|
|
102
|
+
this.synchronizer.on('statusChanged', async payload => {
|
|
103
|
+
this.synchronizerStatus = payload.name
|
|
104
|
+
await this.queryAll()
|
|
105
|
+
})
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async queryAll() {
|
|
109
|
+
if (this.queryMutex) return
|
|
110
|
+
this.queryMutex = true
|
|
111
|
+
try {
|
|
112
|
+
await this.queryBalance()
|
|
113
|
+
await this.queryTransactions()
|
|
114
|
+
this.onUpdateTransactions()
|
|
115
|
+
} catch (e) {}
|
|
116
|
+
this.queryMutex = false
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
onUpdateBlockHeight(networkBlockHeight) {
|
|
120
|
+
if (this.walletLocalData.blockHeight !== networkBlockHeight) {
|
|
121
|
+
this.walletLocalData.blockHeight = networkBlockHeight
|
|
122
|
+
this.walletLocalDataDirty = true
|
|
123
|
+
this.currencyEngineCallbacks.onBlockHeightChanged(
|
|
124
|
+
this.walletLocalData.blockHeight
|
|
125
|
+
)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
onUpdateTransactions() {
|
|
130
|
+
if (this.transactionsChangedArray.length > 0) {
|
|
131
|
+
this.currencyEngineCallbacks.onTransactionsChanged(
|
|
132
|
+
this.transactionsChangedArray
|
|
133
|
+
)
|
|
134
|
+
this.transactionsChangedArray = []
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
onUpdateProgress(
|
|
139
|
+
lastDownloadedHeight,
|
|
140
|
+
scanProgress,
|
|
141
|
+
networkBlockHeight
|
|
142
|
+
) {
|
|
143
|
+
if (!this.addressesChecked && !this.isSynced()) {
|
|
144
|
+
// Sync status is split up between downloading blocks (40%), scanning blocks (49.5%),
|
|
145
|
+
// getting balance (0.5%), and querying transactions (10%).
|
|
146
|
+
this.tokenCheckBalanceStatus[this.currencyInfo.currencyCode] =
|
|
147
|
+
(scanProgress * 0.99) / 100
|
|
148
|
+
|
|
149
|
+
let downloadProgress = 0
|
|
150
|
+
if (lastDownloadedHeight > 0) {
|
|
151
|
+
// Initial lastDownloadedHeight value is -1
|
|
152
|
+
const currentNumBlocksToDownload =
|
|
153
|
+
networkBlockHeight - lastDownloadedHeight
|
|
154
|
+
if (this.initialNumBlocksToDownload < 0) {
|
|
155
|
+
this.initialNumBlocksToDownload = currentNumBlocksToDownload
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
downloadProgress =
|
|
159
|
+
currentNumBlocksToDownload === 0 ||
|
|
160
|
+
this.initialNumBlocksToDownload === 0
|
|
161
|
+
? 1
|
|
162
|
+
: 1 - currentNumBlocksToDownload / this.initialNumBlocksToDownload
|
|
163
|
+
}
|
|
164
|
+
this.tokenCheckTransactionsStatus[this.currencyInfo.currencyCode] =
|
|
165
|
+
downloadProgress * 0.8
|
|
166
|
+
|
|
167
|
+
const percent =
|
|
168
|
+
(this.tokenCheckTransactionsStatus[this.currencyInfo.currencyCode] +
|
|
169
|
+
this.tokenCheckBalanceStatus[this.currencyInfo.currencyCode]) /
|
|
170
|
+
2
|
|
171
|
+
if (percent !== this.progressRatio) {
|
|
172
|
+
if (Math.abs(percent - this.progressRatio) > 0.1 || percent === 1) {
|
|
173
|
+
this.progressRatio = percent
|
|
174
|
+
this.updateOnAddressesChecked()
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async startEngine() {
|
|
181
|
+
this.initData()
|
|
182
|
+
// this.synchronizer = await this.makeSynchronizer(this.initializer)
|
|
183
|
+
// await this.synchronizer.start()
|
|
184
|
+
// this.initSubscriptions()
|
|
185
|
+
await super.startEngine()
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
isSynced() {
|
|
189
|
+
// Synchronizer status is updated regularly and should be checked before accessing the db to avoid errors
|
|
190
|
+
return this.synchronizerStatus === 'SYNCED'
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async queryBalance() {
|
|
194
|
+
if (!this.isSynced()) return
|
|
195
|
+
try {
|
|
196
|
+
const balances = await this.synchronizer.getShieldedBalance()
|
|
197
|
+
if (balances.totalZatoshi === '-1') return
|
|
198
|
+
this.availableZatoshi = balances.availableZatoshi
|
|
199
|
+
this.updateBalance(this.currencyInfo.currencyCode, balances.totalZatoshi)
|
|
200
|
+
} catch (e) {
|
|
201
|
+
this.warn('Failed to update balances', e)
|
|
202
|
+
this.updateBalance(this.currencyInfo.currencyCode, '0')
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async queryTransactions() {
|
|
207
|
+
try {
|
|
208
|
+
let first = this.otherData.blockRange.first
|
|
209
|
+
let last = this.otherData.blockRange.last
|
|
210
|
+
while (this.isSynced() && last <= this.walletLocalData.blockHeight) {
|
|
211
|
+
const transactions = await this.synchronizer.getTransactions({
|
|
212
|
+
first,
|
|
213
|
+
last
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
transactions.forEach(tx => this.processTransaction(tx))
|
|
217
|
+
|
|
218
|
+
if (last === this.walletLocalData.blockHeight) {
|
|
219
|
+
first = this.walletLocalData.blockHeight
|
|
220
|
+
this.walletLocalDataDirty = true
|
|
221
|
+
this.tokenCheckTransactionsStatus[this.currencyInfo.currencyCode] = 1
|
|
222
|
+
this.updateOnAddressesChecked()
|
|
223
|
+
break
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
first = last + 1
|
|
227
|
+
last =
|
|
228
|
+
last + this.networkInfo.transactionQueryLimit <
|
|
229
|
+
this.walletLocalData.blockHeight
|
|
230
|
+
? last + this.networkInfo.transactionQueryLimit
|
|
231
|
+
: this.walletLocalData.blockHeight
|
|
232
|
+
|
|
233
|
+
this.otherData.blockRange = {
|
|
234
|
+
first,
|
|
235
|
+
last
|
|
236
|
+
}
|
|
237
|
+
this.walletLocalDataDirty = true
|
|
238
|
+
}
|
|
239
|
+
} catch (e) {
|
|
240
|
+
this.error(
|
|
241
|
+
`Error querying ${this.currencyInfo.currencyCode} transactions `,
|
|
242
|
+
e
|
|
243
|
+
)
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
processTransaction(tx) {
|
|
248
|
+
let netNativeAmount = tx.value
|
|
249
|
+
const ourReceiveAddresses = []
|
|
250
|
+
if (tx.toAddress != null) {
|
|
251
|
+
// check if tx is a spend
|
|
252
|
+
netNativeAmount = `-${_biggystring.add.call(void 0,
|
|
253
|
+
netNativeAmount,
|
|
254
|
+
this.networkInfo.defaultNetworkFee
|
|
255
|
+
)}`
|
|
256
|
+
} else {
|
|
257
|
+
ourReceiveAddresses.push(this.walletInfo.keys.publicKey)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const memos = []
|
|
261
|
+
if (tx.memo != null) {
|
|
262
|
+
memos.push({
|
|
263
|
+
type: 'text',
|
|
264
|
+
value: tx.memo
|
|
265
|
+
})
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const edgeTransaction = {
|
|
269
|
+
txid: tx.rawTransactionId,
|
|
270
|
+
date: tx.blockTimeInSeconds,
|
|
271
|
+
currencyCode: this.currencyInfo.currencyCode,
|
|
272
|
+
blockHeight: tx.minedHeight,
|
|
273
|
+
nativeAmount: netNativeAmount,
|
|
274
|
+
isSend: netNativeAmount.startsWith('-'),
|
|
275
|
+
memos,
|
|
276
|
+
networkFee: this.networkInfo.defaultNetworkFee,
|
|
277
|
+
ourReceiveAddresses, // blank if you sent money otherwise array of addresses that are yours in this transaction
|
|
278
|
+
signedTx: '',
|
|
279
|
+
otherParams: {},
|
|
280
|
+
walletId: this.walletId
|
|
281
|
+
}
|
|
282
|
+
this.addTransaction(this.currencyInfo.currencyCode, edgeTransaction)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async killEngine() {
|
|
286
|
+
// await this.synchronizer.stop()
|
|
287
|
+
await super.killEngine()
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
async clearBlockchainCache() {
|
|
291
|
+
await super.clearBlockchainCache()
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
async resyncBlockchain() {
|
|
295
|
+
// Don't bother stopping and restarting the synchronizer for a resync
|
|
296
|
+
await super.killEngine()
|
|
297
|
+
await this.clearBlockchainCache()
|
|
298
|
+
await this.startEngine()
|
|
299
|
+
// this.synchronizer
|
|
300
|
+
// .rescan(this.walletInfo.keys.birthdayHeight)
|
|
301
|
+
// .catch((e: any) => this.warn('resyncBlockchain failed: ', e))
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
async getMaxSpendable() {
|
|
305
|
+
if (!this.isSynced()) throw new Error('Cannot spend until wallet is synced')
|
|
306
|
+
const spendableBalance = _biggystring.sub.call(void 0,
|
|
307
|
+
this.availableZatoshi,
|
|
308
|
+
this.networkInfo.defaultNetworkFee
|
|
309
|
+
)
|
|
310
|
+
if (_biggystring.lte.call(void 0, spendableBalance, '0')) throw new (0, _types.InsufficientFundsError)()
|
|
311
|
+
|
|
312
|
+
return spendableBalance
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
async makeSpend(edgeSpendInfoIn) {
|
|
316
|
+
edgeSpendInfoIn = _upgradeMemos.upgradeMemos.call(void 0, edgeSpendInfoIn, this.currencyInfo)
|
|
317
|
+
if (!this.isSynced()) throw new Error('Cannot spend until wallet is synced')
|
|
318
|
+
const { edgeSpendInfo, currencyCode } = this.makeSpendCheck(edgeSpendInfoIn)
|
|
319
|
+
const { memos = [] } = edgeSpendInfo
|
|
320
|
+
const spendTarget = edgeSpendInfo.spendTargets[0]
|
|
321
|
+
const { publicAddress, nativeAmount } = spendTarget
|
|
322
|
+
|
|
323
|
+
if (publicAddress == null)
|
|
324
|
+
throw new Error('makeSpend Missing publicAddress')
|
|
325
|
+
if (nativeAmount == null) throw new (0, _types.NoAmountSpecifiedError)()
|
|
326
|
+
|
|
327
|
+
if (_biggystring.eq.call(void 0, nativeAmount, '0')) throw new (0, _types.NoAmountSpecifiedError)()
|
|
328
|
+
|
|
329
|
+
const totalTxAmount = _biggystring.add.call(void 0, nativeAmount, this.networkInfo.defaultNetworkFee)
|
|
330
|
+
|
|
331
|
+
if (
|
|
332
|
+
_biggystring.gt.call(void 0,
|
|
333
|
+
totalTxAmount,
|
|
334
|
+
_nullishCoalesce(this.walletLocalData.totalBalances[this.currencyInfo.currencyCode], () => (
|
|
335
|
+
'0'))
|
|
336
|
+
)
|
|
337
|
+
) {
|
|
338
|
+
throw new (0, _types.InsufficientFundsError)()
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (_biggystring.gt.call(void 0, totalTxAmount, this.availableZatoshi)) {
|
|
342
|
+
throw new (0, _types.InsufficientFundsError)('Amount exceeds available balance')
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// **********************************
|
|
346
|
+
// Create the unsigned EdgeTransaction
|
|
347
|
+
|
|
348
|
+
const spendTargets = edgeSpendInfo.spendTargets.map(si => ({
|
|
349
|
+
uniqueIdentifier: si.uniqueIdentifier,
|
|
350
|
+
memo: si.memo,
|
|
351
|
+
nativeAmount: _nullishCoalesce(si.nativeAmount, () => ( '0')),
|
|
352
|
+
currencyCode,
|
|
353
|
+
publicAddress
|
|
354
|
+
}))
|
|
355
|
+
|
|
356
|
+
const edgeTransaction = {
|
|
357
|
+
txid: '', // txid
|
|
358
|
+
date: 0, // date
|
|
359
|
+
currencyCode, // currencyCode
|
|
360
|
+
blockHeight: 0, // blockHeight
|
|
361
|
+
nativeAmount: `-${totalTxAmount}`, // nativeAmount
|
|
362
|
+
isSend: nativeAmount.startsWith('-'),
|
|
363
|
+
memos,
|
|
364
|
+
networkFee: this.networkInfo.defaultNetworkFee, // networkFee
|
|
365
|
+
ourReceiveAddresses: [], // ourReceiveAddresses
|
|
366
|
+
signedTx: '', // signedTx
|
|
367
|
+
spendTargets,
|
|
368
|
+
walletId: this.walletId
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return edgeTransaction
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
async signTx(edgeTransaction) {
|
|
375
|
+
// Transaction is signed and broadcast at the same time
|
|
376
|
+
return edgeTransaction
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
async broadcastTx(
|
|
380
|
+
edgeTransaction,
|
|
381
|
+
opts
|
|
382
|
+
) {
|
|
383
|
+
if (!this.isSynced())
|
|
384
|
+
throw new Error('Cannot broadcast until wallet is synced')
|
|
385
|
+
const { memos } = edgeTransaction
|
|
386
|
+
const piratechainPrivateKeys = _piratechainTypes.asPiratechainPrivateKeys.call(void 0, this.pluginId)(
|
|
387
|
+
_optionalChain([opts, 'optionalAccess', _ => _.privateKeys])
|
|
388
|
+
)
|
|
389
|
+
if (
|
|
390
|
+
edgeTransaction.spendTargets == null ||
|
|
391
|
+
edgeTransaction.spendTargets.length !== 1
|
|
392
|
+
)
|
|
393
|
+
throw new Error('Invalid spend targets')
|
|
394
|
+
|
|
395
|
+
const memo = _optionalChain([memos, 'access', _2 => _2[0], 'optionalAccess', _3 => _3.type]) === 'text' ? memos[0].value : ''
|
|
396
|
+
const spendTarget = edgeTransaction.spendTargets[0]
|
|
397
|
+
const txParams = {
|
|
398
|
+
zatoshi: _biggystring.sub.call(void 0,
|
|
399
|
+
_biggystring.abs.call(void 0, edgeTransaction.nativeAmount),
|
|
400
|
+
edgeTransaction.networkFee
|
|
401
|
+
),
|
|
402
|
+
toAddress: spendTarget.publicAddress,
|
|
403
|
+
memo,
|
|
404
|
+
fromAccountIndex: 0,
|
|
405
|
+
spendingKey: piratechainPrivateKeys.spendKey
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
try {
|
|
409
|
+
const signedTx = await this.synchronizer.sendToAddress(txParams)
|
|
410
|
+
edgeTransaction.txid = signedTx.txId
|
|
411
|
+
edgeTransaction.signedTx = signedTx.raw
|
|
412
|
+
edgeTransaction.date = Date.now() / 1000
|
|
413
|
+
this.warn(`SUCCESS broadcastTx\n${_utils.cleanTxLogs.call(void 0, edgeTransaction)}`)
|
|
414
|
+
} catch (e) {
|
|
415
|
+
this.warn('FAILURE broadcastTx failed: ', e)
|
|
416
|
+
throw e
|
|
417
|
+
}
|
|
418
|
+
return edgeTransaction
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
getDisplayPrivateSeed(privateKeys) {
|
|
422
|
+
const piratechainPrivateKeys = _piratechainTypes.asPiratechainPrivateKeys.call(void 0, this.pluginId)(
|
|
423
|
+
privateKeys
|
|
424
|
+
)
|
|
425
|
+
return piratechainPrivateKeys.mnemonic
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
getDisplayPublicSeed() {
|
|
429
|
+
return _nullishCoalesce(_optionalChain([this, 'access', _4 => _4.walletInfo, 'access', _5 => _5.keys, 'access', _6 => _6.unifiedViewingKeys, 'optionalAccess', _7 => _7.extfvk]), () => ( ''))
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
async loadEngine() {
|
|
433
|
+
const { walletInfo } = this
|
|
434
|
+
await super.loadEngine()
|
|
435
|
+
this.engineOn = true
|
|
436
|
+
|
|
437
|
+
const { rpcNode } = this.networkInfo
|
|
438
|
+
this.initializer = {
|
|
439
|
+
fullViewingKey: walletInfo.keys.unifiedViewingKeys,
|
|
440
|
+
birthdayHeight: walletInfo.keys.birthdayHeight,
|
|
441
|
+
alias: walletInfo.keys.publicKey,
|
|
442
|
+
...rpcNode
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
} exports.PiratechainEngine = PiratechainEngine;
|
|
446
|
+
async function makeCurrencyEngine(
|
|
447
|
+
env,
|
|
448
|
+
tools,
|
|
449
|
+
walletInfo,
|
|
450
|
+
opts
|
|
451
|
+
) {
|
|
452
|
+
const safeWalletInfo = _piratechainTypes.asSafePiratechainWalletInfo.call(void 0, walletInfo)
|
|
453
|
+
// const { makeSynchronizer } =
|
|
454
|
+
// env.nativeIo['edge-currency-accountbased'].piratechain ?? {}
|
|
455
|
+
|
|
456
|
+
const engine = new PiratechainEngine(
|
|
457
|
+
env,
|
|
458
|
+
tools,
|
|
459
|
+
safeWalletInfo,
|
|
460
|
+
opts,
|
|
461
|
+
// makeSynchronizer
|
|
462
|
+
() => {}
|
|
463
|
+
)
|
|
464
|
+
|
|
465
|
+
// Do any async initialization necessary for the engine
|
|
466
|
+
await engine.loadEngine()
|
|
467
|
+
|
|
468
|
+
return engine
|
|
469
|
+
} exports.makeCurrencyEngine = makeCurrencyEngine;
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _createNamedExportFrom(obj, localName, importedName) { Object.defineProperty(exports, localName, {enumerable: true, get: () => obj[importedName]}); } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }var _biggystring = require('biggystring');
|
|
2
|
+
var _bip39 = require('bip39');
|
|
3
|
+
var _buffer = require('buffer');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
// import {
|
|
17
|
+
// AddressTool as AddressToolType,
|
|
18
|
+
// KeyTool as KeyToolType
|
|
19
|
+
// } from 'react-native-piratechain'
|
|
20
|
+
|
|
21
|
+
// import { asIntegerString } from '../common/types'
|
|
22
|
+
var _uriHelpers = require('../common/uriHelpers');
|
|
23
|
+
var _utils = require('../common/utils');
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
var _piratechainTypes = require('./piratechainTypes');
|
|
31
|
+
|
|
32
|
+
class PiratechainTools {
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
// KeyTool: typeof KeyToolType
|
|
39
|
+
// AddressTool: typeof AddressToolType
|
|
40
|
+
|
|
41
|
+
constructor(env) {
|
|
42
|
+
const { builtinTokens, currencyInfo, io, networkInfo } = env
|
|
43
|
+
this.builtinTokens = builtinTokens
|
|
44
|
+
this.currencyInfo = currencyInfo
|
|
45
|
+
this.io = io
|
|
46
|
+
this.networkInfo = networkInfo
|
|
47
|
+
|
|
48
|
+
const RNAccountbased = env.nativeIo['edge-currency-accountbased']
|
|
49
|
+
if (RNAccountbased == null) {
|
|
50
|
+
throw new Error('Need opts')
|
|
51
|
+
}
|
|
52
|
+
// const { KeyTool, AddressTool } = RNAccountbased.piratechain
|
|
53
|
+
|
|
54
|
+
// this.KeyTool = KeyTool
|
|
55
|
+
// this.AddressTool = AddressTool
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async getNewWalletBirthdayBlockheight() {
|
|
59
|
+
throw new Error('getNewWalletBirthdayBlockheight is disabled')
|
|
60
|
+
// try {
|
|
61
|
+
// return await this.KeyTool.getBirthdayHeight(
|
|
62
|
+
// this.networkInfo.rpcNode.defaultHost,
|
|
63
|
+
// this.networkInfo.rpcNode.defaultPort
|
|
64
|
+
// )
|
|
65
|
+
// } catch (e: any) {
|
|
66
|
+
// return this.networkInfo.defaultBirthday
|
|
67
|
+
// }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async isValidAddress(address) {
|
|
71
|
+
return false
|
|
72
|
+
// return (
|
|
73
|
+
// (await this.AddressTool.isValidShieldedAddress(address)) ||
|
|
74
|
+
// (await this.AddressTool.isValidTransparentAddress(address))
|
|
75
|
+
// )
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// will actually use MNEMONIC version of private key
|
|
79
|
+
async importPrivateKey(
|
|
80
|
+
userInput,
|
|
81
|
+
opts = {}
|
|
82
|
+
) {
|
|
83
|
+
throw new Error('importPrivateKey is disabled')
|
|
84
|
+
// const { pluginId } = this.currencyInfo
|
|
85
|
+
// const isValid = validateMnemonic(userInput)
|
|
86
|
+
// if (!isValid)
|
|
87
|
+
// throw new Error(`Invalid ${this.currencyInfo.currencyCode} mnemonic`)
|
|
88
|
+
// const hexBuffer = await mnemonicToSeed(userInput)
|
|
89
|
+
// const hex = hexBuffer.toString('hex')
|
|
90
|
+
// const spendKey = await this.KeyTool.deriveSpendingKey(
|
|
91
|
+
// hex,
|
|
92
|
+
// this.networkInfo.rpcNode.networkName
|
|
93
|
+
// )
|
|
94
|
+
// if (typeof spendKey !== 'string') throw new Error('Invalid spendKey type')
|
|
95
|
+
|
|
96
|
+
// // Get current network height for the birthday height
|
|
97
|
+
// const currentNetworkHeight = await this.getNewWalletBirthdayBlockheight()
|
|
98
|
+
|
|
99
|
+
// let height = currentNetworkHeight
|
|
100
|
+
|
|
101
|
+
// const { birthdayHeight } = opts
|
|
102
|
+
// if (birthdayHeight != null) {
|
|
103
|
+
// asIntegerString(birthdayHeight)
|
|
104
|
+
|
|
105
|
+
// const birthdayHeightInt = parseInt(birthdayHeight)
|
|
106
|
+
|
|
107
|
+
// if (birthdayHeightInt > currentNetworkHeight) {
|
|
108
|
+
// throw new Error('InvalidBirthdayHeight') // must be less than current block height (assuming query was successful)
|
|
109
|
+
// }
|
|
110
|
+
// height = birthdayHeightInt
|
|
111
|
+
// }
|
|
112
|
+
|
|
113
|
+
// return {
|
|
114
|
+
// [`${pluginId}Mnemonic`]: userInput,
|
|
115
|
+
// [`${pluginId}SpendKey`]: spendKey,
|
|
116
|
+
// [`${pluginId}BirthdayHeight`]: height
|
|
117
|
+
// }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async createPrivateKey(walletType) {
|
|
121
|
+
if (walletType !== this.currencyInfo.walletType) {
|
|
122
|
+
throw new Error('InvalidWalletType')
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const entropy = _buffer.Buffer.from(this.io.random(32)).toString('hex')
|
|
126
|
+
const mnemonic = _bip39.entropyToMnemonic.call(void 0, entropy)
|
|
127
|
+
return await this.importPrivateKey(mnemonic)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async checkPublicKey(publicKey) {
|
|
131
|
+
try {
|
|
132
|
+
_piratechainTypes.asArrrPublicKey.call(void 0, publicKey)
|
|
133
|
+
return true
|
|
134
|
+
} catch (err) {
|
|
135
|
+
return false
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async derivePublicKey(walletInfo) {
|
|
140
|
+
const { pluginId } = this.currencyInfo
|
|
141
|
+
const piratechainPrivateKeys = _piratechainTypes.asPiratechainPrivateKeys.call(void 0, pluginId)(
|
|
142
|
+
walletInfo.keys
|
|
143
|
+
)
|
|
144
|
+
if (walletInfo.type !== this.currencyInfo.walletType) {
|
|
145
|
+
throw new Error('InvalidWalletType')
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
birthdayHeight: piratechainPrivateKeys.birthdayHeight,
|
|
150
|
+
publicKey: 'derivePublicKey is disabled',
|
|
151
|
+
unifiedViewingKeys: {
|
|
152
|
+
extfvk: '',
|
|
153
|
+
extpub: ''
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// const mnemonic = piratechainPrivateKeys.mnemonic
|
|
158
|
+
// if (typeof mnemonic !== 'string') {
|
|
159
|
+
// throw new Error('InvalidMnemonic')
|
|
160
|
+
// }
|
|
161
|
+
// const hexBuffer = await mnemonicToSeed(mnemonic)
|
|
162
|
+
// const hex = hexBuffer.toString('hex')
|
|
163
|
+
// const unifiedViewingKeys: UnifiedViewingKey =
|
|
164
|
+
// await this.KeyTool.deriveViewingKey(
|
|
165
|
+
// hex,
|
|
166
|
+
// this.networkInfo.rpcNode.networkName
|
|
167
|
+
// )
|
|
168
|
+
// const shieldedAddress = await this.AddressTool.deriveShieldedAddress(
|
|
169
|
+
// unifiedViewingKeys.extfvk,
|
|
170
|
+
// this.networkInfo.rpcNode.networkName
|
|
171
|
+
// )
|
|
172
|
+
// return {
|
|
173
|
+
// birthdayHeight: piratechainPrivateKeys.birthdayHeight,
|
|
174
|
+
// publicKey: shieldedAddress,
|
|
175
|
+
// unifiedViewingKeys
|
|
176
|
+
// }
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async parseUri(
|
|
180
|
+
uri,
|
|
181
|
+
currencyCode,
|
|
182
|
+
customTokens
|
|
183
|
+
) {
|
|
184
|
+
const { pluginId } = this.currencyInfo
|
|
185
|
+
const networks = { [pluginId]: true }
|
|
186
|
+
|
|
187
|
+
const {
|
|
188
|
+
edgeParsedUri
|
|
189
|
+
// edgeParsedUri: { publicAddress }
|
|
190
|
+
} = _uriHelpers.parseUriCommon.call(void 0,
|
|
191
|
+
this.currencyInfo,
|
|
192
|
+
uri,
|
|
193
|
+
networks,
|
|
194
|
+
_nullishCoalesce(currencyCode, () => ( this.currencyInfo.currencyCode)),
|
|
195
|
+
customTokens
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
// if (publicAddress == null || !(await this.isValidAddress(publicAddress))) {
|
|
199
|
+
// throw new Error('InvalidPublicAddressError')
|
|
200
|
+
// }
|
|
201
|
+
|
|
202
|
+
return edgeParsedUri
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async encodeUri(
|
|
206
|
+
obj,
|
|
207
|
+
customTokens = []
|
|
208
|
+
) {
|
|
209
|
+
const { pluginId } = this.currencyInfo
|
|
210
|
+
const { nativeAmount, currencyCode /* publicAddress */ } = obj
|
|
211
|
+
|
|
212
|
+
// if (!(await this.isValidAddress(publicAddress))) {
|
|
213
|
+
// throw new Error('InvalidPublicAddressError')
|
|
214
|
+
// }
|
|
215
|
+
|
|
216
|
+
let amount
|
|
217
|
+
if (nativeAmount != null) {
|
|
218
|
+
const denom = _utils.getLegacyDenomination.call(void 0,
|
|
219
|
+
_nullishCoalesce(currencyCode, () => ( this.currencyInfo.currencyCode)),
|
|
220
|
+
this.currencyInfo,
|
|
221
|
+
customTokens
|
|
222
|
+
)
|
|
223
|
+
if (denom == null) {
|
|
224
|
+
throw new Error('InternalErrorInvalidCurrencyCode')
|
|
225
|
+
}
|
|
226
|
+
amount = _biggystring.div.call(void 0, nativeAmount, denom.multiplier, 18)
|
|
227
|
+
}
|
|
228
|
+
const encodedUri = _uriHelpers.encodeUriCommon.call(void 0, obj, `${pluginId}`, amount)
|
|
229
|
+
return encodedUri
|
|
230
|
+
}
|
|
231
|
+
} exports.PiratechainTools = PiratechainTools;
|
|
232
|
+
|
|
233
|
+
async function makeCurrencyTools(
|
|
234
|
+
env
|
|
235
|
+
) {
|
|
236
|
+
return new PiratechainTools(env)
|
|
237
|
+
} exports.makeCurrencyTools = makeCurrencyTools;
|
|
238
|
+
|
|
239
|
+
var _PiratechainEngine = require('./PiratechainEngine'); _createNamedExportFrom(_PiratechainEngine, 'makeCurrencyEngine', 'makeCurrencyEngine');
|