Created
May 17, 2018 14:53
-
-
Save 0xASK/c8cf88630432acdf47a76ab5b28e2f24 to your computer and use it in GitHub Desktop.
Testing lnd asym plugins
This file contains 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 ObjStore = require('ilp-plugin-payment-channel-framework/test/helpers/objStore') | |
const ServerPluginLightning = require('..') | |
const ClientPluginLightning = require('ilp-plugin-lightning') | |
const crypto = require('crypto') | |
const IlpPacket = require('ilp-packet') | |
const uuid = require('uuid/v4') | |
function base64url (buf) { | |
return buf.toString('base64') | |
.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '') | |
} | |
function sha256 (preimage) { | |
return crypto.createHash('sha256').update(preimage).digest() | |
} | |
// Alice | |
const client = new ClientPluginLightning({ | |
server: 'btp+ws://:pass@localhost:9000', | |
maxBalance: '1000000', | |
maxUnsecured: '100000', | |
lndUri: 'localhost:10002', // lnd rpc URI for Bob | |
lndTlsCertPath: process.env.LND_TLS_CERT_PATH, | |
}) | |
// Bob | |
const server = new ServerPluginLightning({ | |
debugHostIldcpInfo: { | |
clientAddress: 'test.server-bob' | |
}, | |
listener: { | |
port: 9000, | |
secret: 'pass' | |
}, | |
incomingSecret: 'pass', | |
port: 9000, | |
prefix: 'g.bitcoin.lightning.', | |
maxBalance: '1000000', | |
maxUnsecured: '100000', | |
lndTlsCertPath: process.env.LND_TLS_CERT_PATH, | |
lndUri: 'localhost:10001', // lnd rpc URI for Bob | |
peerPublicKey: process.env.ALICE_PUBKEY, | |
_store: new ObjStore() | |
}) | |
function doPayment () { | |
const fulfillment = crypto.randomBytes(32) | |
const condition = sha256(fulfillment) | |
return new Promise((resolve, reject) => { | |
server.on('incoming_prepare', transfer => { | |
console.log('Transfer prepared server-side. Condition: ' + transfer.executionCondition) | |
server.fulfillCondition(transfer.id, base64url(fulfillment)) | |
}) | |
client.on('outgoing_fulfill', function (transferId, fulfillmentBase64) { | |
console.log('Transfer executed. Fulfillment: ' + fulfillmentBase64) | |
resolve() | |
}) | |
client.sendTransfer({ | |
ledger: client.getInfo().prefix, | |
from: client.getAccount(), | |
to: server.getAccount(), | |
amount: '12345', | |
executionCondition: base64url(condition), | |
id: uuid(), | |
ilp: base64url(IlpPacket.serializeIlpPayment({ | |
amount: '12345', | |
account: server.getAccount() | |
})), | |
expiresAt: new Date(new Date().getTime() + 1000000).toISOString() | |
}).then(function () { | |
console.log('Transfer prepared client-side, waiting for fulfillment...') | |
}, function (err) { | |
console.error(err.message) | |
}) | |
}) | |
} | |
Promise.all([ client.connect(), server.connect() ]) | |
.then(() => doPayment()) | |
.then(() => new Promise(resolve => setTimeout(resolve, 3000))) | |
.then(() => client.disconnect()) | |
.then(() => server.disconnect()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment