openclaw-messagebox-plugin 0.1.3 → 0.1.4
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/index.d.ts.map +1 -1
- package/dist/index.js +85 -14
- package/dist/index.js.map +1 -1
- package/dist/src/client.d.ts +11 -4
- package/dist/src/client.d.ts.map +1 -1
- package/dist/src/client.js +94 -13
- package/dist/src/client.js.map +1 -1
- package/dist/src/core/config.d.ts +12 -0
- package/dist/src/core/config.d.ts.map +1 -0
- package/dist/src/core/config.js +14 -0
- package/dist/src/core/config.js.map +1 -0
- package/dist/src/core/index.d.ts +26 -0
- package/dist/src/core/index.d.ts.map +1 -0
- package/dist/src/core/index.js +27 -0
- package/dist/src/core/index.js.map +1 -0
- package/dist/src/core/payment.d.ts +17 -0
- package/dist/src/core/payment.d.ts.map +1 -0
- package/dist/src/core/payment.js +95 -0
- package/dist/src/core/payment.js.map +1 -0
- package/dist/src/core/types.d.ts +95 -0
- package/dist/src/core/types.d.ts.map +1 -0
- package/dist/src/core/types.js +5 -0
- package/dist/src/core/types.js.map +1 -0
- package/dist/src/core/verify.d.ts +29 -0
- package/dist/src/core/verify.d.ts.map +1 -0
- package/dist/src/core/verify.js +105 -0
- package/dist/src/core/verify.js.map +1 -0
- package/dist/src/core/wallet.d.ts +100 -0
- package/dist/src/core/wallet.d.ts.map +1 -0
- package/dist/src/core/wallet.js +225 -0
- package/dist/src/core/wallet.js.map +1 -0
- package/index.ts +95 -14
- package/openclaw.plugin.json +2 -1
- package/package.json +7 -6
- package/src/client.ts +106 -14
- package/src/core/config.d.ts +12 -0
- package/src/core/config.ts +21 -0
- package/src/core/index.ts +42 -0
- package/src/core/payment.d.ts +17 -0
- package/src/core/payment.ts +111 -0
- package/src/core/types.d.ts +95 -0
- package/src/core/types.ts +102 -0
- package/src/core/verify.d.ts +29 -0
- package/src/core/verify.ts +119 -0
- package/src/core/wallet.d.ts +100 -0
- package/src/core/wallet.ts +289 -0
|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;CACzB;AAED,uCAAuC;AACvC,MAAM,WAAW,YAAY;IAC3B,6CAA6C;IAC7C,KAAK,EAAE,OAAO,CAAC;IACf,4BAA4B;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,kDAAkD;IAClD,WAAW,EAAE,MAAM,CAAC;IACpB,8CAA8C;IAC9C,MAAM,EAAE,MAAM,EAAE,CAAC;CAClB;AAED,mEAAmE;AACnE,MAAM,WAAW,YAAY;IAC3B,uCAAuC;IACvC,IAAI,EAAE,MAAM,CAAC;IACb,oDAAoD;IACpD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,uDAAuD;IACvD,gBAAgB,EAAE,MAAM,CAAC;IACzB,uDAAuD;IACvD,gBAAgB,EAAE,MAAM,CAAC;IACzB,oDAAoD;IACpD,iBAAiB,EAAE,MAAM,CAAC;IAC1B,kEAAkE;IAClE,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,uCAAuC;AACvC,MAAM,WAAW,YAAY;IAC3B,wCAAwC;IACxC,QAAQ,EAAE,OAAO,CAAC;CACnB;AAED,kFAAkF;AAClF,MAAM,WAAW,cAAc;IAC7B,wDAAwD;IACxD,UAAU,EAAE,MAAM,CAAC;IACnB,yDAAyD;IACzD,WAAW,EAAE,MAAM,CAAC;IACpB,mCAAmC;IACnC,OAAO,EAAE,SAAS,GAAG,SAAS,CAAC;CAChC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/core/types.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @a2a-bsv/core — Payment verification and acceptance helpers.
|
|
3
|
+
*
|
|
4
|
+
* Verification: parse the Atomic BEEF, validate structure.
|
|
5
|
+
* Acceptance: internalize the payment into the recipient wallet via BRC-29
|
|
6
|
+
* wallet payment protocol.
|
|
7
|
+
*/
|
|
8
|
+
import type { SetupWallet } from '@bsv/wallet-toolbox';
|
|
9
|
+
import type { VerifyParams, VerifyResult, AcceptParams, AcceptResult } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Verify an incoming Atomic BEEF payment.
|
|
12
|
+
*
|
|
13
|
+
* This performs structural validation:
|
|
14
|
+
* - Decodes the base64 BEEF
|
|
15
|
+
* - Checks the BEEF is parseable
|
|
16
|
+
* - Checks there is at least one transaction
|
|
17
|
+
* - Runs SPV verification via tx.verify()
|
|
18
|
+
* - Optionally checks the sender identity key
|
|
19
|
+
*/
|
|
20
|
+
export declare function verifyPayment(params: VerifyParams): Promise<VerifyResult>;
|
|
21
|
+
/**
|
|
22
|
+
* Accept (internalize) a verified BRC-29 payment into the recipient's wallet.
|
|
23
|
+
*
|
|
24
|
+
* This calls wallet.internalizeAction with the 'wallet payment' protocol,
|
|
25
|
+
* providing the BRC-29 derivation info so the wallet can derive the correct
|
|
26
|
+
* key and claim the output.
|
|
27
|
+
*/
|
|
28
|
+
export declare function acceptPayment(setup: SetupWallet, params: AcceptParams): Promise<AcceptResult>;
|
|
29
|
+
//# sourceMappingURL=verify.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verify.d.ts","sourceRoot":"","sources":["../../../src/core/verify.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAGzF;;;;;;;;;GASG;AACH,wBAAsB,aAAa,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC,CAkD/E;AAED;;;;;;GAMG;AACH,wBAAsB,aAAa,CACjC,KAAK,EAAE,WAAW,EAClB,MAAM,EAAE,YAAY,GACnB,OAAO,CAAC,YAAY,CAAC,CA2BvB"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @a2a-bsv/core — Payment verification and acceptance helpers.
|
|
3
|
+
*
|
|
4
|
+
* Verification: parse the Atomic BEEF, validate structure.
|
|
5
|
+
* Acceptance: internalize the payment into the recipient wallet via BRC-29
|
|
6
|
+
* wallet payment protocol.
|
|
7
|
+
*/
|
|
8
|
+
import { Beef, Utils } from '@bsv/sdk';
|
|
9
|
+
/**
|
|
10
|
+
* Verify an incoming Atomic BEEF payment.
|
|
11
|
+
*
|
|
12
|
+
* This performs structural validation:
|
|
13
|
+
* - Decodes the base64 BEEF
|
|
14
|
+
* - Checks the BEEF is parseable
|
|
15
|
+
* - Checks there is at least one transaction
|
|
16
|
+
* - Runs SPV verification via tx.verify()
|
|
17
|
+
* - Optionally checks the sender identity key
|
|
18
|
+
*/
|
|
19
|
+
export async function verifyPayment(params) {
|
|
20
|
+
const errors = [];
|
|
21
|
+
let txid = '';
|
|
22
|
+
let outputCount = 0;
|
|
23
|
+
try {
|
|
24
|
+
const binary = Utils.toArray(params.beef, 'base64');
|
|
25
|
+
const beef = Beef.fromBinary(binary);
|
|
26
|
+
if (beef.txs.length === 0) {
|
|
27
|
+
errors.push('BEEF contains no transactions');
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
const lastTx = beef.txs[beef.txs.length - 1];
|
|
31
|
+
txid = lastTx.txid;
|
|
32
|
+
// Parse the atomic transaction to count outputs
|
|
33
|
+
const tx = beef.findAtomicTransaction(txid);
|
|
34
|
+
if (tx) {
|
|
35
|
+
outputCount = tx.outputs.length;
|
|
36
|
+
// Run SPV verification
|
|
37
|
+
try {
|
|
38
|
+
await tx.verify();
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
42
|
+
errors.push(`SPV verification failed: ${message}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
errors.push('Could not find atomic transaction in BEEF');
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
52
|
+
errors.push(`BEEF parse error: ${message}`);
|
|
53
|
+
}
|
|
54
|
+
// Sender validation is independent of BEEF parsing
|
|
55
|
+
if (params.expectedSender) {
|
|
56
|
+
if (!/^0[23][0-9a-fA-F]{64}$/.test(params.expectedSender)) {
|
|
57
|
+
errors.push('expectedSender is not a valid compressed public key');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
valid: errors.length === 0,
|
|
62
|
+
txid,
|
|
63
|
+
outputCount,
|
|
64
|
+
errors,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Accept (internalize) a verified BRC-29 payment into the recipient's wallet.
|
|
69
|
+
*
|
|
70
|
+
* This calls wallet.internalizeAction with the 'wallet payment' protocol,
|
|
71
|
+
* providing the BRC-29 derivation info so the wallet can derive the correct
|
|
72
|
+
* key and claim the output.
|
|
73
|
+
*/
|
|
74
|
+
export async function acceptPayment(setup, params) {
|
|
75
|
+
const desc = normalizeDescription(params.description ?? 'received payment');
|
|
76
|
+
const vout = params.vout ?? 0;
|
|
77
|
+
const binary = Utils.toArray(params.beef, 'base64');
|
|
78
|
+
const args = {
|
|
79
|
+
tx: binary,
|
|
80
|
+
outputs: [
|
|
81
|
+
{
|
|
82
|
+
outputIndex: vout,
|
|
83
|
+
protocol: 'wallet payment',
|
|
84
|
+
paymentRemittance: {
|
|
85
|
+
derivationPrefix: params.derivationPrefix,
|
|
86
|
+
derivationSuffix: params.derivationSuffix,
|
|
87
|
+
senderIdentityKey: params.senderIdentityKey,
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
description: desc,
|
|
92
|
+
};
|
|
93
|
+
const result = await setup.wallet.internalizeAction(args);
|
|
94
|
+
return {
|
|
95
|
+
accepted: result.accepted,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function normalizeDescription(desc) {
|
|
99
|
+
if (desc.length < 5)
|
|
100
|
+
return desc.padEnd(5, ' ');
|
|
101
|
+
if (desc.length > 50)
|
|
102
|
+
return desc.slice(0, 50);
|
|
103
|
+
return desc;
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=verify.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verify.js","sourceRoot":"","sources":["../../../src/core/verify.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAKvC;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,MAAoB;IACtD,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,IAAI,IAAI,GAAG,EAAE,CAAC;IACd,IAAI,WAAW,GAAG,CAAC,CAAC;IAEpB,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;QACpD,MAAM,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;QAErC,IAAI,IAAI,CAAC,GAAG,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC1B,MAAM,CAAC,IAAI,CAAC,+BAA+B,CAAC,CAAC;QAC/C,CAAC;aAAM,CAAC;YACN,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;YAC7C,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;YAEnB,gDAAgD;YAChD,MAAM,EAAE,GAAG,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,CAAC;YAC5C,IAAI,EAAE,EAAE,CAAC;gBACP,WAAW,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC;gBAEhC,uBAAuB;gBACvB,IAAI,CAAC;oBACH,MAAM,EAAE,CAAC,MAA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QAAQ,CAAC,CAAC;IAEpD,MAAM,IAAI,GAA0B;QAClC,EAAE,EAAE,MAAM;QACV,OAAO,EAAE;YACP;gBACE,WAAW,EAAE,IAAI;gBACjB,QAAQ,EAAE,gBAAgB;gBAC1B,iBAAiB,EAAE;oBACjB,gBAAgB,EAAE,MAAM,CAAC,gBAAgB;oBACzC,gBAAgB,EAAE,MAAM,CAAC,gBAAgB;oBACzC,iBAAiB,EAAE,MAAM,CAAC,iBAAiB;iBAC5C;aACF;SACF;QACD,WAAW,EAAE,IAAI;KAClB,CAAC;IAEF,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,MAAM,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC;IAE1D,OAAO;QACL,QAAQ,EAAE,MAAM,CAAC,QAAQ;KAC1B,CAAC;AACJ,CAAC;AAED,SAAS,oBAAoB,CAAC,IAAY;IACxC,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC;QAAE,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IAChD,IAAI,IAAI,CAAC,MAAM,GAAG,EAAE;QAAE,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAC/C,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @a2a-bsv/core — BSVAgentWallet
|
|
3
|
+
*
|
|
4
|
+
* High-level wallet class for AI agent-to-agent BSV payments.
|
|
5
|
+
* Wraps @bsv/wallet-toolbox's Wallet + StorageKnex with a clean,
|
|
6
|
+
* minimal API surface designed for automated agent use.
|
|
7
|
+
*/
|
|
8
|
+
import type { SetupWallet } from '@bsv/wallet-toolbox';
|
|
9
|
+
import type { WalletConfig, PaymentParams, PaymentResult, VerifyParams, VerifyResult, AcceptParams, AcceptResult } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* BSVAgentWallet — the primary class for agent-to-agent BSV payments.
|
|
12
|
+
*
|
|
13
|
+
* Usage:
|
|
14
|
+
* ```ts
|
|
15
|
+
* // Create a new wallet (generates keys)
|
|
16
|
+
* const wallet = await BSVAgentWallet.load({ network: 'testnet', storageDir: './agent-wallet' });
|
|
17
|
+
*
|
|
18
|
+
* // Load an existing wallet
|
|
19
|
+
* const wallet = await BSVAgentWallet.load({ network: 'testnet', storageDir: './agent-wallet' });
|
|
20
|
+
*
|
|
21
|
+
* // Make a payment
|
|
22
|
+
* const payment = await wallet.createPayment({ to: recipientPubKey, satoshis: 500 });
|
|
23
|
+
*
|
|
24
|
+
* // Verify and accept a payment
|
|
25
|
+
* const verification = wallet.verifyPayment({ beef: payment.beef });
|
|
26
|
+
* if (verification.valid) {
|
|
27
|
+
* await wallet.acceptPayment({ beef: payment.beef, ...derivationInfo });
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare class BSVAgentWallet {
|
|
32
|
+
/** @internal — exposed for advanced operations (e.g. direct internalizeAction) */
|
|
33
|
+
readonly _setup: SetupWallet;
|
|
34
|
+
private constructor();
|
|
35
|
+
/**
|
|
36
|
+
* Create a new agent wallet. Generates a fresh root key and persists it.
|
|
37
|
+
* The SQLite database and identity file are written to `config.storageDir`.
|
|
38
|
+
*/
|
|
39
|
+
private static create;
|
|
40
|
+
/**
|
|
41
|
+
* Load an existing agent wallet from its storage directory.
|
|
42
|
+
* Reads the persisted identity file and re-initializes the wallet.
|
|
43
|
+
*/
|
|
44
|
+
static load(config: WalletConfig): Promise<BSVAgentWallet>;
|
|
45
|
+
/**
|
|
46
|
+
* Get this wallet's public identity key (compressed hex, 33 bytes).
|
|
47
|
+
* This is the key other agents use to send payments to you.
|
|
48
|
+
*/
|
|
49
|
+
getIdentityKey(): Promise<string>;
|
|
50
|
+
/**
|
|
51
|
+
* Get the wallet's current balance in satoshis.
|
|
52
|
+
*
|
|
53
|
+
* Uses the BRC-100 wallet's balance method which sums spendable outputs
|
|
54
|
+
* in the default basket.
|
|
55
|
+
*/
|
|
56
|
+
getBalance(): Promise<number>;
|
|
57
|
+
/**
|
|
58
|
+
* Cleanly shut down the wallet, releasing database connections and
|
|
59
|
+
* stopping the background monitor.
|
|
60
|
+
*/
|
|
61
|
+
destroy(): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* Build a BRC-29 payment to another agent.
|
|
64
|
+
*
|
|
65
|
+
* The transaction is created with `noSend: true` — the sender does NOT
|
|
66
|
+
* broadcast it. Instead, the Atomic BEEF and derivation metadata are
|
|
67
|
+
* returned so they can be transmitted to the recipient, who will
|
|
68
|
+
* verify and internalize (broadcast) the payment.
|
|
69
|
+
*
|
|
70
|
+
* @param params.to — Recipient's compressed public key (hex).
|
|
71
|
+
* @param params.satoshis — Amount in satoshis.
|
|
72
|
+
* @param params.description — Optional human-readable note.
|
|
73
|
+
*/
|
|
74
|
+
createPayment(params: PaymentParams): Promise<PaymentResult>;
|
|
75
|
+
/**
|
|
76
|
+
* Verify an incoming Atomic BEEF payment.
|
|
77
|
+
*
|
|
78
|
+
* This performs structural validation and SPV verification via tx.verify().
|
|
79
|
+
*/
|
|
80
|
+
verifyPayment(params: VerifyParams): Promise<VerifyResult>;
|
|
81
|
+
/**
|
|
82
|
+
* Accept (internalize) a verified payment into this wallet.
|
|
83
|
+
*
|
|
84
|
+
* Uses the BRC-29 wallet payment protocol to derive the correct key
|
|
85
|
+
* and claim the output. This triggers SPV verification and, if the
|
|
86
|
+
* transaction hasn't been broadcast yet, broadcasts it.
|
|
87
|
+
*/
|
|
88
|
+
acceptPayment(params: AcceptParams): Promise<AcceptResult>;
|
|
89
|
+
/** Get the underlying wallet-toolbox SetupWallet for advanced operations. */
|
|
90
|
+
getSetup(): SetupWallet;
|
|
91
|
+
/**
|
|
92
|
+
* Internal: manually construct a BRC-100 wallet backed by SQLite.
|
|
93
|
+
*
|
|
94
|
+
* We build this by hand instead of using Setup.createWalletSQLite because
|
|
95
|
+
* the toolbox has a bug where its internal randomBytesHex is a stub.
|
|
96
|
+
* We use the same components but wire them up correctly.
|
|
97
|
+
*/
|
|
98
|
+
private static buildSetup;
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=wallet.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet.d.ts","sourceRoot":"","sources":["../../../src/core/wallet.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAYH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAKvD,OAAO,KAAK,EACV,YAAY,EAEZ,aAAa,EACb,aAAa,EACb,YAAY,EACZ,YAAY,EACZ,YAAY,EACZ,YAAY,EACb,MAAM,YAAY,CAAC;AAQpB;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,cAAc;IACzB,kFAAkF;IAClF,SAAgB,MAAM,EAAE,WAAW,CAAC;IAEpC,OAAO;IAQP;;;OAGG;mBACkB,MAAM;IAwB3B;;;OAGG;WACU,IAAI,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,cAAc,CAAC;IAoBhE;;;OAGG;IACG,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC;IAIvC;;;;;OAKG;IACG,UAAU,IAAI,OAAO,CAAC,MAAM,CAAC;IAInC;;;OAGG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAQ9B;;;;;;;;;;;OAWG;IACG,aAAa,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;IAQlE;;;;OAIG;IACG,aAAa,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;IAIhE;;;;;;OAMG;IACG,aAAa,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;IAQhE,6EAA6E;IAC7E,QAAQ,IAAI,WAAW;IAQvB;;;;;;OAMG;mBACkB,UAAU;CAyEhC"}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @a2a-bsv/core — BSVAgentWallet
|
|
3
|
+
*
|
|
4
|
+
* High-level wallet class for AI agent-to-agent BSV payments.
|
|
5
|
+
* Wraps @bsv/wallet-toolbox's Wallet + StorageKnex with a clean,
|
|
6
|
+
* minimal API surface designed for automated agent use.
|
|
7
|
+
*/
|
|
8
|
+
import { PrivateKey, CachedKeyDeriver } from '@bsv/sdk';
|
|
9
|
+
import { Wallet, WalletStorageManager, Services, Monitor, StorageKnex, randomBytesHex, ChaintracksServiceClient, } from '@bsv/wallet-toolbox';
|
|
10
|
+
import knexLib from 'knex';
|
|
11
|
+
import * as path from 'node:path';
|
|
12
|
+
import * as fs from 'node:fs';
|
|
13
|
+
import { toChain, DEFAULT_TAAL_API_KEYS, DEFAULT_DB_NAME } from './config.js';
|
|
14
|
+
import { buildPayment } from './payment.js';
|
|
15
|
+
import { verifyPayment, acceptPayment } from './verify.js';
|
|
16
|
+
/** Filename for the persisted wallet identity JSON. */
|
|
17
|
+
const IDENTITY_FILE = 'wallet-identity.json';
|
|
18
|
+
/**
|
|
19
|
+
* BSVAgentWallet — the primary class for agent-to-agent BSV payments.
|
|
20
|
+
*
|
|
21
|
+
* Usage:
|
|
22
|
+
* ```ts
|
|
23
|
+
* // Create a new wallet (generates keys)
|
|
24
|
+
* const wallet = await BSVAgentWallet.load({ network: 'testnet', storageDir: './agent-wallet' });
|
|
25
|
+
*
|
|
26
|
+
* // Load an existing wallet
|
|
27
|
+
* const wallet = await BSVAgentWallet.load({ network: 'testnet', storageDir: './agent-wallet' });
|
|
28
|
+
*
|
|
29
|
+
* // Make a payment
|
|
30
|
+
* const payment = await wallet.createPayment({ to: recipientPubKey, satoshis: 500 });
|
|
31
|
+
*
|
|
32
|
+
* // Verify and accept a payment
|
|
33
|
+
* const verification = wallet.verifyPayment({ beef: payment.beef });
|
|
34
|
+
* if (verification.valid) {
|
|
35
|
+
* await wallet.acceptPayment({ beef: payment.beef, ...derivationInfo });
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export class BSVAgentWallet {
|
|
40
|
+
/** @internal — exposed for advanced operations (e.g. direct internalizeAction) */
|
|
41
|
+
_setup;
|
|
42
|
+
constructor(setup) {
|
|
43
|
+
this._setup = setup;
|
|
44
|
+
}
|
|
45
|
+
// ---------------------------------------------------------------------------
|
|
46
|
+
// Factory methods
|
|
47
|
+
// ---------------------------------------------------------------------------
|
|
48
|
+
/**
|
|
49
|
+
* Create a new agent wallet. Generates a fresh root key and persists it.
|
|
50
|
+
* The SQLite database and identity file are written to `config.storageDir`.
|
|
51
|
+
*/
|
|
52
|
+
static async create(config) {
|
|
53
|
+
// Generate a new root key (or use one provided in config)
|
|
54
|
+
const rootKeyHex = config.rootKeyHex ?? PrivateKey.fromRandom().toHex();
|
|
55
|
+
const rootKey = PrivateKey.fromHex(rootKeyHex);
|
|
56
|
+
const identityKey = rootKey.toPublicKey().toString();
|
|
57
|
+
// Ensure the storage directory exists
|
|
58
|
+
fs.mkdirSync(config.storageDir, { recursive: true });
|
|
59
|
+
// Persist identity for later loading
|
|
60
|
+
const identity = {
|
|
61
|
+
rootKeyHex,
|
|
62
|
+
identityKey,
|
|
63
|
+
network: config.network,
|
|
64
|
+
};
|
|
65
|
+
const identityPath = path.join(config.storageDir, IDENTITY_FILE);
|
|
66
|
+
fs.writeFileSync(identityPath, JSON.stringify(identity, null, 2), 'utf-8');
|
|
67
|
+
// Build the wallet
|
|
68
|
+
const setup = await BSVAgentWallet.buildSetup(config, rootKeyHex);
|
|
69
|
+
return new BSVAgentWallet(setup);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Load an existing agent wallet from its storage directory.
|
|
73
|
+
* Reads the persisted identity file and re-initializes the wallet.
|
|
74
|
+
*/
|
|
75
|
+
static async load(config) {
|
|
76
|
+
const identityPath = path.join(config.storageDir, IDENTITY_FILE);
|
|
77
|
+
if (!fs.existsSync(identityPath)) {
|
|
78
|
+
return this.create(config);
|
|
79
|
+
}
|
|
80
|
+
const identity = JSON.parse(fs.readFileSync(identityPath, 'utf-8'));
|
|
81
|
+
const rootKeyHex = config.rootKeyHex ?? identity.rootKeyHex;
|
|
82
|
+
const setup = await BSVAgentWallet.buildSetup(config, rootKeyHex);
|
|
83
|
+
return new BSVAgentWallet(setup);
|
|
84
|
+
}
|
|
85
|
+
// ---------------------------------------------------------------------------
|
|
86
|
+
// Wallet lifecycle
|
|
87
|
+
// ---------------------------------------------------------------------------
|
|
88
|
+
/**
|
|
89
|
+
* Get this wallet's public identity key (compressed hex, 33 bytes).
|
|
90
|
+
* This is the key other agents use to send payments to you.
|
|
91
|
+
*/
|
|
92
|
+
async getIdentityKey() {
|
|
93
|
+
return this._setup.identityKey;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Get the wallet's current balance in satoshis.
|
|
97
|
+
*
|
|
98
|
+
* Uses the BRC-100 wallet's balance method which sums spendable outputs
|
|
99
|
+
* in the default basket.
|
|
100
|
+
*/
|
|
101
|
+
async getBalance() {
|
|
102
|
+
return await this._setup.wallet.balance();
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Cleanly shut down the wallet, releasing database connections and
|
|
106
|
+
* stopping the background monitor.
|
|
107
|
+
*/
|
|
108
|
+
async destroy() {
|
|
109
|
+
await this._setup.wallet.destroy();
|
|
110
|
+
}
|
|
111
|
+
// ---------------------------------------------------------------------------
|
|
112
|
+
// Payment creation (sender/payer side)
|
|
113
|
+
// ---------------------------------------------------------------------------
|
|
114
|
+
/**
|
|
115
|
+
* Build a BRC-29 payment to another agent.
|
|
116
|
+
*
|
|
117
|
+
* The transaction is created with `noSend: true` — the sender does NOT
|
|
118
|
+
* broadcast it. Instead, the Atomic BEEF and derivation metadata are
|
|
119
|
+
* returned so they can be transmitted to the recipient, who will
|
|
120
|
+
* verify and internalize (broadcast) the payment.
|
|
121
|
+
*
|
|
122
|
+
* @param params.to — Recipient's compressed public key (hex).
|
|
123
|
+
* @param params.satoshis — Amount in satoshis.
|
|
124
|
+
* @param params.description — Optional human-readable note.
|
|
125
|
+
*/
|
|
126
|
+
async createPayment(params) {
|
|
127
|
+
return buildPayment(this._setup, params);
|
|
128
|
+
}
|
|
129
|
+
// ---------------------------------------------------------------------------
|
|
130
|
+
// Payment verification & acceptance (receiver/merchant side)
|
|
131
|
+
// ---------------------------------------------------------------------------
|
|
132
|
+
/**
|
|
133
|
+
* Verify an incoming Atomic BEEF payment.
|
|
134
|
+
*
|
|
135
|
+
* This performs structural validation and SPV verification via tx.verify().
|
|
136
|
+
*/
|
|
137
|
+
async verifyPayment(params) {
|
|
138
|
+
return await verifyPayment(params);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Accept (internalize) a verified payment into this wallet.
|
|
142
|
+
*
|
|
143
|
+
* Uses the BRC-29 wallet payment protocol to derive the correct key
|
|
144
|
+
* and claim the output. This triggers SPV verification and, if the
|
|
145
|
+
* transaction hasn't been broadcast yet, broadcasts it.
|
|
146
|
+
*/
|
|
147
|
+
async acceptPayment(params) {
|
|
148
|
+
return acceptPayment(this._setup, params);
|
|
149
|
+
}
|
|
150
|
+
// ---------------------------------------------------------------------------
|
|
151
|
+
// Access to underlying toolbox objects (for advanced use)
|
|
152
|
+
// ---------------------------------------------------------------------------
|
|
153
|
+
/** Get the underlying wallet-toolbox SetupWallet for advanced operations. */
|
|
154
|
+
getSetup() {
|
|
155
|
+
return this._setup;
|
|
156
|
+
}
|
|
157
|
+
// ---------------------------------------------------------------------------
|
|
158
|
+
// Private helpers
|
|
159
|
+
// ---------------------------------------------------------------------------
|
|
160
|
+
/**
|
|
161
|
+
* Internal: manually construct a BRC-100 wallet backed by SQLite.
|
|
162
|
+
*
|
|
163
|
+
* We build this by hand instead of using Setup.createWalletSQLite because
|
|
164
|
+
* the toolbox has a bug where its internal randomBytesHex is a stub.
|
|
165
|
+
* We use the same components but wire them up correctly.
|
|
166
|
+
*/
|
|
167
|
+
static async buildSetup(config, rootKeyHex) {
|
|
168
|
+
const chain = toChain(config.network);
|
|
169
|
+
const taalApiKey = config.taalApiKey ?? DEFAULT_TAAL_API_KEYS[chain];
|
|
170
|
+
const rootKey = PrivateKey.fromHex(rootKeyHex);
|
|
171
|
+
const identityKey = rootKey.toPublicKey().toString();
|
|
172
|
+
// 1. Key derivation
|
|
173
|
+
const keyDeriver = new CachedKeyDeriver(rootKey);
|
|
174
|
+
// 2. Storage manager (empty initially)
|
|
175
|
+
const storage = new WalletStorageManager(identityKey);
|
|
176
|
+
// 3. Network services (ARC broadcasting, chain tracking, etc.)
|
|
177
|
+
const serviceOptions = Services.createDefaultOptions(chain);
|
|
178
|
+
const chaintracksUrl = process.env.BSV_CHAINTRACKS_URL || 'https://chaintracks-us-1.bsvb.tech';
|
|
179
|
+
const arcUrl = process.env.BSV_ARC_URL;
|
|
180
|
+
serviceOptions.chaintracks = new ChaintracksServiceClient(chain, chaintracksUrl);
|
|
181
|
+
if (arcUrl) {
|
|
182
|
+
serviceOptions.arcUrl = arcUrl;
|
|
183
|
+
}
|
|
184
|
+
serviceOptions.taalApiKey = taalApiKey;
|
|
185
|
+
const services = new Services(serviceOptions);
|
|
186
|
+
// 4. Background monitor
|
|
187
|
+
const monopts = Monitor.createDefaultWalletMonitorOptions(chain, storage, services);
|
|
188
|
+
const monitor = new Monitor(monopts);
|
|
189
|
+
monitor.addDefaultTasks();
|
|
190
|
+
// 5. The BRC-100 Wallet
|
|
191
|
+
const wallet = new Wallet({ chain, keyDeriver, storage, services, monitor });
|
|
192
|
+
// 6. SQLite storage via knex
|
|
193
|
+
const filePath = path.join(config.storageDir, `${DEFAULT_DB_NAME}.sqlite`);
|
|
194
|
+
const knex = knexLib({
|
|
195
|
+
client: 'sqlite3',
|
|
196
|
+
connection: { filename: filePath },
|
|
197
|
+
useNullAsDefault: true,
|
|
198
|
+
});
|
|
199
|
+
// Fee model: configurable via BSV_FEE_MODEL env var (default: 100 sat/KB)
|
|
200
|
+
const feeModelValue = config.feeModel ??
|
|
201
|
+
(process.env.BSV_FEE_MODEL ? parseInt(process.env.BSV_FEE_MODEL, 10) : 100);
|
|
202
|
+
const activeStorage = new StorageKnex({
|
|
203
|
+
chain,
|
|
204
|
+
knex,
|
|
205
|
+
commissionSatoshis: 0,
|
|
206
|
+
commissionPubKeyHex: undefined,
|
|
207
|
+
feeModel: { model: 'sat/kb', value: feeModelValue },
|
|
208
|
+
});
|
|
209
|
+
await activeStorage.migrate(DEFAULT_DB_NAME, randomBytesHex(33));
|
|
210
|
+
await activeStorage.makeAvailable();
|
|
211
|
+
await storage.addWalletStorageProvider(activeStorage);
|
|
212
|
+
await activeStorage.findOrInsertUser(identityKey);
|
|
213
|
+
return {
|
|
214
|
+
rootKey,
|
|
215
|
+
identityKey,
|
|
216
|
+
keyDeriver,
|
|
217
|
+
chain,
|
|
218
|
+
storage,
|
|
219
|
+
services,
|
|
220
|
+
monitor,
|
|
221
|
+
wallet,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
//# sourceMappingURL=wallet.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet.js","sourceRoot":"","sources":["../../../src/core/wallet.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AACxD,OAAO,EACL,MAAM,EACN,oBAAoB,EACpB,QAAQ,EACR,OAAO,EACP,WAAW,EACX,cAAc,EACd,wBAAwB,GACzB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,OAAO,MAAM,MAAM,CAAC;AAC3B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAY9B,OAAO,EAAE,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9E,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MA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AAC,eAAe,EAAE,cAAc,CAAC,EAAE,CAAC,CAAC,CAAC;QACjE,MAAM,aAAa,CAAC,aAAa,EAAE,CAAC;QACpC,MAAM,OAAO,CAAC,wBAAwB,CAAC,aAAa,CAAC,CAAC;QACtD,MAAM,aAAa,CAAC,gBAAgB,CAAC,WAAW,CAAC,CAAC;QAElD,OAAO;YACL,OAAO;YACP,WAAW;YACX,UAAU;YACV,KAAK;YACL,OAAO;YACP,QAAQ;YACR,OAAO;YACP,MAAM;SACP,CAAC;IACJ,CAAC;CACF"}
|
package/index.ts
CHANGED
|
@@ -1,22 +1,51 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
2
|
import os from "node:os";
|
|
3
3
|
import fs from "node:fs";
|
|
4
|
+
import { MessageBoxManager } from "./src/client.js";
|
|
5
|
+
import { MessageStore } from "./src/store.js";
|
|
6
|
+
import { BSVAgentWallet } from "./src/core/wallet.js";
|
|
4
7
|
|
|
5
8
|
/**
|
|
6
9
|
* OpenClaw MessageBox Plugin
|
|
7
10
|
* Enables secure P2P encrypted messaging and payments.
|
|
8
11
|
*/
|
|
9
12
|
export default function register(api: any) {
|
|
10
|
-
// Capture configuration from the gateway (handle
|
|
11
|
-
const
|
|
13
|
+
// Capture configuration from the gateway (handle multiple IDs and flat/nested structures)
|
|
14
|
+
const entries = api.getConfig?.()?.plugins?.entries || {};
|
|
15
|
+
const entry = entries['openclaw-messagebox']
|
|
16
|
+
|| entries['openclaw-messagebox-plugin']
|
|
17
|
+
|| entries['bsv-messagebox']
|
|
18
|
+
|| {};
|
|
19
|
+
|
|
12
20
|
const pluginConfig = { ...entry, ...(entry.config || {}), ...(api.config || {}) };
|
|
13
21
|
const host = pluginConfig.host || 'https://msg.bsv.direct';
|
|
14
22
|
const network = pluginConfig.network || 'mainnet';
|
|
15
23
|
const chaintracksUrl = pluginConfig.chaintracksUrl || 'https://chaintracks-us-1.bsvb.tech';
|
|
16
|
-
const arcUrl = pluginConfig.arcUrl || '';
|
|
24
|
+
const arcUrl = pluginConfig.arcUrl || (network === 'testnet' ? 'https://testnet.arc.gorillapool.io' : 'https://arc.gorillapool.io');
|
|
17
25
|
const walletDir = pluginConfig.walletDir || path.join(os.homedir(), '.openclaw', 'bsv-wallet');
|
|
26
|
+
const dbPath = pluginConfig.dbPath || path.join(os.homedir(), '.openclaw', 'openclaw-messagebox', 'messages.sqlite');
|
|
18
27
|
|
|
19
|
-
|
|
28
|
+
let manager: MessageBoxManager | null = null;
|
|
29
|
+
let store: MessageStore | null = null;
|
|
30
|
+
let wallet: BSVAgentWallet | null = null;
|
|
31
|
+
|
|
32
|
+
api.logger.info(`[messagebox] Initializing MessageBox Plugin (network: ${network}, host: ${host})`);
|
|
33
|
+
|
|
34
|
+
async function ensureInitialized() {
|
|
35
|
+
if (manager) return manager;
|
|
36
|
+
|
|
37
|
+
// Initialize shared wallet
|
|
38
|
+
wallet = await BSVAgentWallet.load({
|
|
39
|
+
network: network as any,
|
|
40
|
+
storageDir: walletDir
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
store = new MessageStore(dbPath);
|
|
44
|
+
await store.init();
|
|
45
|
+
|
|
46
|
+
manager = new MessageBoxManager({ host, walletDir, dbPath }, store, wallet);
|
|
47
|
+
return manager;
|
|
48
|
+
}
|
|
20
49
|
|
|
21
50
|
// Register the messagebox tool
|
|
22
51
|
api.registerTool({
|
|
@@ -38,6 +67,11 @@ export default function register(api: any) {
|
|
|
38
67
|
type: "string",
|
|
39
68
|
description: "Message content (will be encrypted client-side)"
|
|
40
69
|
},
|
|
70
|
+
box: {
|
|
71
|
+
type: "string",
|
|
72
|
+
default: "default",
|
|
73
|
+
description: "Target mailbox/folder"
|
|
74
|
+
},
|
|
41
75
|
sats: {
|
|
42
76
|
type: "number",
|
|
43
77
|
description: "Amount in satoshis for direct P2P payment"
|
|
@@ -52,13 +86,44 @@ export default function register(api: any) {
|
|
|
52
86
|
},
|
|
53
87
|
async execute(_id: string, params: any) {
|
|
54
88
|
try {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
89
|
+
const m = await ensureInitialized();
|
|
90
|
+
|
|
91
|
+
switch (params.action) {
|
|
92
|
+
case "send":
|
|
93
|
+
if (!params.recipientKey || !params.body) throw new Error("recipientKey and body required");
|
|
94
|
+
const msgId = await m.send(params.recipientKey, params.body, params.box);
|
|
95
|
+
return { content: [{ type: "text", text: `✅ Message sent successfully. ID: ${msgId}` }] };
|
|
96
|
+
|
|
97
|
+
case "inbox":
|
|
98
|
+
await m.poll(params.box);
|
|
99
|
+
const messages = await store!.getInbox();
|
|
100
|
+
if (messages.length === 0) return { content: [{ type: "text", text: "Your inbox is empty." }] };
|
|
101
|
+
|
|
102
|
+
const list = messages.map(msg => `[${new Date(msg.ts).toLocaleString()}] FROM: ${msg.senderKey.slice(0, 12)}... - "${msg.body}"`).join('\n');
|
|
103
|
+
return { content: [{ type: "text", text: `📬 Inbox (Last ${messages.length} messages):\n\n${list}` }] };
|
|
104
|
+
|
|
105
|
+
case "acknowledge":
|
|
106
|
+
if (!params.messageIds || params.messageIds.length === 0) throw new Error("messageIds array required");
|
|
107
|
+
await m.acknowledge(params.messageIds);
|
|
108
|
+
return { content: [{ type: "text", text: `✅ Acknowledged ${params.messageIds.length} messages.` }] };
|
|
109
|
+
|
|
110
|
+
case "status":
|
|
111
|
+
return { content: [{ type: "text", text: `MessageBox Status:\nHost: ${host}\nIdentity: ${m.getIdentityKey()}\nDatabase: ${dbPath}` }] };
|
|
112
|
+
|
|
113
|
+
case "pay":
|
|
114
|
+
if (!params.recipientKey || !params.sats) throw new Error("recipientKey and sats required");
|
|
115
|
+
const payResult = await wallet!.createPayment({
|
|
116
|
+
to: params.recipientKey,
|
|
117
|
+
satoshis: params.sats,
|
|
118
|
+
description: "P2P Payment via MessageBox"
|
|
119
|
+
});
|
|
120
|
+
// Send the BEEF payment as a message to the recipient's payment_inbox
|
|
121
|
+
const pmsgId = await m.send(params.recipientKey, JSON.stringify(payResult), 'payment_inbox');
|
|
122
|
+
return { content: [{ type: "text", text: `✅ P2P Payment sent! Amount: ${params.sats} sats. Notification ID: ${pmsgId}` }] };
|
|
123
|
+
|
|
124
|
+
default:
|
|
125
|
+
throw new Error(`Unknown action: ${params.action}`);
|
|
126
|
+
}
|
|
62
127
|
} catch (error: any) {
|
|
63
128
|
return {
|
|
64
129
|
content: [{
|
|
@@ -74,11 +139,27 @@ export default function register(api: any) {
|
|
|
74
139
|
api.registerService({
|
|
75
140
|
id: "messagebox-listener",
|
|
76
141
|
start: async () => {
|
|
77
|
-
|
|
78
|
-
|
|
142
|
+
try {
|
|
143
|
+
const m = await ensureInitialized();
|
|
144
|
+
api.logger.info("[messagebox] Starting WebSocket listener...");
|
|
145
|
+
|
|
146
|
+
await m.listen((msg) => {
|
|
147
|
+
api.logger.info(`[messagebox] ⚡ New message from ${msg.senderKey.slice(0, 12)}...`);
|
|
148
|
+
|
|
149
|
+
// Wake the agent to handle the incoming private message
|
|
150
|
+
const wakeText = `⚡ New private message received!\n\nFrom: ${msg.senderKey}\nMessage: ${msg.body}\n\nYou can reply using messagebox({ action: "send", recipientKey: "${msg.senderKey}", body: "..." })`;
|
|
151
|
+
|
|
152
|
+
api.wakeAgent?.({
|
|
153
|
+
prompt: wakeText,
|
|
154
|
+
sessionKey: `hook:messagebox:${msg.id}`
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
} catch (err: any) {
|
|
158
|
+
api.logger.error(`[messagebox] WebSocket listener failed: ${err.message}`);
|
|
159
|
+
}
|
|
79
160
|
},
|
|
80
161
|
stop: async () => {
|
|
81
|
-
api.logger.info("[messagebox] Stopping
|
|
162
|
+
api.logger.info("[messagebox] Stopping WebSocket listener...");
|
|
82
163
|
}
|
|
83
164
|
});
|
|
84
165
|
}
|