feat(completeTx()): Uses user wallet to complete sale

This commit is contained in:
Chris Troutner
2025-06-01 18:18:34 -07:00
parent 625b8895b0
commit ddc70d6fbc
4 changed files with 27 additions and 6 deletions
+3 -1
View File
@@ -41,7 +41,9 @@ const Order = new mongoose.Schema({
nostrEventId: { type: String, required: true }, // Nostr Event Id. nostrEventId: { type: String, required: true }, // Nostr Event Id.
// Additional properties found in createOrder // Additional properties found in createOrder
dataType: { type: String, required: true } dataType: { type: String, required: true },
userId: { type: String, required: true }
}) })
export default mongoose.model('order', Order) export default mongoose.model('order', Order)
+11 -4
View File
@@ -465,7 +465,7 @@ class WalletAdapter {
// Complete the partially signed transaction by signing the first input, // Complete the partially signed transaction by signing the first input,
// then broadcasting the transaction to the network. // then broadcasting the transaction to the network.
async completeTx (hex, hdIndex) { async completeTx (hex, hdIndex, mnemonic) {
try { try {
// Input validation // Input validation
if (!hex || typeof hex !== 'string') { if (!hex || typeof hex !== 'string') {
@@ -474,10 +474,17 @@ class WalletAdapter {
if (typeof hdIndex !== 'number' || hdIndex < 0) { if (typeof hdIndex !== 'number' || hdIndex < 0) {
throw new Error('hdIndex must be a non-negative number!') throw new Error('hdIndex must be a non-negative number!')
} }
if (!mnemonic || typeof mnemonic !== 'string') {
throw new Error('mnemonic must be a string!')
}
// console.log('hex: ', hex) // console.log('hex: ', hex)
console.log('completeTx() hdIndex: ', hdIndex) console.log('completeTx() hdIndex: ', hdIndex)
console.log('completeTx() mnemonic: ', mnemonic)
const bchjs = this.bchWallet.bchjs // Instantiate the user wallet
const userWallet = await this.instanceWallet({ mnemonic })
const bchjs = userWallet.bchjs
// instance of transaction builder // instance of transaction builder
// const transactionBuilder = new bchjs.TransactionBuilder() // const transactionBuilder = new bchjs.TransactionBuilder()
@@ -500,10 +507,10 @@ class WalletAdapter {
// 'mainnet' // 'mainnet'
// ) // )
console.log('completeTx() this.walletInfo: ', this.walletInfo) console.log('completeTx() userWallet.walletInfo: ', userWallet.walletInfo)
// Get the keypair for the address used in the Order // Get the keypair for the address used in the Order
const keyPair = await this.getKeyPair(hdIndex) const keyPair = await userWallet.getKeyPair(hdIndex)
console.log(`maker keyPair: ${JSON.stringify(keyPair, null, 2)}`) console.log(`maker keyPair: ${JSON.stringify(keyPair, null, 2)}`)
const makerECPair = bchjs.ECPair.fromWIF(keyPair.wif) const makerECPair = bchjs.ECPair.fromWIF(keyPair.wif)
+12 -1
View File
@@ -607,8 +607,19 @@ class OfferUseCases {
throw new Error(`The Counter Offer has an output address of ${addrInCounterOffer}, which does not match the Maker address of ${makerAddr} in the Offer.`) throw new Error(`The Counter Offer has an output address of ${addrInCounterOffer}, which does not match the Maker address of ${makerAddr} in the Offer.`)
} }
// Get the User ID from the Order model.
const userId = orderData.userId
// Get the User model from the database.
this.UserModel = this.adapters.localdb.Users
const user = await this.UserModel.findById(userId)
console.log('user: ', user)
// Get the HD index from the Order model.
const hdIndex = orderData.hdIndex
// Sign and broadcast the transaction. // Sign and broadcast the transaction.
const txid = await this.adapters.wallet.completeTx(txHex, orderData.hdIndex) const txid = await this.adapters.wallet.completeTx(txHex, hdIndex, user.mnemonic)
console.log('txid: ', txid) console.log('txid: ', txid)
return txid return txid
+1
View File
@@ -114,6 +114,7 @@ class OrderLib {
// Add P2WDB specific flag for signaling that this is a new offer. // Add P2WDB specific flag for signaling that this is a new offer.
orderEntity.dataType = 'offer' orderEntity.dataType = 'offer'
orderEntity.userId = user._id
// 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.