Skip to content

Commit

Permalink
chore: Update sign psbt and ui
Browse files Browse the repository at this point in the history
  • Loading branch information
AricRedemption committed Jan 18, 2024
1 parent 035fdf8 commit a071f35
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
15 changes: 6 additions & 9 deletions src/lib/actions/btc/sign-psbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ECPairFactory from 'ecpair'
import { getNetwork } from '@/lib/network'
import * as ecc from '@bitcoin-js/tiny-secp256k1-asmjs'
import btcjs, { Psbt, payments, networks, Transaction } from 'bitcoinjs-lib'
import { getPrivateKey, getAddressType, getPublicKey, getAddress } from '@/lib/account'
import { getPrivateKey, getAddressType, getPublicKey, getAddress, getSigner } from '@/lib/account'

const ECPair = ECPairFactory(ecc)
btcjs.initEccLib(ecc)
Expand Down Expand Up @@ -36,17 +36,16 @@ export interface SignPsbtOptions {
toSignInputs?: UserToSignInput[]
}

export async function process(
export async function process({ psbtHex, options }: {
psbtHex: string,
options?: { toSignInputs?: ToSignInput[]; autoFinalized: boolean }
): Promise<Psbt> {
}): Promise<string> {
const networkType = await getNetwork()
const pubkey = await getPublicKey('btc')
const addressType = await getAddressType('btc')
const privateKey = await getPrivateKey('btc')
const psbtNetwork = networkType === 'mainnet' ? networks.bitcoin : networks.testnet

const keyPair = ECPair.fromWIF(privateKey)
const keyPair = await getSigner('btc', addressType)

if (!options) [
options = { toSignInputs: undefined, autoFinalized: true }
Expand Down Expand Up @@ -78,7 +77,7 @@ export async function process(

options?.toSignInputs.forEach((v) => {
// psbt.signInput(v.index, keyPair)
psbt = psbt.signInput(v.index, keyPair)
psbt.signInput(v.index, keyPair, v.sighashTypes)
})

if (options.autoFinalized) {
Expand All @@ -88,7 +87,7 @@ export async function process(
})
}

return psbt
return psbt.toHex()
}

const formatOptionsToSignInputs = async (_psbt: string | Psbt, options?: SignPsbtOptions) => {
Expand Down Expand Up @@ -140,8 +139,6 @@ const formatOptionsToSignInputs = async (_psbt: string | Psbt, options?: SignPsb
? Psbt.fromHex(_psbt as string, { network: psbtNetwork })
: (_psbt as Psbt);
psbt.data.inputs.forEach((v, index) => {
console.log(`inputs v ${index}`, v);

let script: any = null;
let value = 0;
if (v.witnessUtxo) {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/actions/btc/sign-psbts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export interface ToSignInput {
sighashTypes?: number[]
}

export async function process(psbtHexs: string[], options: { toSignInputs?: ToSignInput[], autoFinalized: boolean }[]): Promise<Psbt[]> {
const psbts: Psbt[] = []
export async function process(psbtHexs: string[], options: { toSignInputs?: ToSignInput[], autoFinalized: boolean }[]): Promise<string[]> {
const psbts:string[] = []
psbtHexs.forEach(async (psbtHex, index) => {
const psbt = await SignPsbt.process(psbtHex, options[index])
const psbt = await SignPsbt.process({ psbtHex, options: options[index] })
psbts.push(psbt)
})
return psbts
Expand Down
22 changes: 8 additions & 14 deletions src/pages/authorize/SignBtcPsbt.vue
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
<script lang="ts" setup>
import { ref } from 'vue'
import { getNetwork } from '@/lib/network'
import { getAddressType, getSigner } from '@/lib/account'
import { Psbt, networks, address as btcAddress } from 'bitcoinjs-lib'
import { CheckBadgeIcon, ChevronDoubleRightIcon, ChevronLeftIcon } from '@heroicons/vue/24/solid'
import { computed, ref } from 'vue'
import actions from '@/data/authorize-actions'
import { prettifyTxId, prettifyBalance } from '@/lib/formatters'
const action = actions.SignBTCPsbt
const props = defineProps<{
params: string
params: {
psbtHex: string,
options: any
}
}>()
const isShowingDetails = ref(false)
const psbtHex = props.params
const psbtHex = props.params.psbtHex
const inputs = ref<{ address: string, value: number }[]>([])
const outputs = ref<{ address: string, value: number }[]>([])
getNetwork().then(async networkType => {
Expand All @@ -28,15 +30,10 @@ getNetwork().then(async networkType => {
}
inputs.value.push({ address, value: inputData.witnessUtxo?.value || 0 })
})
const addressType = await getAddressType('btc')
const signer = await getSigner('btc', addressType)
psbt.signAllInputs(signer).finalizeAllInputs()
const tx = psbt.extractTransaction()
console.log({ tx });
outputs.value = tx.outs.map((out) => ({
address: btcAddress.fromOutputScript(out.script),
outputs.value = psbt.txOutputs.map((out) => ({
value: out.value,
address: out.address || '',
}))
})
</script>
Expand All @@ -56,9 +53,6 @@ getNetwork().then(async networkType => {

<!-- detail body -->
<div class="py-4 grow">
<div class="label">Message</div>
<div class="value break-all">{{ `Sign PSBT` }}</div>

<div class="label mt-4">PSBT Structure</div>
<div class="grid grid-cols-11 items-center mt-1">
<div class="col-span-5 bg-sky-50 border-2 border-sky-300 border-dashed py-2 px-1 rounded-lg">
Expand Down

0 comments on commit a071f35

Please sign in to comment.