-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(abstract-utxo): implement parseTransaction for descriptor
TICKET: BTC-1450
- Loading branch information
1 parent
b7eac0e
commit d13a956
Showing
8 changed files
with
221 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { DescriptorMap } from '../../core/descriptor'; | ||
export { explainPsbt } from './explainPsbt'; | ||
export { parse, parse } from './parse'; |
124 changes: 124 additions & 0 deletions
124
modules/abstract-utxo/src/transaction/descriptor/parse.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import * as utxolib from '@bitgo/utxo-lib'; | ||
import { ITransactionRecipient } from '@bitgo/sdk-core'; | ||
|
||
import { AbstractUtxoCoin, BaseOutput, BaseParsedTransaction, ParseTransactionOptions } from '../../abstractUtxoCoin'; | ||
import { isUsefulNamedKeychains, NamedKeychains, toBip32Triple } from '../../keychains'; | ||
import { getDescriptorMapFromWallet, getPolicyForEnv } from '../../descriptor'; | ||
import { IDescriptorWallet } from '../../descriptor/descriptorWallet'; | ||
import * as coreDescriptors from '../../core/descriptor'; | ||
import { ParsedOutput } from '../../core/descriptor/psbt/parse'; | ||
import { outputDifferencesWithExpected, OutputDifferenceWithExpected } from './outputDifference'; | ||
import { parsedDescriptorTransactionToTNumber } from './toTNumber'; | ||
|
||
function toParsedOutput( | ||
coin: AbstractUtxoCoin, | ||
recipient: ITransactionRecipient, | ||
network: utxolib.Network | ||
): ParsedOutput { | ||
return { | ||
address: recipient.address, | ||
value: BigInt(recipient.amount), | ||
script: coin.addressToScript(recipient.address, network), | ||
}; | ||
} | ||
|
||
type ParsedOutputs = OutputDifferenceWithExpected<ParsedOutput> & { | ||
outputs: ParsedOutput[]; | ||
changeOutputs: ParsedOutput[]; | ||
}; | ||
|
||
function parseOutputsWithPsbt( | ||
psbt: utxolib.bitgo.UtxoPsbt, | ||
descriptorMap: coreDescriptors.DescriptorMap, | ||
recipientOutputs: ParsedOutput[] | ||
): ParsedOutputs { | ||
const parsed = coreDescriptors.parse(psbt, descriptorMap, psbt.network); | ||
const externalOutputs = parsed.outputs.filter((o) => o.scriptId === undefined); | ||
const changeOutputs = parsed.outputs.filter((o) => o.scriptId !== undefined); | ||
const outputDiffs = outputDifferencesWithExpected(externalOutputs, recipientOutputs); | ||
return { | ||
outputs: parsed.outputs, | ||
changeOutputs, | ||
...outputDiffs, | ||
}; | ||
} | ||
|
||
export type ParsedDescriptorTransaction<TAmount extends number | bigint> = BaseParsedTransaction< | ||
TAmount, | ||
BaseOutput<TAmount> | ||
>; | ||
|
||
function sumValues(arr: { value: bigint }[]): bigint { | ||
return arr.reduce((sum, e) => sum + e.value, BigInt(0)); | ||
} | ||
|
||
function toBaseOutputs(outputs: ParsedOutput[]): BaseOutput<bigint>[] { | ||
return outputs.map((o) => ({ | ||
address: o.address, | ||
amount: o.value, | ||
external: o.scriptId === undefined, | ||
})); | ||
} | ||
|
||
function toParsedTransaction( | ||
{ outputs, changeOutputs, explicitExternalOutputs, implicitExternalOutputs, missingOutputs }: ParsedOutputs, | ||
{ | ||
keychains, | ||
keySignatures, | ||
}: { | ||
keychains: NamedKeychains; | ||
keySignatures: Record<string, string>; | ||
} | ||
): ParsedDescriptorTransaction<bigint> { | ||
return { | ||
outputs: toBaseOutputs(outputs), | ||
changeOutputs: toBaseOutputs(changeOutputs), | ||
explicitExternalOutputs: toBaseOutputs(explicitExternalOutputs), | ||
explicitExternalSpendAmount: sumValues(explicitExternalOutputs), | ||
implicitExternalOutputs: toBaseOutputs(implicitExternalOutputs), | ||
implicitExternalSpendAmount: sumValues(implicitExternalOutputs), | ||
missingOutputs: toBaseOutputs(missingOutputs), | ||
keychains, | ||
keySignatures, | ||
customChange: undefined, | ||
needsCustomChangeKeySignatureVerification: false, | ||
}; | ||
} | ||
|
||
export function parse<TNumber extends number | bigint>( | ||
coin: AbstractUtxoCoin, | ||
wallet: IDescriptorWallet, | ||
params: ParseTransactionOptions<TNumber> | ||
): ParsedDescriptorTransaction<TNumber> { | ||
if (params.txParams.allowExternalChangeAddress) { | ||
throw new Error('allowExternalChangeAddress is not supported for descriptor wallets'); | ||
} | ||
if (params.txParams.changeAddress) { | ||
throw new Error('changeAddress is not supported for descriptor wallets'); | ||
} | ||
const keychains = params.verification?.keychains; | ||
if (!keychains) { | ||
throw new Error('keychain is required for descriptor wallets'); | ||
} | ||
if (!isUsefulNamedKeychains(keychains)) { | ||
throw new Error('keychain must contain user, backup, and bitgo keys'); | ||
} | ||
const { recipients } = params.txParams; | ||
if (!recipients) { | ||
throw new Error('recipients is required'); | ||
} | ||
const psbt = coin.decodeTransactionFromPrebuild(params.txPrebuild); | ||
if (!(psbt instanceof utxolib.bitgo.UtxoPsbt)) { | ||
throw new Error('expected psbt to be an instance of UtxoPsbt'); | ||
} | ||
const walletKeys = toBip32Triple(keychains); | ||
const descriptorMap = getDescriptorMapFromWallet(wallet, walletKeys, getPolicyForEnv(params.wallet.bitgo.env)); | ||
const recipientOutputs = recipients.map((r) => toParsedOutput(coin, r, psbt.network)); | ||
return parsedDescriptorTransactionToTNumber( | ||
toParsedTransaction(parseOutputsWithPsbt(psbt, descriptorMap, recipientOutputs), { | ||
keychains, | ||
keySignatures, | ||
}), | ||
coin.amountType | ||
); | ||
} |
4 changes: 4 additions & 0 deletions
4
modules/abstract-utxo/src/transaction/descriptor/recipient.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type Recipient = { | ||
address: string; | ||
amount: bigint; | ||
}; |
52 changes: 52 additions & 0 deletions
52
modules/abstract-utxo/src/transaction/descriptor/toTNumber.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as utxolib from '@bitgo/utxo-lib'; | ||
|
||
import { BaseOutput } from '../../abstractUtxoCoin'; | ||
import { ParsedDescriptorTransaction } from './parse'; | ||
|
||
function baseOutputToTNumber<TAmount extends number | bigint>( | ||
output: BaseOutput<bigint>, | ||
amountType: 'number' | 'bigint' | ||
): BaseOutput<TAmount> { | ||
return { | ||
address: output.address, | ||
amount: utxolib.bitgo.toTNumber(output.amount, amountType), | ||
external: output.external, | ||
}; | ||
} | ||
|
||
function entryToTNumber< | ||
K extends keyof ParsedDescriptorTransaction<bigint>, | ||
V extends ParsedDescriptorTransaction<bigint>[K] | ||
>(k: K, v: V, amountType: 'bigint' | 'number'): [K, V] { | ||
switch (k) { | ||
case 'outputs': | ||
case 'changeOutputs': | ||
case 'explicitExternalOutputs': | ||
case 'implicitExternalOutputs': | ||
case 'missingOutputs': | ||
if (v === undefined) { | ||
return [k, v]; | ||
} | ||
if (Array.isArray(v)) { | ||
return [k, v.map((o) => baseOutputToTNumber(o, amountType)) as V]; | ||
} | ||
throw new Error('expected array'); | ||
case 'explicitExternalSpendAmount': | ||
case 'implicitExternalSpendAmount': | ||
if (typeof v !== 'bigint') { | ||
throw new Error('expected bigint'); | ||
} | ||
return [k, utxolib.bitgo.toTNumber(v, amountType) as V]; | ||
default: | ||
return [k, v]; | ||
} | ||
} | ||
|
||
export function parsedDescriptorTransactionToTNumber<TAmount extends number | bigint>( | ||
obj: ParsedDescriptorTransaction<bigint>, | ||
amountType: 'bigint' | 'number' | ||
): ParsedDescriptorTransaction<TAmount> { | ||
return Object.fromEntries( | ||
Object.entries(obj).map(([k, v]) => entryToTNumber(k as keyof ParsedDescriptorTransaction<bigint>, v, amountType)) | ||
) as ParsedDescriptorTransaction<TAmount>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters