@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/dist/cjs/package.json +2 -2
- package/dist/cjs/src/PeerPayClient.js +58 -57
- package/dist/cjs/src/PeerPayClient.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/PeerPayClient.js +59 -57
- package/dist/esm/src/PeerPayClient.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/types/src/PeerPayClient.d.ts +1 -0
- package/dist/types/src/PeerPayClient.d.ts.map +1 -1
- package/dist/types/tsconfig.types.tsbuildinfo +1 -1
- package/dist/umd/bundle.js +1 -1
- package/package.json +2 -2
- package/src/PeerPayClient.ts +69 -63
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bsv/message-box-client",
|
|
3
|
-
"version": "2.0.
|
|
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.
|
|
86
|
+
"@bsv/sdk": "^2.0.11"
|
|
87
87
|
}
|
|
88
88
|
}
|
package/src/PeerPayClient.ts
CHANGED
|
@@ -12,10 +12,14 @@
|
|
|
12
12
|
|
|
13
13
|
import { MessageBoxClient } from './MessageBoxClient.js'
|
|
14
14
|
import { PeerMessage } from './types.js'
|
|
15
|
-
import { WalletInterface,
|
|
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
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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
|
-
|
|
149
|
+
)
|
|
160
150
|
|
|
161
|
-
if (
|
|
162
|
-
|
|
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:',
|
|
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:
|
|
173
|
-
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
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
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}`)
|