Created
April 16, 2017 07:44
-
-
Save pacwoodson/7f872e1e938d3ae90ef4ec80a3b48e57 to your computer and use it in GitHub Desktop.
JS Stellar Bridge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const EventEmitter = require('events'); | |
const { Server, Keypair, Network } = require('stellar-sdk'); | |
const { getAccount } = require('stellar-toolkit').StellarServer; | |
const { sendPayment } = require('stellar-toolkit').StellarOperations; | |
const { paymentListener } = require('stellar-toolkit').StellarStreamers; | |
const { areSameAssets, AssetInstance } = require('stellar-toolkit').StellarTools; | |
/** | |
* Generic Stellar anchor | |
* Extends EventEmitter | |
* | |
* @param {Object} config | |
* @param {Object} config.server | |
* @param {String} config.server.url | |
* @param {String} config.server.type | |
* @param {String} config.server.options | |
* @param {String} config.seed | |
* @param {String} config.asset_code | |
* @param {String} config.base_account | |
* @param {Function} config.log | |
*/ | |
class StellarAnchor extends EventEmitter { | |
constructor({ | |
server, | |
seed, | |
asset_code, | |
base_account, | |
log, | |
}) { | |
super(); | |
this.fetchPayment = this.fetchPayment.bind(this); | |
this.issueAsset = this.issueAsset.bind(this); | |
this._sendPayment = sendPayment; | |
this._paymentListener = paymentListener; | |
this._getAccount = getAccount; | |
this._log = log || console.log; | |
this.base_account = base_account; | |
this.keypair = Keypair.fromSecret(seed); | |
const asset = { | |
asset_code, | |
asset_issuer: this.keypair.publicKey(), | |
}; | |
this.issuingAsset = AssetInstance(asset); | |
this.stellarServer = new Server(server.url, server.options); | |
if (server.type === 'public') { | |
Network.usePublicNetwork(); | |
} else { | |
Network.useTestNetwork(); | |
} | |
} | |
initialize() { | |
return this.openAccount() | |
.then(() => { | |
this.listenAccount(); | |
this.on('issue', this.issueAsset); | |
}); | |
} | |
openAccount() { | |
const accountId = this.keypair.publicKey(); | |
return this._getAccount(accountId) | |
.then(account => { | |
this._log(`Successfuly loaded account ${accountId}`); | |
return account; | |
}) | |
.catch(e => { | |
throw e.name; | |
}) | |
} | |
fetchPayment(payment) { | |
const accountId = this.keypair.publicKey(); | |
if(payment.to !== accountId) return; | |
if(!areSameAssets(payment, this.issuingAsset)) return; | |
const amount = payment.amount; | |
let memo = { | |
type: payment.memo_type || 'none', | |
value: payment.memo, | |
}; | |
this._log(`Funded ${amount} ${this.issuingAsset.getCode()} from ${payment.from}`); | |
this.emit('fund', { | |
amount, | |
memo, | |
from: payment.from, | |
}); | |
} | |
listenAccount() { | |
// TODO listen from now only | |
this._paymentListener({ | |
accountId: this.keypair.publicKey(), | |
onPayment: this.fetchPayment, | |
server: this.stellarServer, | |
}); | |
} | |
issueAsset({ destination, amount, memo, callback = () => 0 }) { | |
this._getAccount(this.keypair.publicKey()) | |
.then(sourceAccount => | |
this._sendPayment( | |
{ | |
amount, | |
asset: this.issuingAsset, | |
destination: destination || this.base_account, | |
memo, | |
})( | |
{ | |
sourceAccount, | |
keypair: this.keypair, | |
} | |
)) | |
.then(() => { | |
const message = `Issued ${amount} ${this.issuingAsset.getCode()} to ${destination || this.base_account}` | |
this._log(message); | |
callback(null, message); | |
}) | |
.catch(callback); | |
} | |
} | |
module.exports = StellarAnchor; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment