Skip to content

Commit

Permalink
👌 IMPROVE: Simplify the sign transactions api to make it more easy to…
Browse files Browse the repository at this point in the history
… use.
  • Loading branch information
riverrun46 committed Sep 20, 2023
1 parent c848dac commit 0038f5f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 23 deletions.
11 changes: 3 additions & 8 deletions src/lib/actions/sign-transactions.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { getAddress, getCurrentAccount, privateKey } from '../account'
import connector from '../connector'
import { signTransaction } from '../crypto'
import { signTransaction, signTransactions } from '../crypto'

export async function process(params: any, host: string) {
const wif = await getCurrentAccount().then((account) => privateKey.value)

const signingTransactions = params.transactions
const signedTransactions = signTransactions(wif, signingTransactions)

const signatures = signingTransactions.map((transaction: any) => {
const signature = signTransaction(wif, transaction)

return signature
})

return { signatures }
return { signedTransactions }
}
90 changes: 75 additions & 15 deletions src/lib/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,16 @@ export const verifySignature = (
return { verified }
}

type ToSignTransaction = {
txHex: string
scriptHex: string
inputIndex: number
satoshis: number
sigtype?: number
}
export const signTransaction = (
wif: string,
{
txHex,
scriptHex,
address,
inputIndex,
satoshis,
sigtype,
}: {
txHex: string
scriptHex: string
address?: string
inputIndex: number
satoshis: number
sigtype?: number
},
{ txHex, scriptHex, inputIndex, satoshis, sigtype }: ToSignTransaction,
returnsTransaction: boolean = false
) => {
if (!sigtype) sigtype = mvc.crypto.Signature.SIGHASH_ALL | mvc.crypto.Signature.SIGHASH_FORKID
Expand Down Expand Up @@ -120,3 +113,70 @@ export const signTransaction = (
txid: tx.id,
}
}

export const signTransactions = (wif: string, toSignTransactions: ToSignTransaction[]) => {
const privateKey = mvc.PrivateKey.fromWIF(wif)
const publicKey = privateKey.toPublicKey()

// find out if transactions other than the first one are dependent on previous ones
// if so, we need to sign them in order, and sequentially update the prevTxId of the later ones
// so that the signature of the previous one can be calculated correctly
const toSignTransactionsWithDependsOn: (ToSignTransaction & {
dependsOn?: number
})[] = []
const txids = toSignTransactions.map((tx) => new mvc.Transaction(tx.txHex).id)
for (let i = 0; i < toSignTransactions.length; i++) {
const { txHex, inputIndex } = toSignTransactions[i]
const tx = new mvc.Transaction(txHex)
const prevTxId = tx.inputs[inputIndex].prevTxId.toString('hex')
if (txids.includes(prevTxId)) {
toSignTransactionsWithDependsOn.push({
...toSignTransactions[i],
dependsOn: txids.indexOf(prevTxId),
})
} else {
toSignTransactionsWithDependsOn.push(toSignTransactions[i])
}
}

const signedTransactions: {
txHex: string
txid: string
}[] = []
for (let i = 0; i < toSignTransactionsWithDependsOn.length; i++) {
let { txHex, scriptHex, inputIndex, satoshis, sigtype } = toSignTransactionsWithDependsOn[i]
const toSign = toSignTransactionsWithDependsOn[i]

if (!sigtype) {
sigtype = mvc.crypto.Signature.SIGHASH_ALL | mvc.crypto.Signature.SIGHASH_FORKID
}

const tx = new mvc.Transaction(txHex)

// find out if this transaction depends on previous ones
// if so, we need to update the prevTxId of the current one
if (toSign.dependsOn !== undefined) {
const prevTxId = signedTransactions[toSign.dependsOn].txid
tx.inputs[inputIndex].prevTxId = Buffer.from(prevTxId, 'hex')
}

// Build signature of this input
const signature = mvc.Transaction.Sighash.sign(
tx,
privateKey,
sigtype,
inputIndex,
new mvc.Script(scriptHex),
new BN(satoshis)
)
const signatureScript = mvc.Script.buildPublicKeyHashIn(publicKey, signature, sigtype)
tx.inputs[inputIndex].setScript(signatureScript)

signedTransactions.push({
txHex: tx.toString(),
txid: tx.id,
})
}

return signedTransactions
}

0 comments on commit 0038f5f

Please sign in to comment.