Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Updated code per Marshall
Browse files Browse the repository at this point in the history
- transition is called for all wallets
- sync is now done before transition
- added logic after transition (state updated via onLedgerCallback)

Auditors: @mrose17

Set wallet to v1 if v1 properties are found
(it should be transitioned on launch)

In the roundtrip callback, add `response` to param list when calling during an error

changes default debug mode
  • Loading branch information
bsclifton authored and NejcZdovc committed Oct 6, 2017
1 parent 6ab632a commit fbda568
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 66 deletions.
84 changes: 57 additions & 27 deletions app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ let visitsByPublisher = {}
let bootP
let quitP
const _internal = {
verboseP: process.env.LEDGER_VERBOSE || true,
debugP: process.env.LEDGER_DEBUG || true,
verboseP: process.env.LEDGER_VERBOSE || false,
debugP: process.env.LEDGER_DEBUG || false,
ruleset: {
raw: [],
cooked: []
Expand Down Expand Up @@ -154,14 +154,18 @@ const notifications = {
}, notifications.pollingInterval, state)
},
onLaunch: (state) => {
if (hasFunds(state)) {
// One time conversion of funds
const isNewInstall = state.get('firstRunTimestamp') === state.getIn(['migrations', 'batMercuryTimestamp'])
const hasUpgradedWallet = state.getIn(['migrations', 'batMercuryTimestamp']) !== state.getIn(['migrations', 'btcToBatTimestamp'])
if (!isNewInstall && !hasUpgradedWallet) {
module.exports.transitionWalletToBat(state)
}
if (!getSetting(settings.PAYMENTS_ENABLED)) {
return
}

// One time conversion of wallet
const isNewInstall = state.get('firstRunTimestamp') === state.getIn(['migrations', 'batMercuryTimestamp'])
const hasUpgradedWallet = state.getIn(['migrations', 'batMercuryTimestamp']) !== state.getIn(['migrations', 'btcToBatTimestamp'])
if (!isNewInstall && !hasUpgradedWallet) {
module.exports.transitionWalletToBat(state)
}

if (hasFunds(state)) {
// Don't bother processing the rest, which are only notifications.
if (!getSetting(settings.PAYMENTS_NOTIFICATIONS)) {
return
Expand Down Expand Up @@ -1476,16 +1480,18 @@ const roundtrip = (params, options, callback) => {
console.log('>>> ' + (body || '').split('\n').join('\n>>> '))
}

if (err) return callback(err)
if (err) return callback(err, response)

if (Math.floor(response.statusCode / 100) !== 2) {
return callback(new Error('HTTP response ' + response.statusCode) + ' for ' + params.method + ' ' + params.path)
return callback(
new Error('HTTP response ' + response.statusCode + ' for ' + params.method + ' ' + params.path),
response)
}

try {
payload = rawP ? body : (response.statusCode !== 204) ? JSON.parse(body) : null
} catch (err) {
return callback(err)
return callback(err, response)
}

try {
Expand Down Expand Up @@ -1668,7 +1674,7 @@ const onWalletProperties = (state, body) => {

// Balance
const balance = parseFloat(body.get('balance'))
if (balance > 0) {
if (balance >= 0) {
state = ledgerState.setInfoProp(state, 'balance', balance)
}

Expand Down Expand Up @@ -1946,8 +1952,16 @@ const onInitRead = (state, parsedData) => {
try {
let timeUntilReconcile
clientprep()

const options = Object.assign({}, clientOptions)
try {
if (parsedData.properties.wallet.keychains.user) {
options.version = 'v1'
}
} catch (ex) {}

client = ledgerClient(parsedData.personaId,
underscore.extend(parsedData.options, {roundtrip: roundtrip}, clientOptions),
underscore.extend(parsedData.options, {roundtrip: roundtrip}, options),
parsedData)

// Scenario: User enables Payments, disables it, waits 30+ days, then
Expand All @@ -1970,7 +1984,7 @@ const onInitRead = (state, parsedData) => {
})
}
} catch (ex) {
console.error('ledger client creation error: ', ex)
console.error('ledger client creation error(1): ', ex)
return state
}

Expand Down Expand Up @@ -2222,28 +2236,44 @@ const deleteSynopsis = () => {
synopsis.publishers = {}
}

let newClient = null
const transitionWalletToBat = (state) => {
let newClient
let newPaymentId, result

try {
clientprep()
newClient = ledgerClient(null, underscore.extend({roundtrip: roundtrip}, clientOptions), null)
} catch (ex) {
console.error('exception during ledger client creation: ', ex)
if (newClient === true) return

if (!newClient) {
try {
clientprep()
newClient = ledgerClient(null, underscore.extend({roundtrip: roundtrip}, clientOptions), null)
} catch (ex) {
return console.error('ledger client creation error(2): ', ex)
}
}

newPaymentId = newClient.getPaymentId()
if (!newPaymentId) {
newClient.sync((err, result, delayTime) => {
if (err) {
return console.log('ledger client error(3): ' + JSON.stringify(err, null, 2) + (err.stack ? ('\n' + err.stack) : ''))
}

if (typeof delayTime === 'undefined') delayTime = random.randomInt({ min: 1, max: 500 })

setTimeout(() => transitionWalletToBat(state), delayTime)
})
return
}

try {
// TODO: this line is breaking
// exception during ledger client transition: TypeError: Cannot read property 'wallet' of undefined
client.transition(newClient.properties.wallet.paymentId, (err) => {
client.transition(newPaymentId, (err, properties) => {
if (err) {
console.error('ledger client transition error: ', err)
} else {
// save previous client transactions
newClient.state.transactions = client.state.transactions
// persist client's state, overwriting the old state
result = newClient.transitioned(properties)
client = newClient
newClient = true
appActions.onLedgerCallback(result, random.randomInt({ min: miliseconds.minute, max: 10 * miliseconds.minute }))
appActions.onBitcoinToBatTransitioned()
}
})
Expand Down
66 changes: 33 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"aphrodite": "1.1.0",
"async": "^2.0.1",
"bat-balance": "^0.9.8",
"bat-client": "^0.9.20",
"bat-client": "^0.9.22",
"bat-publisher": "^0.9.4",
"bignumber.js": "^4.0.4",
"bloodhound-js": "brave/bloodhound",
Expand Down
Loading

0 comments on commit fbda568

Please sign in to comment.