From ddc70d6fbcfaf9526d3abaf83f56153e2a09c932 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sun, 1 Jun 2025 18:18:34 -0700 Subject: [PATCH] feat(completeTx()): Uses user wallet to complete sale --- src/adapters/localdb/models/order.js | 4 +++- src/adapters/wallet.js | 15 +++++++++++---- src/use-cases/offer/index.js | 13 ++++++++++++- src/use-cases/order.js | 1 + 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/adapters/localdb/models/order.js b/src/adapters/localdb/models/order.js index 4f31e2a..fd0651f 100644 --- a/src/adapters/localdb/models/order.js +++ b/src/adapters/localdb/models/order.js @@ -41,7 +41,9 @@ const Order = new mongoose.Schema({ nostrEventId: { type: String, required: true }, // Nostr Event Id. // 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) diff --git a/src/adapters/wallet.js b/src/adapters/wallet.js index 572252f..18ea4ef 100644 --- a/src/adapters/wallet.js +++ b/src/adapters/wallet.js @@ -465,7 +465,7 @@ class WalletAdapter { // Complete the partially signed transaction by signing the first input, // then broadcasting the transaction to the network. - async completeTx (hex, hdIndex) { + async completeTx (hex, hdIndex, mnemonic) { try { // Input validation if (!hex || typeof hex !== 'string') { @@ -474,10 +474,17 @@ class WalletAdapter { if (typeof hdIndex !== 'number' || hdIndex < 0) { 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('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 // const transactionBuilder = new bchjs.TransactionBuilder() @@ -500,10 +507,10 @@ class WalletAdapter { // '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 - const keyPair = await this.getKeyPair(hdIndex) + const keyPair = await userWallet.getKeyPair(hdIndex) console.log(`maker keyPair: ${JSON.stringify(keyPair, null, 2)}`) const makerECPair = bchjs.ECPair.fromWIF(keyPair.wif) diff --git a/src/use-cases/offer/index.js b/src/use-cases/offer/index.js index 6c15d4f..f1932f8 100644 --- a/src/use-cases/offer/index.js +++ b/src/use-cases/offer/index.js @@ -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.`) } + // 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. - 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) return txid diff --git a/src/use-cases/order.js b/src/use-cases/order.js index 8d766f2..c4bd0b3 100644 --- a/src/use-cases/order.js +++ b/src/use-cases/order.js @@ -114,6 +114,7 @@ class OrderLib { // Add P2WDB specific flag for signaling that this is a new offer. orderEntity.dataType = 'offer' + orderEntity.userId = user._id // Post the new Order information to Nostr under the topic set in the // config file.