Skip to content

Commit

Permalink
🎨 Improved folder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisarevalo11 committed May 27, 2024
1 parent 7cea47e commit 903d313
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 48 deletions.
1 change: 1 addition & 0 deletions constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const BLOCKSTREAM_BASE_URL = 'https://blockstream.info/testnet/api'
85 changes: 85 additions & 0 deletions functions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { RuneId, Runestone, none, some } from 'runelib'
import ecc from '@bitcoinerlab/secp256k1'
import { Psbt, networks, payments } from 'bitcoinjs-lib'
import ECPairFactory, { ECPairAPI, ECPairInterface } from 'ecpair'
import axios from 'axios'
import { toOutputScript } from 'bitcoinjs-lib/src/address'
import { BLOCKSTREAM_BASE_URL } from '@/constants'

export async function mint() {
const ECPair: ECPairAPI = ECPairFactory(ecc)

const network = networks.testnet

const mintstone = new Runestone([], none(), some(new RuneId(1, 0)), some(1))
const keyPair = ECPair.makeRandom()

const { address } = payments.p2wpkh({ pubkey: keyPair.publicKey, network })

console.log('address:', address)

const utxos = await getUTXOs(address as string)
console.log(`Using UTXO ${utxos[0].txid}:${utxos[0].vout}`)

const psbt = new Psbt({ network })

psbt.addInput({
hash: utxos[0].txid,
index: utxos[0].vout,
witnessUtxo: {
value: utxos[0].value,
script: toOutputScript(address as string, network),
},
})

psbt.addOutput({
script: mintstone.encipher(),
value: 0,
})

psbt.addOutput({
address: 'tb1pm9rkxwrqddjjyhj8yc0s7gdt4ks5uedgehhaqu93pv9mps8xmdjq07pqgd', // ord address
value: 10000,
})

const fee = 5000

const change = utxos[0].value - fee - 10000

psbt.addOutput({
address: '2N9jgK3Cv8CocHu9ViDvPJhYMmkaeHJojR5', // change address
value: change,
})

await signAndSend(keyPair, psbt, address as string)
}

async function signAndSend(
keyPair: ECPairInterface,
psbt: Psbt,
address: string
) {
psbt.signAllInputs(keyPair)
psbt.finalizeAllInputs()

const tx = psbt.extractTransaction()
const txHex = tx.toHex()

const { data } = await axios.post(`${BLOCKSTREAM_BASE_URL}/tx`, txHex)

console.log('Transaction ID:', data)
}

async function getUTXOs(address: string) {
const { data: utxos } = await axios.get(UTXO_URL(address))
console.log(utxos)

if (!utxos.length) {
throw Error(`No UTXOs available for address ${address}`)
} else {
return utxos
}
}

const UTXO_URL = (address: string) =>
`${BLOCKSTREAM_BASE_URL}/address/${address}/utxo`
48 changes: 0 additions & 48 deletions lib/functions.ts

This file was deleted.

0 comments on commit 903d313

Please sign in to comment.