Guest User

Untitled

a guest
May 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. const ObjStore = require('ilp-plugin-payment-channel-framework/test/helpers/objStore')
  2. const ServerPluginLightning = require('..')
  3. const ClientPluginLightning = require('ilp-plugin-lightning')
  4. const crypto = require('crypto')
  5. const IlpPacket = require('ilp-packet')
  6. const uuid = require('uuid/v4')
  7.  
  8. function base64url (buf) {
  9. return buf.toString('base64')
  10. .replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
  11. }
  12.  
  13. function sha256 (preimage) {
  14. return crypto.createHash('sha256').update(preimage).digest()
  15. }
  16.  
  17. // Alice
  18. const client = new ClientPluginLightning({
  19. server: 'btp+ws://:pass@localhost:9000',
  20. maxBalance: '1000000',
  21. maxUnsecured: '100000',
  22.  
  23. lndUri: 'localhost:10002', // lnd rpc URI for Bob
  24. lndTlsCertPath: process.env.LND_TLS_CERT_PATH,
  25. })
  26.  
  27. // Bob
  28. const server = new ServerPluginLightning({
  29.  
  30. debugHostIldcpInfo: {
  31. clientAddress: 'test.server-bob'
  32. },
  33.  
  34. listener: {
  35. port: 9000,
  36. secret: 'pass'
  37. },
  38. incomingSecret: 'pass',
  39. port: 9000,
  40. prefix: 'g.bitcoin.lightning.',
  41.  
  42. maxBalance: '1000000',
  43. maxUnsecured: '100000',
  44.  
  45. lndTlsCertPath: process.env.LND_TLS_CERT_PATH,
  46. lndUri: 'localhost:10001', // lnd rpc URI for Bob
  47. peerPublicKey: process.env.ALICE_PUBKEY,
  48.  
  49.  
  50. _store: new ObjStore()
  51. })
  52.  
  53. function doPayment () {
  54. const fulfillment = crypto.randomBytes(32)
  55. const condition = sha256(fulfillment)
  56. return new Promise((resolve, reject) => {
  57. server.on('incoming_prepare', transfer => {
  58. console.log('Transfer prepared server-side. Condition: ' + transfer.executionCondition)
  59. server.fulfillCondition(transfer.id, base64url(fulfillment))
  60. })
  61. client.on('outgoing_fulfill', function (transferId, fulfillmentBase64) {
  62. console.log('Transfer executed. Fulfillment: ' + fulfillmentBase64)
  63. resolve()
  64. })
  65.  
  66. client.sendTransfer({
  67. ledger: client.getInfo().prefix,
  68. from: client.getAccount(),
  69. to: server.getAccount(),
  70. amount: '12345',
  71. executionCondition: base64url(condition),
  72. id: uuid(),
  73. ilp: base64url(IlpPacket.serializeIlpPayment({
  74. amount: '12345',
  75. account: server.getAccount()
  76. })),
  77. expiresAt: new Date(new Date().getTime() + 1000000).toISOString()
  78. }).then(function () {
  79. console.log('Transfer prepared client-side, waiting for fulfillment...')
  80. }, function (err) {
  81. console.error(err.message)
  82. })
  83. })
  84. }
  85.  
  86. Promise.all([ client.connect(), server.connect() ])
  87. .then(() => doPayment())
  88. .then(() => new Promise(resolve => setTimeout(resolve, 3000)))
  89. .then(() => client.disconnect())
  90. .then(() => server.disconnect())
Add Comment
Please, Sign In to add comment