Skip to content

Commit

Permalink
Starting to refactor p2wdb away and replace it with nostr
Browse files Browse the repository at this point in the history
  • Loading branch information
christroutner committed Dec 16, 2024
1 parent 08efd21 commit f2d864c
Show file tree
Hide file tree
Showing 5 changed files with 794 additions and 1,009 deletions.
2 changes: 1 addition & 1 deletion lib/adapters/nostr.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class NostrAdapter {

// Convert an Event ID into a `noteabc..` syntax that Astral expects.
// This can be used to generate a link to Astral to display the post.
eventId2note(eventId) {
eventId2note (eventId) {
return nip19.noteEncode(eventId)
}
}
Expand Down
20 changes: 20 additions & 0 deletions lib/bch-dex-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

// Global npm libraries
import BchNostr from 'bch-nostr'

class BchDexUtil {
constructor (localConfig = {}) {
Expand All @@ -17,8 +18,12 @@ class BchDexUtil {
}
this.p2wdbRead = localConfig.p2wdbRead

// Encapsulate dependencies
this.bchNostr = new BchNostr()

// Bind 'this' object to all subfunctions
this.getEntryFromP2wdb = this.getEntryFromP2wdb.bind(this)
this.getEntryFromNostr = this.getEntryFromNostr.bind(this)
this.validateUtxo = this.validateUtxo.bind(this)
this.getKeyPair = this.getKeyPair.bind(this)
}
Expand All @@ -41,6 +46,21 @@ class BchDexUtil {
return data
}

// Get a trade entry from Nostr
async getEntryFromNostr (eventId) {
try {
const relayWs = 'wss://nostr-relay.psfoundation.info'

const event = await this.bchNostr.read({ eventId, relayWs })
console.log('getEntryFromNostr() event: ', event)

return event.content
} catch (err) {
console.log('Error in bch-dex-lib/lib/bch-dex-util.js getEntryFromNostr()')
throw err
}
}

// Returns true if a UTXO is still unspent. False if not. Requires the
// txid (tx_hash) and vout (tx_pos) of the UTXO.
/*eslint-disable */
Expand Down
12 changes: 9 additions & 3 deletions lib/take.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ class Take {
// Encapsulate dependencies
this.util = new BchDexUtil(localConfig)
this.retryQueue = new RetryQueue({ attempts: 3, retryPeriod: 1000 })
this.nostr = new NostrAdapter()
// console.log('NostrAdapter: ', NostrAdapter)
this.nostr = new NostrAdapter({
nostrRelay: 'wss://nostr-relay.psfoundation.info',
nostrTopic: 'bch-dex-test-topic-01'
})

// Bind 'this' object to all subfunctions
this.takeOffer = this.takeOffer.bind(this)
Expand All @@ -46,6 +50,7 @@ class Take {
try {
// Get the Offer from the P2WDB.
// const offerData = await this.util.getEntryFromP2wdb(cid)
// const offerData = await this.retryQueue.addToQueue(this.util.getEntryFromP2wdb, cid)
const offerData = await this.retryQueue.addToQueue(this.util.getEntryFromP2wdb, cid)
console.log('offerData: ', offerData)

Expand Down Expand Up @@ -123,7 +128,7 @@ class Take {
// Generate a Counter Offer and upload it to Nostr
async uploadCounterOffer2 (inObj = {}) {
try {
const { offerData, partialHex, offerCid } = inObj
const { offerData, partialHex } = inObj

console.log(`uploadCounterOffer2() offerData: ${JSON.stringify(offerData, null, 2)}`)

Expand All @@ -142,10 +147,11 @@ class Take {
data: counterOfferData
}
const resultEventId = await this.nostr.post(JSON.stringify(nostrData))
// const resultEventId = 'fake-event-ID'

const noteId = this.nostr.eventId2note(resultEventId)

return {eventId: resultEventId, nodeId}
return { eventId: resultEventId, noteId }
} catch (err) {
console.error('Error in uploadCounterOffer2(): ', err)
throw err
Expand Down
Loading

0 comments on commit f2d864c

Please sign in to comment.