import ky from "ky"; const rpcUrl = "http://152.53.124.126:9332/"; const rpcUser = "notiiwasntherexdddd"; const rpcPassword = "NYwsxePgMrThiapHnfCzUfaEfVlNKZECwvlqhHcWjerlZfcaTp"; const headers = { "Content-Type": "application/json", Authorization: `Basic ${Buffer.from(`${rpcUser}:${rpcPassword}`).toString( "base64" )}`, }; async function callRpc(method, params = [], walletName = null) { console.log(`Calling RPC method: ${method} with params: ${JSON.stringify(params)}`); const url = walletName ? `${rpcUrl}wallet/${walletName}` : rpcUrl; try { const response = await ky .post(url, { json: { jsonrpc: "1.0", id: "curltest", method, params, }, headers, }) .json(); console.log( `RPC Response for method ${method}:`, JSON.stringify(response, null, 2) ); if (response.error) { throw new Error(`RPC Error: ${JSON.stringify(response.error, null, 2)}`); } return response.result; } catch (error) { console.error(`Error calling RPC method ${method}:`, error); throw error; } } async function createWallet(walletName) { const result = await callRpc("createwallet", [walletName]); console.log("Wallet created:", result); return result; } async function generateAddress(walletName = null) { const address = await callRpc("getnewaddress", [], walletName); console.log("Generated Address:", address); return address; } async function checkWalletLoaded(walletName) { const walletInfo = await callRpc("getwalletinfo", [], walletName); console.log("Wallet Info:", walletInfo); return walletInfo; } async function walletExists(walletName) { try { await callRpc("getwalletinfo", [], walletName); return true; } catch (error) { return false; } } async function dumpPrivateKey(address, walletName) { const privateKey = await callRpc("dumpprivkey", [address], walletName); console.log("Private Key:", privateKey); return privateKey; } async function setupWallet(walletName) { const createResult = await createWallet(walletName); const address = await generateAddress(walletName); console.log("Address:", address); console.log("Wallet Name:", walletName); console.log("Create Result:", createResult); const privKey = await dumpPrivateKey(address, walletName); console.log("Wallet Info:", privKey); return { address, privKey }; } export { setupWallet };