Skip to content
This repository has been archived by the owner on Nov 27, 2018. It is now read-only.

Commit

Permalink
0.8.42
Browse files Browse the repository at this point in the history
  • Loading branch information
mrose17 committed Aug 18, 2016
1 parent bd0580f commit d38a8bc
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 318 deletions.
33 changes: 11 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# ledger-client
An example of client code for the [Brave ledger](https://github.com/brave/ledger).

**NOTE WELL:**
you must have CMake installed. The easiest way to do this is:

npm install -g install-cmake

## API

To begin:
Expand All @@ -22,10 +17,6 @@ if `err` is `null`, and `result` is not `null`, then `result` must be put into p
then the operation has succeeded,
regardless of whether `result` is defined or not.)

If `result.thisPayment` is present,
then the user should be directed to the URL `result.thisPayment.paymentURL` --
this allows the use of an external wallet for `adFree` behavior.

- The [Ledger protocol](https://github.com/brave/ledger/tree/master/documentation/Ledger-Principles.md)
requires that the client uses a pseudo-random delay be introduced at certain points during operations.
Accordingly,
Expand All @@ -50,22 +41,10 @@ where the value for `personaId` is the property of the same name associated with
and `options` is:

// all properties are optional...
{ server : 'https://ledger-staging.brave.com'
{ server : 'https://ledger.brave.com'
, debugP : false
, loggingP : false
, verboseP : false
, wallet :
{ // if the wallet property is present, then the address and provider properties must be present
address : 'BTC address'
, provider : 'coinbase'
, credentials :
{ access_token : '...'
, token_type : 'bearer'
, expires_in : ...
, refresh_token : '...'
, scope : 'wallet:accounts:read'
}
}
}

and `state` is either: whatever was previously stored in persistent storage, or `{}`.
Expand Down Expand Up @@ -104,6 +83,10 @@ indicating that persistent storage be updated.
var redirectURL = this.client.getVerificationURL()

### Monthly Reconcilation
`isReadyToReconcile`
`timeUntilReconcile`
`reconcile`

The client should periodical call:

var nowP = client.isReadyToReconcile()
Expand Down Expand Up @@ -145,6 +128,12 @@ Otherwise, if the Bravery `setting` is `adReplacement`, then the client calls:

Regardless of the value of the Bravery `setting`,

## Statistical Voting
`ballots`
`vote`

## Logging
`report`

## Examples
The file `blastoff.js` is a (non-sensical) example of how to use the API --
Expand Down
28 changes: 18 additions & 10 deletions blastoff.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var fs = require('fs')
var path = require('path')
var url = require('url')
var uuid = require('node-uuid')

/*
*
Expand All @@ -23,7 +24,6 @@ var personaID = process.env.PERSONA
var debugP = process.env.DEBUG || false
var loggingP = process.env.LOGGING || false
var verboseP = process.env.VERBOSE || false
var walletFile = process.env.WALLETFILE

while (argv.length > 0) {
if (argv[0].indexOf('-') !== 0) break
Expand All @@ -49,7 +49,6 @@ while (argv.length > 0) {
if (argv[0] === '-f') configFile = argv[1]
else if (argv[0] === '-s') server = argv[1]
else if (argv[0] === '-p') personaID = argv[1].toLowerCase()
else if (argv[0] === '-w') walletFile = argv[1]
else usage()

argv = argv.slice(2)
Expand All @@ -62,7 +61,6 @@ if (server.indexOf('http') !== 0) server = 'https://' + server
server = url.parse(server)

options = { server: server, debugP: debugP, loggingP: loggingP, verboseP: verboseP }
if (walletFile) options.wallet = JSON.parse(fs.readFileSync(walletFile))

/*
*
Expand All @@ -80,7 +78,7 @@ var callback = function (err, result, delayTime) {

if (!result) return run(delayTime)

if (entries) entries.forEach(function (entry) { console.log('*** ' + JSON.stringify(entry)) })
if (entries) entries.forEach((entry) => { console.log('*** ' + JSON.stringify(entry)) })

if (result.paymentInfo) {
console.log(JSON.stringify(result.paymentInfo, null, 2))
Expand All @@ -90,6 +88,13 @@ var callback = function (err, result, delayTime) {
fs.writeFile(configFile, JSON.stringify(result, null, 2), { encoding: 'utf8', mode: parseInt('644', 8) }, function (err) {
if (err) oops(configFile, err)

// at least one transaction
// with all ballots created
// and all ballots submitted
if ((result.transactions) && (result.transactions.length) &&
(result.transactions[0].count === result.transactions[0].votes) &&
(!result.ballots.length)) process.exit(0)

run(delayTime)
})
}
Expand All @@ -98,7 +103,7 @@ fs.readFile(personaID ? '/dev/null' : configFile, { encoding: 'utf8' }, function
var state = err ? null : data ? JSON.parse(data) : {}

client = require('./index.js')(personaID, options, state)
client.sync(callback)
if (client.sync(callback) === true) run(10 * 1000)
})

/*
Expand All @@ -110,15 +115,18 @@ fs.readFile(personaID ? '/dev/null' : configFile, { encoding: 'utf8' }, function
var reconcileP = false

var run = function (delayTime) {
var report = [ { publisher: 'wsj.com', weight: 100 } ]
var viewingId = uuid.v4().toLowerCase()

if (delayTime > 0) return setTimeout(function () { if (client.sync(callback)) return run(0) }, delayTime)
if (delayTime > 0) return setTimeout(() => { if (client.sync(callback)) return run(0) }, delayTime)

if (!client.isReadyToReconcile()) return client.reconcile(report, callback)
if (reconcileP) return console.log('already reconciling.')
if (!client.isReadyToReconcile()) return client.reconcile(viewingId, callback)
if (reconcileP) {
console.log('already reconciling.\n')
return run(60 * 1000)
}

reconcileP = true
client.reconcile(report, callback)
client.reconcile(viewingId, callback)
}

var oops = function (s, err) {
Expand Down
Loading

0 comments on commit d38a8bc

Please sign in to comment.