|
| 1 | +import { ByteArray, formatEther, parseEther, type Hex } from "viem"; |
| 2 | +import { |
| 3 | + composeContext, |
| 4 | + generateObjectDeprecated, |
| 5 | + HandlerCallback, |
| 6 | + ModelClass, |
| 7 | + type IAgentRuntime, |
| 8 | + type Memory, |
| 9 | + type State, |
| 10 | +} from "@elizaos/core"; |
| 11 | + |
| 12 | +import { networks, Psbt } from 'bitcoinjs-lib'; |
| 13 | +import { BIP32Factory } from 'bip32'; |
| 14 | +import {randomBytes} from 'crypto'; |
| 15 | +import * as ecc from 'tiny-secp256k1'; |
| 16 | +import { BtcWallet, privateKeyFromWIF } from "@okxweb3/coin-bitcoin"; |
| 17 | +import { base } from "@okxweb3/crypto-lib"; |
| 18 | +import { mintTemplate } from "../templates"; |
| 19 | +import {initBtcFunProvider} from "../providers/btcfun.ts"; |
| 20 | +export { mintTemplate }; |
| 21 | + |
| 22 | +export const btcfunMintAction = { |
| 23 | + name: "btcfun", |
| 24 | + description: "btcfun mint brc20", |
| 25 | + handler: async ( |
| 26 | + runtime: IAgentRuntime, |
| 27 | + _message: Memory, |
| 28 | + state: State, |
| 29 | + _options: any, |
| 30 | + callback?: HandlerCallback |
| 31 | + ) => { |
| 32 | + console.log("btcfun action handler called"); |
| 33 | + const btcfunProvider = initBtcFunProvider(runtime); |
| 34 | + |
| 35 | + const chainCode = randomBytes(32); |
| 36 | + const bip32Factory = BIP32Factory(ecc); |
| 37 | + const network = networks.bitcoin; |
| 38 | + const privateKeyWif = runtime.getSetting("BTC_PRIVATE_KEY_WIF") ?? process.env.BTC_PRIVATE_KEY_WIF; |
| 39 | + let address = runtime.getSetting("ADDRESS") ?? process.env.ADDRESS; |
| 40 | + //const psbtHex = '70736274ff0100dd0200000002079a01841a17ba439545a00c73031db6a2317be7b12b619a4a1239a67e18ffaa0000000000fdffffffa0dffa35eee28e5c1b0c2048f5725f82200c6726337c27160b324e77a9c6ad530100000000fdffffff032202000000000000225120a11f9fa43193da4b9b825cf92d13fde040fc7205076c5a6eebfdb4b6a67a583d805c00000000000022512097162ff9c360ce05c59079a6f34897564528eee056dfcf2549383a3016683b8a4f0200000000000022512097162ff9c360ce05c59079a6f34897564528eee056dfcf2549383a3016683b8a000000000001012b220200000000000022512097162ff9c360ce05c59079a6f34897564528eee056dfcf2549383a3016683b8a011720e2df0c6fced9b8530a46649bc7ec06abfa8636a51bcacde4150c52715b76e9df0001012bc56800000000000022512097162ff9c360ce05c59079a6f34897564528eee056dfcf2549383a3016683b8a011720e2df0c6fced9b8530a46649bc7ec06abfa8636a51bcacde4150c52715b76e9df00000000'; |
| 41 | + |
| 42 | + const privateKey = base.fromHex(privateKeyFromWIF(privateKeyWif, network)); |
| 43 | + const privateKeyHex = base.toHex(privateKey); |
| 44 | + console.log('Private key: ', privateKeyHex) |
| 45 | + const privateKeyBuffer = Buffer.from(privateKeyHex, 'hex'); |
| 46 | + const keyPair = bip32Factory.fromPrivateKey(privateKeyBuffer, chainCode, network); |
| 47 | + const publicKeyBuffer = Buffer.from(keyPair.publicKey); |
| 48 | + const publicKeyHex = publicKeyBuffer.toString('hex'); |
| 49 | + console.log('Public Key: ', publicKeyHex); |
| 50 | + |
| 51 | + // Compose mint context |
| 52 | + const mintContext = composeContext({ |
| 53 | + state, |
| 54 | + template: mintTemplate, |
| 55 | + }); |
| 56 | + const content = await generateObjectDeprecated({ |
| 57 | + runtime, |
| 58 | + context: mintContext, |
| 59 | + modelClass: ModelClass.LARGE, |
| 60 | + }); |
| 61 | + let tick = content.inputToken; |
| 62 | + console.log("begin to mint token", tick, content) |
| 63 | + //todo remove |
| 64 | + tick = "dongj" |
| 65 | + |
| 66 | + try { |
| 67 | + const {order_id, psbt_hex} = await btcfunProvider.createBrc20Order(publicKeyHex, address, publicKeyHex, address, 5, tick,"100",864000,"1000") |
| 68 | + console.log("11110",psbt_hex) |
| 69 | + const psbt = Psbt.fromHex(psbt_hex) |
| 70 | + let wallet = new BtcWallet() |
| 71 | + const toSignInputs = []; |
| 72 | + psbt.data.inputs.forEach((input, index)=>{ |
| 73 | + toSignInputs.push({ |
| 74 | + index: index, |
| 75 | + address: address, |
| 76 | + sighashTypes: [0], |
| 77 | + disableTweakSigner: false, |
| 78 | + }); |
| 79 | + }) |
| 80 | + |
| 81 | + let params = { |
| 82 | + type: 3, |
| 83 | + psbt: psbt_hex, |
| 84 | + autoFinalized: false, |
| 85 | + toSignInputs: toSignInputs, |
| 86 | + }; |
| 87 | + |
| 88 | + let signParams = { |
| 89 | + privateKey: privateKeyWif, |
| 90 | + data: params, |
| 91 | + }; |
| 92 | + console.log("signParams: ", signParams) |
| 93 | + //let signedPsbtHex = await wallet.signTransaction(signParams); |
| 94 | + |
| 95 | + //todo open |
| 96 | + //await btcfunProvider.broadcastOrder(orderID, signedPsbtHex) |
| 97 | + //console.log('signedPsbtHex: ', signedPsbtHex, 'orderID: ', order_id) |
| 98 | + if (callback) { |
| 99 | + callback({ |
| 100 | + text: `Successfully mint ${tick} tokens`, |
| 101 | + content: { |
| 102 | + success: true, |
| 103 | + orderID: order_id, |
| 104 | + //psbtHex: signedPsbtHex, |
| 105 | + }, |
| 106 | + }); |
| 107 | + } |
| 108 | + } catch (error) { |
| 109 | + console.error('Error:', error); |
| 110 | + } |
| 111 | + }, |
| 112 | + template: mintTemplate, |
| 113 | + validate: async (runtime: IAgentRuntime) => { |
| 114 | + const privateKey = runtime.getSetting("BTC_PRIVATE_KEY_WIF"); |
| 115 | + return typeof privateKey === "string" && privateKey.length > 0; |
| 116 | + }, |
| 117 | + examples: [ |
| 118 | + [ |
| 119 | + { |
| 120 | + user: "assistant", |
| 121 | + content: { |
| 122 | + text: "I'll help you mint 100000000 Party", |
| 123 | + action: "MINT_BRC20", |
| 124 | + }, |
| 125 | + }, |
| 126 | + { |
| 127 | + user: "user", |
| 128 | + content: { |
| 129 | + text: "import token BRC20 `Party`", |
| 130 | + action: "MINT_BRC20", |
| 131 | + }, |
| 132 | + }, |
| 133 | + ], |
| 134 | + ], |
| 135 | + similes: ["MINT_BRC20","MINT_RUNES"], |
| 136 | +}; |
0 commit comments