mirror of
https://github.com/Permissionless-Software-Foundation/bch-dex.git
synced 2026-09-21 16:52:00 -07:00
Merge pull request #119 from Permissionless-Software-Foundation/dh-npub-order
feat(nostr): Added npub to order
This commit is contained in:
@@ -24,6 +24,7 @@ const Order = new mongoose.Schema({
|
|||||||
p2wdbTxid: { type: String },
|
p2wdbTxid: { type: String },
|
||||||
p2wdbHash: { type: String },
|
p2wdbHash: { type: String },
|
||||||
makerAddr: { type: String, required: true },
|
makerAddr: { type: String, required: true },
|
||||||
|
makerNpub: { type: String, required: true },
|
||||||
|
|
||||||
// Authentication data
|
// Authentication data
|
||||||
signature: { type: String },
|
signature: { type: String },
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import BchNostr from 'bch-nostr'
|
|||||||
import { RelayPool } from 'nostr'
|
import { RelayPool } from 'nostr'
|
||||||
import RetryQueue from '@chris.troutner/retry-queue'
|
import RetryQueue from '@chris.troutner/retry-queue'
|
||||||
import * as nip19 from 'nostr-tools/nip19'
|
import * as nip19 from 'nostr-tools/nip19'
|
||||||
|
import { base58_to_binary as base58Tobinary } from 'base58-js'
|
||||||
|
import { getPublicKey } from 'nostr-tools/pure'
|
||||||
|
|
||||||
class NostrAdapter {
|
class NostrAdapter {
|
||||||
constructor (localConfig = { nostrRelay: '', nostrTopic: '' }) {
|
constructor (localConfig = { nostrRelay: '', nostrTopic: '' }) {
|
||||||
@@ -44,6 +46,7 @@ class NostrAdapter {
|
|||||||
this.npub2pubkey = this.npub2pubkey.bind(this)
|
this.npub2pubkey = this.npub2pubkey.bind(this)
|
||||||
this.readGlobalFeed = this.readGlobalFeed.bind(this)
|
this.readGlobalFeed = this.readGlobalFeed.bind(this)
|
||||||
this.getFollowers = this.getFollowers.bind(this)
|
this.getFollowers = this.getFollowers.bind(this)
|
||||||
|
this.privKeyToNpub = this.privKeyToNpub.bind(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create nostr keys.
|
// Create nostr keys.
|
||||||
@@ -247,6 +250,31 @@ class NostrAdapter {
|
|||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert a BCH private key (WIF) to a Nostr npub.
|
||||||
|
privKeyToNpub (privKey) {
|
||||||
|
try {
|
||||||
|
if (!privKey || typeof privKey !== 'string') {
|
||||||
|
throw new Error('privKey must be a string!')
|
||||||
|
}
|
||||||
|
// Extract the privaty key from the WIF, using this guide:
|
||||||
|
// https://learnmeabitcoin.com/technical/keys/private-key/wif/
|
||||||
|
const wifBuf = base58Tobinary(privKey)
|
||||||
|
const privBuf = wifBuf.slice(1, 33)
|
||||||
|
// console.log('privBuf: ', privBuf)
|
||||||
|
|
||||||
|
const nostrPubKey = getPublicKey(privBuf)
|
||||||
|
// console.log('nostrPubKey: ', nostrPubKey)
|
||||||
|
// Convert pubkey to npub
|
||||||
|
const nostrNpub = nip19.npubEncode(nostrPubKey)
|
||||||
|
// console.log('nostrNpub: ', nostrNpub)
|
||||||
|
|
||||||
|
return nostrNpub
|
||||||
|
} catch (error) {
|
||||||
|
console.log(`Error in nostr.js/privKeyToNpub() ${error.message} `)
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default NostrAdapter
|
export default NostrAdapter
|
||||||
|
|||||||
@@ -202,7 +202,7 @@ class TimerControllers {
|
|||||||
// Remove older deleted chats and posts.
|
// Remove older deleted chats and posts.
|
||||||
async removeOlderDeletedChatsAndPosts () {
|
async removeOlderDeletedChatsAndPosts () {
|
||||||
try {
|
try {
|
||||||
//console.log('removeOlderDeletedChatsAndPosts() Timer Controller executing at ', new Date().toLocaleString())
|
// console.log('removeOlderDeletedChatsAndPosts() Timer Controller executing at ', new Date().toLocaleString())
|
||||||
clearInterval(this.removeOlderDeletedChatsAndPostsInt)
|
clearInterval(this.removeOlderDeletedChatsAndPostsInt)
|
||||||
await this.useCases.nostr.removeOlderDeletedChats()
|
await this.useCases.nostr.removeOlderDeletedChats()
|
||||||
await this.useCases.nostr.removeOlderDeletedPosts()
|
await this.useCases.nostr.removeOlderDeletedPosts()
|
||||||
|
|||||||
@@ -116,6 +116,9 @@ class OrderLib {
|
|||||||
orderEntity.operatorAddress = this.config.operatorAddress
|
orderEntity.operatorAddress = this.config.operatorAddress
|
||||||
orderEntity.operatorPercentage = this.config.operatorPercentage
|
orderEntity.operatorPercentage = this.config.operatorPercentage
|
||||||
|
|
||||||
|
// Convert the Seller's BCH private key to a Nostr npub. This is used to identify the seller in Nostr.
|
||||||
|
orderEntity.makerNpub = this.adapters.nostr.privKeyToNpub(userWallet.walletInfo.privateKey)
|
||||||
|
|
||||||
// Post the new Order information to Nostr under the topic set in the
|
// Post the new Order information to Nostr under the topic set in the
|
||||||
// config file.
|
// config file.
|
||||||
const postObj = {
|
const postObj = {
|
||||||
|
|||||||
@@ -224,4 +224,33 @@ describe('#nostr.js', () => {
|
|||||||
assert.isArray(result)
|
assert.isArray(result)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
describe('#privKeyToNpub', () => {
|
||||||
|
it('should handle error if providedprivKey is not a string', async () => {
|
||||||
|
try {
|
||||||
|
uut.privKeyToNpub()
|
||||||
|
|
||||||
|
assert.fail('Unexpected code path')
|
||||||
|
} catch (err) {
|
||||||
|
// console.log('err: ', err)
|
||||||
|
assert.include(err.message, 'privKey must be a string!')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return npub if privKey is valid', async () => {
|
||||||
|
const result = uut.privKeyToNpub('L2kh34VinQNRq1ad8nnXnGoFrb9pT7j8GJXiT6Scsg18KMxsUVDr')
|
||||||
|
// console.log('result: ', result)
|
||||||
|
assert.isString(result)
|
||||||
|
assert.include(result, 'npub')
|
||||||
|
})
|
||||||
|
it('should handle unexpected error', async () => {
|
||||||
|
try {
|
||||||
|
uut.privKeyToNpub('invalid format')
|
||||||
|
|
||||||
|
assert.fail('Unexpected code path')
|
||||||
|
} catch (err) {
|
||||||
|
// console.log('err: ', err)
|
||||||
|
assert.include(err.message, 'Invalid base58 character')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ describe('#OrderPagination', () => {
|
|||||||
nostrEventId: 'nostrEventId',
|
nostrEventId: 'nostrEventId',
|
||||||
dataType: 'dataType',
|
dataType: 'dataType',
|
||||||
makerAddr: 'makerAddr',
|
makerAddr: 'makerAddr',
|
||||||
|
makerNpub: 'makerNpub',
|
||||||
rateInBaseUnit: 'rateInBaseUnit',
|
rateInBaseUnit: 'rateInBaseUnit',
|
||||||
minUnitsToExchange: 'minUnitsToExchange',
|
minUnitsToExchange: 'minUnitsToExchange',
|
||||||
ticker: 'ticker',
|
ticker: 'ticker',
|
||||||
|
|||||||
@@ -240,7 +240,8 @@ const nostr = {
|
|||||||
read: async () => { return true },
|
read: async () => { return true },
|
||||||
eventId2note: () => { return 'testNoteId' },
|
eventId2note: () => { return 'testNoteId' },
|
||||||
readGlobalFeed: async () => { return [] },
|
readGlobalFeed: async () => { return [] },
|
||||||
getFollowers: async () => { return [] }
|
getFollowers: async () => { return [] },
|
||||||
|
privKeyToNpub: async () => { return 'fakeNpub' }
|
||||||
}
|
}
|
||||||
|
|
||||||
export default { ipfs, localdb, bch, wallet, p2wdb, bchjs, nostr}
|
export default { ipfs, localdb, bch, wallet, p2wdb, bchjs, nostr}
|
||||||
|
|||||||
Reference in New Issue
Block a user