openclaw-storage-plugin 0.1.2 → 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/README.md +38 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +36 -13
- package/dist/index.js.map +1 -1
- package/dist/src/client.d.ts +8 -0
- package/dist/src/client.d.ts.map +1 -0
- package/dist/src/client.js +45 -0
- package/dist/src/client.js.map +1 -0
- 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/dist/src/types.d.ts +11 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +2 -0
- package/dist/src/types.js.map +1 -0
- package/index.ts +41 -13
- package/openclaw.plugin.json +1 -1
- package/package.json +1 -1
- package/src/client.ts +56 -0
- 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
- package/src/types.ts +11 -0
|
@@ -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,qBAAq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M,CAAC,WAAW,IAAI,kBAAkB,CAAC,CAAC;IAC5E,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,IAAI,CAAC,CAAC;IAE9B,MAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,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,C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J,kBAAkB,EAAE,CAAC;YACrB,mBAAmB,EAAE,SAAS;YAC9B,QAAQ,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,aAAa,EAAE;SACpD,CAAC,CAAC;QAEH,MAAM,aAAa,CAAC,OAAO,CAAC,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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}
|
package/index.ts
CHANGED
|
@@ -1,23 +1,35 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
2
|
import os from "node:os";
|
|
3
|
+
import { StorageManager } from "./src/client.js";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* OpenClaw Storage Plugin
|
|
6
7
|
* Enables UHRP hash-based storage for agent data.
|
|
7
8
|
*/
|
|
8
9
|
export default function register(api: any) {
|
|
9
|
-
const
|
|
10
|
+
const entries = api.getConfig?.()?.plugins?.entries || {};
|
|
11
|
+
const entry = entries['openclaw-storage']
|
|
12
|
+
|| entries['openclaw-storage-plugin']
|
|
13
|
+
|| {};
|
|
14
|
+
|
|
10
15
|
const pluginConfig = { ...entry, ...(entry.config || {}), ...(api.config || {}) };
|
|
11
16
|
const storageHost = pluginConfig.storageHost || 'https://storage.bsv.direct';
|
|
12
|
-
const network = pluginConfig.network || 'mainnet';
|
|
13
17
|
const walletDir = pluginConfig.walletDir || path.join(os.homedir(), '.openclaw', 'bsv-wallet');
|
|
14
18
|
|
|
15
|
-
|
|
19
|
+
let manager: StorageManager | null = null;
|
|
20
|
+
|
|
21
|
+
api.logger.info(`[storage] Initializing Storage Plugin (host: ${storageHost})`);
|
|
22
|
+
|
|
23
|
+
function ensureInitialized() {
|
|
24
|
+
if (manager) return manager;
|
|
25
|
+
manager = new StorageManager({ storageHost, walletDir });
|
|
26
|
+
return manager;
|
|
27
|
+
}
|
|
16
28
|
|
|
17
29
|
// Register the storage tool
|
|
18
30
|
api.registerTool({
|
|
19
31
|
name: "storage",
|
|
20
|
-
description: "Hash-based permanent storage for large assets",
|
|
32
|
+
description: "Hash-based permanent storage for large assets via UHRP",
|
|
21
33
|
parameters: {
|
|
22
34
|
type: "object",
|
|
23
35
|
properties: {
|
|
@@ -28,24 +40,40 @@ export default function register(api: any) {
|
|
|
28
40
|
},
|
|
29
41
|
data: {
|
|
30
42
|
type: "string",
|
|
31
|
-
description: "Content to upload"
|
|
43
|
+
description: "Content to upload (UTF-8 string)"
|
|
32
44
|
},
|
|
33
45
|
hash: {
|
|
34
46
|
type: "string",
|
|
35
|
-
description: "UHRP hash to download"
|
|
47
|
+
description: "SHA256 UHRP hash to download"
|
|
36
48
|
}
|
|
37
49
|
},
|
|
38
50
|
required: ["action"]
|
|
39
51
|
},
|
|
40
52
|
async execute(_id: string, params: any) {
|
|
41
53
|
try {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
54
|
+
const m = ensureInitialized();
|
|
55
|
+
|
|
56
|
+
switch (params.action) {
|
|
57
|
+
case "upload":
|
|
58
|
+
if (!params.data) throw new Error("data is required for upload");
|
|
59
|
+
const hash = await m.upload(params.data);
|
|
60
|
+
return { content: [{ type: "text", text: `✅ Uploaded successfully!\nUHRP Hash: ${hash}\nURL: ${storageHost}/cdn/${hash}` }] };
|
|
61
|
+
|
|
62
|
+
case "download":
|
|
63
|
+
if (!params.hash) throw new Error("hash is required for download");
|
|
64
|
+
const buffer = await m.download(params.hash);
|
|
65
|
+
const content = buffer.toString('utf-8');
|
|
66
|
+
return { content: [{ type: "text", text: `📥 Downloaded Content (Verified):\n\n${content}` }] };
|
|
67
|
+
|
|
68
|
+
case "status":
|
|
69
|
+
return { content: [{ type: "text", text: `Storage Status:\nHost: ${storageHost}\nMode: UHRP Content-Addressable` }] };
|
|
70
|
+
|
|
71
|
+
case "list":
|
|
72
|
+
return { content: [{ type: "text", text: "Local file index integration pending logic development." }] };
|
|
73
|
+
|
|
74
|
+
default:
|
|
75
|
+
throw new Error(`Unknown action: ${params.action}`);
|
|
76
|
+
}
|
|
49
77
|
} catch (error: any) {
|
|
50
78
|
return {
|
|
51
79
|
content: [{
|
package/openclaw.plugin.json
CHANGED
package/package.json
CHANGED
package/src/client.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Utils, Hash } from '@bsv/sdk';
|
|
2
|
+
import { StorageConfig, FileRecord } from './types.js';
|
|
3
|
+
|
|
4
|
+
export class StorageManager {
|
|
5
|
+
private config: StorageConfig;
|
|
6
|
+
|
|
7
|
+
constructor(config: StorageConfig) {
|
|
8
|
+
this.config = config;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async upload(data: Buffer | string): Promise<string> {
|
|
12
|
+
const buffer = typeof data === 'string' ? Buffer.from(data) : data;
|
|
13
|
+
const hash = Utils.toHex(Hash.sha256(buffer));
|
|
14
|
+
const host = this.config.storageHost.replace(/\/$/, '');
|
|
15
|
+
|
|
16
|
+
// 1. Check if it already exists
|
|
17
|
+
const headResp = await fetch(`${host}/cdn/${hash}`, { method: 'HEAD' });
|
|
18
|
+
if (headResp.ok) return hash;
|
|
19
|
+
|
|
20
|
+
// 2. Upload
|
|
21
|
+
const uploadResp = await fetch(`${host}/upload`, {
|
|
22
|
+
method: 'POST',
|
|
23
|
+
headers: {
|
|
24
|
+
'Content-Type': 'application/octet-stream',
|
|
25
|
+
'X-UHRP-Hash': hash
|
|
26
|
+
},
|
|
27
|
+
body: buffer
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
if (!uploadResp.ok) {
|
|
31
|
+
const err = await uploadResp.text();
|
|
32
|
+
throw new Error(`Upload failed: ${uploadResp.status} ${err}`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return hash;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async download(hash: string): Promise<Buffer> {
|
|
39
|
+
const host = this.config.storageHost.replace(/\/$/, '');
|
|
40
|
+
const resp = await fetch(`${host}/cdn/${hash}`);
|
|
41
|
+
|
|
42
|
+
if (!resp.ok) {
|
|
43
|
+
throw new Error(`Download failed: ${resp.status}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const buffer = Buffer.from(await resp.arrayBuffer());
|
|
47
|
+
|
|
48
|
+
// Integrity verification
|
|
49
|
+
const downloadedHash = Utils.toHex(Hash.sha256(buffer));
|
|
50
|
+
if (downloadedHash !== hash) {
|
|
51
|
+
throw new Error(`Integrity check failed: Expected ${hash}, got ${downloadedHash}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return buffer;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @a2a-bsv/core — Configuration defaults and helpers.
|
|
3
|
+
*/
|
|
4
|
+
import type { WalletConfig } from './types.js';
|
|
5
|
+
/** Map our 'mainnet'/'testnet' to the wallet-toolbox's 'main'/'test' chain type. */
|
|
6
|
+
export type Chain = 'main' | 'test';
|
|
7
|
+
export declare function toChain(network: WalletConfig['network']): Chain;
|
|
8
|
+
/** Default TAAL API keys from the wallet-toolbox examples. */
|
|
9
|
+
export declare const DEFAULT_TAAL_API_KEYS: Record<Chain, string>;
|
|
10
|
+
/** Default SQLite database name. */
|
|
11
|
+
export declare const DEFAULT_DB_NAME = "a2a_agent_wallet";
|
|
12
|
+
//# sourceMappingURL=config.d.ts.map
|