@bsv/message-box-client 2.0.5 → 2.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/message-box-client",
3
- "version": "2.0.5",
3
+ "version": "2.0.6",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -83,6 +83,6 @@
83
83
  },
84
84
  "dependencies": {
85
85
  "@bsv/authsocket-client": "^2.0.2",
86
- "@bsv/sdk": "^2.0.4"
86
+ "@bsv/sdk": "^2.0.11"
87
87
  }
88
88
  }
@@ -12,10 +12,14 @@
12
12
 
13
13
  import { MessageBoxClient } from './MessageBoxClient.js'
14
14
  import { PeerMessage } from './types.js'
15
- import { WalletInterface, P2PKH, PublicKey, createNonce, AtomicBEEF, AuthFetch, Base64String, OriginatorDomainNameStringUnder250Bytes } from '@bsv/sdk'
15
+ import { WalletInterface, AtomicBEEF, AuthFetch, Base64String, OriginatorDomainNameStringUnder250Bytes, Brc29RemittanceModule } from '@bsv/sdk'
16
16
 
17
17
  import * as Logger from './Utils/logger.js'
18
18
 
19
+ function toNumberArray (tx: AtomicBEEF): number[] {
20
+ return Array.isArray(tx) ? tx : Array.from(tx)
21
+ }
22
+
19
23
  function safeParse<T> (input: any): T {
20
24
  try {
21
25
  return typeof input === 'string' ? JSON.parse(input) : input
@@ -79,6 +83,7 @@ export class PeerPayClient extends MessageBoxClient {
79
83
  private readonly peerPayWalletClient: WalletInterface
80
84
  private _authFetchInstance?: AuthFetch
81
85
  private readonly messageBox: string
86
+ private readonly settlementModule: Brc29RemittanceModule
82
87
 
83
88
  constructor (config: PeerPayClientConfig) {
84
89
  const { messageBoxHost = 'https://messagebox.babbage.systems', walletClient, enableLogging = false, originator } = config
@@ -89,6 +94,16 @@ export class PeerPayClient extends MessageBoxClient {
89
94
  this.messageBox = config.messageBox ?? STANDARD_PAYMENT_MESSAGEBOX
90
95
  this.peerPayWalletClient = walletClient
91
96
  this.originator = originator
97
+
98
+ this.settlementModule = new Brc29RemittanceModule({
99
+ protocolID: [2, '3241645161d8'],
100
+ labels: ['peerpay'],
101
+ description: 'PeerPay payment',
102
+ outputDescription: 'Payment for PeerPay transaction',
103
+ internalizeProtocol: 'wallet payment',
104
+ refundFeeSatoshis: 1000,
105
+ minRefundSatoshis: 1000
106
+ })
92
107
  }
93
108
 
94
109
  private get authFetchInstance (): AuthFetch {
@@ -115,62 +130,40 @@ export class PeerPayClient extends MessageBoxClient {
115
130
  throw new Error('Invalid payment details: recipient and valid amount are required')
116
131
  };
117
132
 
118
- // Generate derivation paths using correct nonce function
119
- const derivationPrefix = await createNonce(this.peerPayWalletClient, 'self', this.originator)
120
- const derivationSuffix = await createNonce(this.peerPayWalletClient, 'self', this.originator)
121
-
122
- Logger.log(`[PP CLIENT] Derivation Prefix: ${derivationPrefix}`)
123
- Logger.log(`[PP CLIENT] Derivation Suffix: ${derivationSuffix}`)
124
- // Get recipient's derived public key
125
- const { publicKey: derivedKeyResult } = await this.peerPayWalletClient.getPublicKey({
126
- protocolID: [2, '3241645161d8'],
127
- keyID: `${derivationPrefix} ${derivationSuffix}`,
128
- counterparty: payment.recipient
129
- }, this.originator)
130
-
131
- Logger.log(`[PP CLIENT] Derived Public Key: ${derivedKeyResult}`)
132
-
133
- if (derivedKeyResult == null || derivedKeyResult.trim() === '') {
134
- throw new Error('Failed to derive recipient’s public key')
135
- }
136
-
137
- // Create locking script using recipient's public key
138
- const lockingScript = new P2PKH().lock(PublicKey.fromString(derivedKeyResult).toAddress()).toHex()
139
-
140
- Logger.log(`[PP CLIENT] Locking Script: ${lockingScript}`)
141
-
142
- // Create the payment action
143
- const paymentAction = await this.peerPayWalletClient.createAction({
144
- description: 'PeerPay payment',
145
- labels: ['peerpay'],
146
- outputs: [{
147
- satoshis: payment.amount,
148
- lockingScript,
149
- customInstructions: JSON.stringify({
150
- derivationPrefix,
151
- derivationSuffix,
152
- payee: payment.recipient
153
- }),
154
- outputDescription: 'Payment for PeerPay transaction'
155
- }],
156
- options: {
157
- randomizeOutputs: false
133
+ const result = await this.settlementModule.buildSettlement(
134
+ {
135
+ threadId: 'peerpay',
136
+ option: {
137
+ amountSatoshis: payment.amount,
138
+ payee: payment.recipient,
139
+ labels: ['peerpay'],
140
+ description: 'PeerPay payment'
141
+ }
142
+ },
143
+ {
144
+ wallet: this.peerPayWalletClient,
145
+ originator: this.originator,
146
+ now: () => Date.now(),
147
+ logger: Logger
158
148
  }
159
- }, this.originator)
149
+ )
160
150
 
161
- if (paymentAction.tx === undefined) {
162
- throw new Error('Transaction creation failed!')
151
+ if (result.action === 'terminate') {
152
+ if (result.termination.code === 'brc29.public_key_missing') {
153
+ throw new Error('Failed to derive recipient’s public key')
154
+ }
155
+ throw new Error(result.termination.message)
163
156
  }
164
157
 
165
- Logger.log('[PP CLIENT] Payment Action:', paymentAction)
158
+ Logger.log('[PP CLIENT] Payment Action Settlement Artifact:', result.artifact)
166
159
 
167
160
  return {
168
161
  customInstructions: {
169
- derivationPrefix,
170
- derivationSuffix
162
+ derivationPrefix: result.artifact.customInstructions.derivationPrefix as Base64String,
163
+ derivationSuffix: result.artifact.customInstructions.derivationSuffix as Base64String
171
164
  },
172
- transaction: paymentAction.tx,
173
- amount: payment.amount
165
+ transaction: result.artifact.transaction as AtomicBEEF,
166
+ amount: result.artifact.amountSatoshis
174
167
  }
175
168
  }
176
169
 
@@ -290,20 +283,33 @@ export class PeerPayClient extends MessageBoxClient {
290
283
  try {
291
284
  Logger.log(`[PP CLIENT] Processing payment: ${JSON.stringify(payment, null, 2)}`)
292
285
 
293
- const paymentResult = await this.peerPayWalletClient.internalizeAction({
294
- tx: payment.token.transaction,
295
- outputs: [{
296
- paymentRemittance: {
297
- derivationPrefix: payment.token.customInstructions.derivationPrefix,
298
- derivationSuffix: payment.token.customInstructions.derivationSuffix,
299
- senderIdentityKey: payment.sender
300
- },
301
- outputIndex: payment.token.outputIndex ?? STANDARD_PAYMENT_OUTPUT_INDEX,
302
- protocol: 'wallet payment'
303
- }],
304
- labels: ['peerpay'],
305
- description: 'PeerPay Payment'
306
- }, this.originator)
286
+ const acceptResult = await this.settlementModule.acceptSettlement(
287
+ {
288
+ threadId: 'peerpay',
289
+ sender: payment.sender,
290
+ settlement: {
291
+ customInstructions: {
292
+ derivationPrefix: payment.token.customInstructions.derivationPrefix,
293
+ derivationSuffix: payment.token.customInstructions.derivationSuffix
294
+ },
295
+ transaction: toNumberArray(payment.token.transaction),
296
+ amountSatoshis: payment.token.amount,
297
+ outputIndex: payment.token.outputIndex ?? STANDARD_PAYMENT_OUTPUT_INDEX
298
+ }
299
+ },
300
+ {
301
+ wallet: this.peerPayWalletClient,
302
+ originator: this.originator,
303
+ now: () => Date.now(),
304
+ logger: Logger
305
+ }
306
+ )
307
+
308
+ if (acceptResult.action === 'terminate') {
309
+ throw new Error(acceptResult.termination.message)
310
+ }
311
+
312
+ const paymentResult = acceptResult.receiptData?.internalizeResult
307
313
 
308
314
  Logger.log(`[PP CLIENT] Payment internalized successfully: ${JSON.stringify(paymentResult, null, 2)}`)
309
315
  Logger.log(`[PP CLIENT] Acknowledging payment with messageId: ${payment.messageId}`)