Wallet subaddress creation with retry recovery
This commit is contained in:
@@ -2,7 +2,7 @@ import { randomUUID } from 'node:crypto';
|
||||
import { lstat, readFile } from 'node:fs/promises';
|
||||
import { z } from 'zod';
|
||||
import { parseAtomicXmr, XMR_RPC_UINT64_MAX } from '../../shared/xmr-amount.ts';
|
||||
import { validateXmrAddress } from './xmr.ts';
|
||||
import { validateXmrAddress, validateXmrSubaddress } from './xmr.ts';
|
||||
|
||||
const MAX_CONFIG_BYTES = 4096;
|
||||
const MAX_RESPONSE_BYTES = 1024 * 1024;
|
||||
@@ -20,6 +20,13 @@ const rpcEnvelopeSchema = z.object({
|
||||
const integerString = z.string().regex(/^(0|[1-9]\d{0,19})$/);
|
||||
const heightResultSchema = z.object({ height: integerString, status: z.literal('OK') }).passthrough();
|
||||
const addressResultSchema = z.object({ address: z.string().length(95), addresses: z.array(z.unknown()).optional(), status: z.literal('OK') }).passthrough();
|
||||
const subaddressEntrySchema = z.object({ address: z.string().length(95), label: z.string().max(128),
|
||||
address_index: integerString, used: z.boolean() }).passthrough();
|
||||
const subaddressReadResultSchema = z.object({ address: z.string().length(95),
|
||||
addresses: z.array(subaddressEntrySchema).max(1), status: z.literal('OK') }).passthrough();
|
||||
const subaddressCreateResultSchema = z.object({ address: z.string().length(95), address_index: integerString,
|
||||
address_indices: z.array(integerString).max(1).optional(), addresses: z.array(z.string().length(95)).max(1).optional(),
|
||||
status: z.literal('OK') }).passthrough();
|
||||
const incomingTransferSchema = z.object({
|
||||
txid: z.string().regex(/^[a-f0-9]{64}$/),
|
||||
address: z.string().length(95),
|
||||
@@ -123,10 +130,12 @@ export async function loadMoneroObserverConfig(env: NodeJS.ProcessEnv = process.
|
||||
}
|
||||
|
||||
/** Restricted read-only surface. No generic RPC method, subaddress creation, transfer or sweep API exists. */
|
||||
export function createMoneroWalletObserver(config: z.infer<typeof configSchema>, transport: MoneroFixtureTransport = fetch) {
|
||||
function createWalletRpc(config: z.infer<typeof configSchema>, transport: MoneroFixtureTransport,
|
||||
allowedMethods: readonly ('get_height' | 'get_address' | 'get_transfers' | 'create_address')[]) {
|
||||
const endpoint = validateEndpoint(config.url);
|
||||
const authorization = `Basic ${Buffer.from(`${config.username}:${config.password}`).toString('base64')}`;
|
||||
async function rpc(method: 'get_height' | 'get_address' | 'get_transfers', params: Record<string, unknown> = {}) {
|
||||
async function rpc(method: 'get_height' | 'get_address' | 'get_transfers' | 'create_address', params: Record<string, unknown> = {}) {
|
||||
if (!allowedMethods.includes(method)) throw new Error('Monero wallet-RPC method is not allowed for this client');
|
||||
const id = randomUUID(), controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), RPC_TIMEOUT_MS); timeout.unref();
|
||||
const abort = new Promise<never>((_, reject) => controller.signal.addEventListener('abort',
|
||||
@@ -165,6 +174,12 @@ export function createMoneroWalletObserver(config: z.infer<typeof configSchema>,
|
||||
try { return await Promise.race([operation, abort]); }
|
||||
finally { clearTimeout(timeout); }
|
||||
}
|
||||
return rpc;
|
||||
}
|
||||
|
||||
/** Read-only observer capability. It deliberately cannot create addresses or move funds. */
|
||||
export function createMoneroWalletObserver(config: z.infer<typeof configSchema>, transport: MoneroFixtureTransport = fetch) {
|
||||
const rpc = createWalletRpc(config, transport, ['get_height', 'get_address', 'get_transfers']);
|
||||
return {
|
||||
async verifyMainnetWallet(): Promise<string> {
|
||||
const result = addressResultSchema.parse(await rpc('get_address', { account_index: 0 }));
|
||||
@@ -202,3 +217,49 @@ export function createMoneroWalletObserver(config: z.infer<typeof configSchema>,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export type MoneroDepositAddressRpc = {
|
||||
getSubaddress(accountIndex: number, addressIndex: number): Promise<{ address: string; label: string } | null>;
|
||||
createSubaddress(accountIndex: number, label: string): Promise<{ address: string; addressIndex: number }>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Narrow address-allocation capability; it can only inspect or create one subaddress at a
|
||||
* time. Keep this credential/service separate from API and withdrawal capabilities. This
|
||||
* client is intentionally not attached to startup, routes, UI, or automatic crediting.
|
||||
*/
|
||||
export function createMoneroDepositAddressRpc(config: z.infer<typeof configSchema>, transport: MoneroFixtureTransport = fetch) {
|
||||
const rpc = createWalletRpc(config, transport, ['get_address', 'create_address']);
|
||||
return {
|
||||
async getSubaddress(accountIndex: number, addressIndex: number): Promise<{ address: string; label: string } | null> {
|
||||
if (![accountIndex, addressIndex].every(value => Number.isSafeInteger(value) && value >= 0 && value <= 0xffff_ffff)) {
|
||||
throw new Error('Invalid Monero subaddress index');
|
||||
}
|
||||
const raw = await rpc('get_address', { account_index: accountIndex, address_index: [addressIndex] });
|
||||
const result = subaddressReadResultSchema.parse(raw);
|
||||
const entry = result.addresses[0];
|
||||
if (!entry) return null;
|
||||
if (integer(entry.address_index, 0xffff_ffffn, 'address index') !== addressIndex) {
|
||||
throw new Error('Monero wallet returned a different subaddress index');
|
||||
}
|
||||
await validateXmrAddress(entry.address);
|
||||
return { address: entry.address, label: entry.label };
|
||||
},
|
||||
async createSubaddress(accountIndex: number, label: string): Promise<{ address: string; addressIndex: number }> {
|
||||
if (!Number.isSafeInteger(accountIndex) || accountIndex < 0 || accountIndex > 0xffff_ffff ||
|
||||
typeof label !== 'string' || label.length < 1 || label.length > 128 || /[\r\n\x00]/.test(label)) {
|
||||
throw new Error('Invalid Monero subaddress creation parameters');
|
||||
}
|
||||
const result = subaddressCreateResultSchema.parse(await rpc('create_address', {
|
||||
account_index: accountIndex, label, count: 1,
|
||||
}));
|
||||
const addressIndex = integer(result.address_index, 0xffff_ffffn, 'address index');
|
||||
if ((result.address_indices && (result.address_indices.length !== 1 || integer(result.address_indices[0], 0xffff_ffffn, 'address index') !== addressIndex)) ||
|
||||
(result.addresses && (result.addresses.length !== 1 || result.addresses[0] !== result.address))) {
|
||||
throw new Error('Monero wallet returned inconsistent subaddress creation data');
|
||||
}
|
||||
await validateXmrSubaddress(result.address);
|
||||
return { address: result.address, addressIndex };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user