Making progress on creating a swap offer

This commit is contained in:
Chris Troutner
2021-09-12 06:33:57 -07:00
parent fb77ce7a50
commit b015167a67
3 changed files with 93 additions and 42 deletions
+17 -3
View File
@@ -27,10 +27,24 @@ class OfferLib {
console.log('offerEntity: ', offerEntity)
// Verify that the entry was signed by a specific BCH address.
const isValidSignature = this.bch._verifySignature(offerEntity)
if (!isValidSignature) {
throw new Error('Invalid signature')
// const isValidSignature = this.bch._verifySignature(offerEntity)
// if (!isValidSignature) {
// throw new Error('Invalid signature')
// }
// generate signature.
const now = new Date()
const message = now.toISOString()
const signature = await this.adapters.wallet.generateSignature(message)
console.log('signature: ', signature)
// Burn PSF token to pay for P2WDB write.
const result = await await this.adapters.wallet.burnPsf()
if (!result.success) {
throw new Error('Could not burn PSF token to pay for P2WDB write.')
}
const txid = result.txid
console.log('txid: ', txid)
// Add offer to P2WDB.
+39 -39
View File
@@ -2,13 +2,13 @@
Automated E2E tests for the /offer endpoint.
*/
const config = require('../../../config')
const assert = require('chai').assert
// const config = require('../../../config')
// const assert = require('chai').assert
const axios = require('axios').default
// const axios = require('axios').default
const sinon = require('sinon')
const LOCALHOST = `http://localhost:${config.port}`
// const LOCALHOST = `http://localhost:${config.port}`
// const OfferController = require('../../../src/controllers/rest-api/offer/controller')
// const mockContext = require('../../unit/mocks/ctx-mock').context
@@ -25,40 +25,40 @@ describe('OfferApi', () => {
afterEach(() => sandbox.restore())
describe('POST /offer', () => {
it('should pass data to the handlers', async () => {
try {
const mockOffer = {
lokadId: 'SWP',
messageType: 1,
messageClass: 1,
tokenId: 'token-id',
buyOrSell: 'sell',
rateInSats: 1000,
minSatsToExchange: 1250,
signature:
'H4cRPaAtyNyzG+4Qz+Tp2O7TtFZ7QRsWKKxG71dUZG5xfX0EWRMrBmqM6rH7jToOAT2s9Dm759HMxwP/WTMPzyA=',
sigMsg: 'test',
utxoTxid: 'txid-goes-here',
utxoVout: 1,
offerIpfsId: 'Qmblah',
offerBchAddr:
'bitcoincash:qzxj37k35z6whp4mj4hrdw2vprx4klcydc6ete5xft',
offerPubKey: 'pubkeyInHex'
}
const options = {
method: 'post',
url: `${LOCALHOST}/offer`,
data: { offer: mockOffer }
}
const result = await axios(options)
console.log('result.data: ', result.data)
// assert.isFalse(result.data.success)
} catch (err) {
assert(false, 'Unexpected result')
}
})
// it('should pass data to the handlers', async () => {
// try {
// const mockOffer = {
// lokadId: 'SWP',
// messageType: 1,
// messageClass: 1,
// tokenId: 'token-id',
// buyOrSell: 'sell',
// rateInSats: 1000,
// minSatsToExchange: 1250,
// signature:
// 'H4cRPaAtyNyzG+4Qz+Tp2O7TtFZ7QRsWKKxG71dUZG5xfX0EWRMrBmqM6rH7jToOAT2s9Dm759HMxwP/WTMPzyA=',
// sigMsg: 'test',
// utxoTxid: 'txid-goes-here',
// utxoVout: 1,
// offerIpfsId: 'Qmblah',
// offerBchAddr:
// 'bitcoincash:qzxj37k35z6whp4mj4hrdw2vprx4klcydc6ete5xft',
// offerPubKey: 'pubkeyInHex'
// }
//
// const options = {
// method: 'post',
// url: `${LOCALHOST}/offer`,
// data: { offer: mockOffer }
// }
//
// const result = await axios(options)
// console.log('result.data: ', result.data)
//
// // assert.isFalse(result.data.success)
// } catch (err) {
// assert(false, 'Unexpected result')
// }
// })
})
})
+37
View File
@@ -0,0 +1,37 @@
/*
A manual test for creating an swap offer.
Ensure the REST API is up an running before running this test.
*/
const axios = require('axios')
const LOCALHOST = 'http://localhost:5700'
async function start () {
try {
const mockOffer = {
lokadId: 'SWP',
messageType: 1,
messageClass: 1,
tokenId: 'token-id',
buyOrSell: 'sell',
rateInSats: 1000,
minSatsToExchange: 1250,
utxoTxid: 'txid-goes-here',
utxoVout: 1
}
const options = {
method: 'post',
url: `${LOCALHOST}/offer`,
data: { offer: mockOffer }
}
const result = await axios(options)
console.log('result.data: ', result.data)
} catch (err) {
console.log(err)
}
}
start()