Gaëtan Renaudeau
7 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with
234 additions and
238 deletions
-
src/bridge/EthereumJSBridge.js
-
src/bridge/LibcoreBridge.js
-
src/bridge/RippleJSBridge.js
-
src/bridge/UnsupportedBridge.js
-
src/bridge/makeMockBridge.js
-
src/bridge/types.js
-
src/components/modals/AddAccounts/steps/03-step-import.js
|
|
@ -156,7 +156,8 @@ const fetchCurrentBlock = (perCurrencyId => currency => { |
|
|
|
})({}) |
|
|
|
|
|
|
|
const EthereumBridge: WalletBridge<Transaction> = { |
|
|
|
scanAccountsOnDevice(currency, deviceId, { next, complete, error }) { |
|
|
|
scanAccountsOnDevice: (currency, deviceId) => |
|
|
|
Observable.create(o => { |
|
|
|
let finished = false |
|
|
|
const unsubscribe = () => { |
|
|
|
finished = true |
|
|
@ -251,22 +252,22 @@ const EthereumBridge: WalletBridge<Transaction> = { |
|
|
|
.send({ currencyId: currency.id, devicePath: deviceId, path: freshAddressPath }) |
|
|
|
.toPromise() |
|
|
|
const r = await stepAddress(index, res, isStandard) |
|
|
|
if (r.account) next(r.account) |
|
|
|
if (r.account) o.next(r.account) |
|
|
|
if (r.complete) { |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
complete() |
|
|
|
o.complete() |
|
|
|
} catch (e) { |
|
|
|
error(e) |
|
|
|
o.error(e) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
main() |
|
|
|
|
|
|
|
return { unsubscribe } |
|
|
|
}, |
|
|
|
return unsubscribe |
|
|
|
}), |
|
|
|
|
|
|
|
synchronize: ({ freshAddress, blockHeight, currency, operations }) => |
|
|
|
Observable.create(o => { |
|
|
|
|
|
@ -79,14 +79,13 @@ const getFees = async (a, transaction) => { |
|
|
|
} |
|
|
|
|
|
|
|
const LibcoreBridge: WalletBridge<Transaction> = { |
|
|
|
scanAccountsOnDevice(currency, devicePath, observer) { |
|
|
|
scanAccountsOnDevice(currency, devicePath) { |
|
|
|
return libcoreScanAccounts |
|
|
|
.send({ |
|
|
|
devicePath, |
|
|
|
currencyId: currency.id, |
|
|
|
}) |
|
|
|
.pipe(map(decodeAccount)) |
|
|
|
.subscribe(observer) |
|
|
|
}, |
|
|
|
|
|
|
|
synchronize: account => |
|
|
|
|
|
@ -239,7 +239,8 @@ const getServerInfo = (map => endpointConfig => { |
|
|
|
})({}) |
|
|
|
|
|
|
|
const RippleJSBridge: WalletBridge<Transaction> = { |
|
|
|
scanAccountsOnDevice(currency, deviceId, { next, complete, error }) { |
|
|
|
scanAccountsOnDevice: (currency, deviceId) => |
|
|
|
Observable.create(o => { |
|
|
|
let finished = false |
|
|
|
const unsubscribe = () => { |
|
|
|
finished = true |
|
|
@ -282,7 +283,7 @@ const RippleJSBridge: WalletBridge<Transaction> = { |
|
|
|
// account does not exist in Ripple server
|
|
|
|
// we are generating a new account locally
|
|
|
|
if (!legacy) { |
|
|
|
next({ |
|
|
|
o.next({ |
|
|
|
id: accountId, |
|
|
|
xpub: '', |
|
|
|
name: getNewAccountPlaceholderName(currency, index), |
|
|
@ -331,12 +332,12 @@ const RippleJSBridge: WalletBridge<Transaction> = { |
|
|
|
lastSyncDate: new Date(), |
|
|
|
} |
|
|
|
account.operations = transactions.map(txToOperation(account)) |
|
|
|
next(account) |
|
|
|
o.next(account) |
|
|
|
} |
|
|
|
} |
|
|
|
complete() |
|
|
|
o.complete() |
|
|
|
} catch (e) { |
|
|
|
error(e) |
|
|
|
o.error(e) |
|
|
|
} finally { |
|
|
|
api.disconnect() |
|
|
|
} |
|
|
@ -344,8 +345,8 @@ const RippleJSBridge: WalletBridge<Transaction> = { |
|
|
|
|
|
|
|
main() |
|
|
|
|
|
|
|
return { unsubscribe } |
|
|
|
}, |
|
|
|
return unsubscribe |
|
|
|
}), |
|
|
|
|
|
|
|
synchronize: ({ endpointConfig, freshAddress, blockHeight }) => |
|
|
|
Observable.create(o => { |
|
|
|
|
|
@ -10,10 +10,10 @@ const UnsupportedBridge: WalletBridge<*> = { |
|
|
|
o.error(genericError) |
|
|
|
}), |
|
|
|
|
|
|
|
scanAccountsOnDevice(currency, deviceId, { error }) { |
|
|
|
Promise.resolve(genericError).then(error) |
|
|
|
return { unsubscribe() {} } |
|
|
|
}, |
|
|
|
scanAccountsOnDevice: () => |
|
|
|
Observable.create(o => { |
|
|
|
o.error(genericError) |
|
|
|
}), |
|
|
|
|
|
|
|
pullMoreOperations: () => Promise.reject(genericError), |
|
|
|
|
|
|
|
|
|
@ -75,13 +75,14 @@ function makeMockBridge(opts?: Opts): WalletBridge<*> { |
|
|
|
} |
|
|
|
}), |
|
|
|
|
|
|
|
scanAccountsOnDevice(currency, deviceId, { next, complete, error }) { |
|
|
|
scanAccountsOnDevice: currency => |
|
|
|
Observable.create(o => { |
|
|
|
let unsubscribed = false |
|
|
|
|
|
|
|
async function job() { |
|
|
|
if (Math.random() > scanAccountDeviceSuccessRate) { |
|
|
|
await delay(1000) |
|
|
|
if (!unsubscribed) error(new Error('scan failed')) |
|
|
|
if (!unsubscribed) o.error(new Error('scan failed')) |
|
|
|
return |
|
|
|
} |
|
|
|
const nbAccountToGen = 3 |
|
|
@ -92,9 +93,9 @@ function makeMockBridge(opts?: Opts): WalletBridge<*> { |
|
|
|
currency, |
|
|
|
}) |
|
|
|
account.unit = currency.units[0] |
|
|
|
if (!unsubscribed) next(account) |
|
|
|
if (!unsubscribed) o.next(account) |
|
|
|
} |
|
|
|
if (!unsubscribed) complete() |
|
|
|
if (!unsubscribed) o.complete() |
|
|
|
} |
|
|
|
|
|
|
|
job() |
|
|
@ -104,7 +105,7 @@ function makeMockBridge(opts?: Opts): WalletBridge<*> { |
|
|
|
unsubscribed = true |
|
|
|
}, |
|
|
|
} |
|
|
|
}, |
|
|
|
}), |
|
|
|
|
|
|
|
pullMoreOperations: async (_accountId, _desiredCount) => { |
|
|
|
await delay(1000) |
|
|
|
|
|
@ -33,11 +33,7 @@ export interface WalletBridge<Transaction> { |
|
|
|
// the scan can stop once all accounts are discovered.
|
|
|
|
// the function returns a Subscription and you MUST stop everything if it is unsubscribed.
|
|
|
|
// TODO return Observable
|
|
|
|
scanAccountsOnDevice( |
|
|
|
currency: Currency, |
|
|
|
deviceId: DeviceId, |
|
|
|
observer: Observer<Account>, |
|
|
|
): Subscription; |
|
|
|
scanAccountsOnDevice(currency: Currency, deviceId: DeviceId): Observable<Account>; |
|
|
|
|
|
|
|
// synchronize an account. meaning updating the account object with latest state.
|
|
|
|
// function receives the initialAccount object so you can actually know what the user side currently have
|
|
|
|
|
|
@ -17,7 +17,7 @@ import type { StepProps } from '../index' |
|
|
|
|
|
|
|
class StepImport extends PureComponent<StepProps> { |
|
|
|
componentDidMount() { |
|
|
|
this.startScanAccountsDevice() |
|
|
|
this.props.setState({ scanStatus: 'scanning' }) |
|
|
|
} |
|
|
|
|
|
|
|
componentDidUpdate(prevProps: StepProps) { |
|
|
@ -72,9 +72,7 @@ class StepImport extends PureComponent<StepProps> { |
|
|
|
// TODO: use the real device
|
|
|
|
const devicePath = currentDevice.path |
|
|
|
|
|
|
|
setState({ scanStatus: 'scanning' }) |
|
|
|
|
|
|
|
this.scanSubscription = bridge.scanAccountsOnDevice(currency, devicePath, { |
|
|
|
this.scanSubscription = bridge.scanAccountsOnDevice(currency, devicePath).subscribe({ |
|
|
|
next: account => { |
|
|
|
const { scannedAccounts, checkedAccountsIds, existingAccounts } = this.props |
|
|
|
const hasAlreadyBeenScanned = !!scannedAccounts.find(a => account.id === a.id) |
|
|
|