Skip to content

Commit 3de9a1d

Browse files
fix(trezor): fix output format mapping on trezor HW devices
1 parent 9256fdd commit 3de9a1d

File tree

6 files changed

+43
-47
lines changed

6 files changed

+43
-47
lines changed

packages/hardware-trezor/src/TrezorKeyAgent.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ export class TrezorKeyAgent extends KeyAgentBase {
251251
chainId: this.chainId,
252252
knownAddresses,
253253
tagCborSets: txBody.hasTaggedSets(),
254-
txInKeyPathMap
254+
txInKeyPathMap,
255+
useBabbageOutputs: txBody.hasBabbageOutput()
255256
});
256257

257258
const signingMode = TrezorKeyAgent.matchSigningMode(trezorTxData);

packages/hardware-trezor/src/transformers/txOut.ts

+1-29
Original file line numberDiff line numberDiff line change
@@ -40,42 +40,14 @@ const getScriptHex = (output: Serialization.TransactionOutput): HexBlob | undefi
4040
return scriptRef.toCbor();
4141
};
4242

43-
/**
44-
* There are currently two types of outputs supported by the ledger:
45-
*
46-
* legacy_transaction_output =
47-
* [ address
48-
* , amount : value
49-
* , ? datum_hash : $hash32
50-
* ]
51-
*
52-
* and
53-
*
54-
* post_alonzo_transaction_output =
55-
* { 0 : address
56-
* , 1 : value
57-
* , ? 2 : datum_option ; New; datum option
58-
* , ? 3 : script_ref ; New; script reference
59-
* }
60-
*
61-
* Legacy outputs are definite length arrays of three elements, however the new babbage outputs are definite length maps
62-
* of four elements.
63-
*
64-
* @param out The output to be verified.
65-
*/
66-
const isBabbage = (out: Serialization.TransactionOutput): boolean => {
67-
const reader = new Serialization.CborReader(out.toCbor());
68-
return reader.peekState() === Serialization.CborReaderState.StartMap;
69-
};
70-
7143
const getInlineDatum = (datum: Cardano.PlutusData): string => Serialization.PlutusData.fromCore(datum).toCbor();
7244

7345
export const toTxOut: Transform<Cardano.TxOut, Trezor.CardanoOutput, TrezorTxTransformerContext> = (txOut, context) => {
7446
const destination = toDestination(txOut, context);
7547
const output = Serialization.TransactionOutput.fromCore(txOut);
7648
const scriptHex = getScriptHex(output);
7749

78-
return isBabbage(output)
50+
return context?.useBabbageOutputs
7951
? {
8052
...destination,
8153
amount: txOut.value.coins.toString(),

packages/hardware-trezor/src/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export type TrezorTxTransformerContext = {
1313
accountIndex: number;
1414
/** Whether sets should be encoded as tagged set in CBOR */
1515
tagCborSets: boolean;
16+
/** Whether to use Babbage output format or not. */
17+
useBabbageOutputs: boolean;
1618
} & SignTransactionContext;
1719

1820
export type TrezorTxOutputDestination =

packages/hardware-trezor/test/testData.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ export const contextWithKnownAddresses: TrezorTxTransformerContext = {
171171
},
172172
knownAddresses: [knownAddress],
173173
tagCborSets: false,
174-
txInKeyPathMap: {}
174+
txInKeyPathMap: {},
175+
useBabbageOutputs: false
175176
};
176177

177178
export const contextWithKnownAddressesWithoutStakingCredentials: TrezorTxTransformerContext = {
@@ -182,7 +183,8 @@ export const contextWithKnownAddressesWithoutStakingCredentials: TrezorTxTransfo
182183
},
183184
knownAddresses: [knownAddressWithoutStakingPath],
184185
tagCborSets: false,
185-
txInKeyPathMap: {}
186+
txInKeyPathMap: {},
187+
useBabbageOutputs: false
186188
};
187189

188190
export const contextWithoutKnownAddresses: TrezorTxTransformerContext = {
@@ -193,7 +195,8 @@ export const contextWithoutKnownAddresses: TrezorTxTransformerContext = {
193195
},
194196
knownAddresses: [],
195197
tagCborSets: false,
196-
txInKeyPathMap: {}
198+
txInKeyPathMap: {},
199+
useBabbageOutputs: false
197200
};
198201

199202
export const coreWithdrawalWithKeyHashCredential = {

packages/hardware-trezor/test/transformers/tx.test.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ describe('tx', () => {
204204
...contextWithKnownAddresses,
205205
txInKeyPathMap: {
206206
[TxInId(babbageTxBodyWithScripts.inputs[0])]: knownAddressPaymentKeyPath
207-
}
207+
},
208+
useBabbageOutputs: true
208209
})
209210
).toEqual({
210211
additionalWitnessRequests: [
@@ -327,7 +328,8 @@ describe('tx', () => {
327328
txInKeyPathMap: {
328329
[TxInId(plutusTxWithBabbage.inputs[0])]: knownAddressPaymentKeyPath,
329330
[TxInId(plutusTxWithBabbage.collaterals[0])]: knownAddressPaymentKeyPath
330-
}
331+
},
332+
useBabbageOutputs: true
331333
})
332334
).toEqual({
333335
additionalWitnessRequests: [
@@ -361,7 +363,7 @@ describe('tx', () => {
361363
address:
362364
'addr_test1qz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3jcu5d8ps7zex2k2xt3uqxgjqnnj83ws8lhrn648jjxtwq2ytjqp',
363365
amount: '10',
364-
format: Trezor.PROTO.CardanoTxOutputSerializationFormat.ARRAY_LEGACY
366+
format: Trezor.PROTO.CardanoTxOutputSerializationFormat.MAP_BABBAGE
365367
},
366368
fee: '10',
367369
inputs: [

packages/hardware-trezor/test/transformers/txOut.test.ts

+27-11
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,16 @@ describe('txOut', () => {
156156
});
157157

158158
it('can map a set of transaction outputs with both output formats', async () => {
159-
const txOuts = mapTxOuts(
160-
[txOutWithDatumHashAndOwnedAddress, txOutWithReferenceScriptAndDatumHash],
161-
contextWithKnownAddresses
162-
);
159+
const legacyTxOuts = mapTxOuts([txOutWithDatumHashAndOwnedAddress], contextWithKnownAddresses);
163160

164-
expect(txOuts.length).toEqual(2);
161+
const babbageTxOuts = mapTxOuts([txOutWithReferenceScriptAndDatumHash], {
162+
...contextWithKnownAddresses,
163+
useBabbageOutputs: true
164+
});
165165

166-
expect(txOuts).toEqual([
166+
expect(legacyTxOuts.length).toEqual(1);
167+
168+
expect(legacyTxOuts).toEqual([
167169
{
168170
addressParameters: {
169171
addressType: Trezor.PROTO.CardanoAddressType.BASE,
@@ -173,7 +175,12 @@ describe('txOut', () => {
173175
amount: '10',
174176
datumHash: '0f3abbc8fc19c2e61bab6059bf8a466e6e754833a08a62a6c56fe0e78f19d9d5',
175177
format: Trezor.PROTO.CardanoTxOutputSerializationFormat.ARRAY_LEGACY
176-
},
178+
}
179+
]);
180+
181+
expect(babbageTxOuts.length).toEqual(1);
182+
183+
expect(babbageTxOuts).toEqual([
177184
{
178185
addressParameters: {
179186
addressType: Trezor.PROTO.CardanoAddressType.BASE,
@@ -332,7 +339,7 @@ describe('txOut', () => {
332339
});
333340

334341
it('can map simple transaction with inline datum', async () => {
335-
const out = toTxOut(txOutWithInlineDatum, contextWithKnownAddresses);
342+
const out = toTxOut(txOutWithInlineDatum, { ...contextWithKnownAddresses, useBabbageOutputs: true });
336343

337344
expect(out).toEqual({
338345
address:
@@ -344,7 +351,10 @@ describe('txOut', () => {
344351
});
345352

346353
it('can map simple transaction with inline datum to owned address', async () => {
347-
const out = toTxOut(txOutWithInlineDatumAndOwnedAddress, contextWithKnownAddresses);
354+
const out = toTxOut(txOutWithInlineDatumAndOwnedAddress, {
355+
...contextWithKnownAddresses,
356+
useBabbageOutputs: true
357+
});
348358

349359
expect(out).toEqual({
350360
addressParameters: {
@@ -359,7 +369,10 @@ describe('txOut', () => {
359369
});
360370

361371
it('can map a simple transaction output with reference script and datum hash', async () => {
362-
const out = toTxOut(txOutWithReferenceScriptAndDatumHash, contextWithKnownAddresses);
372+
const out = toTxOut(txOutWithReferenceScriptAndDatumHash, {
373+
...contextWithKnownAddresses,
374+
useBabbageOutputs: true
375+
});
363376
expect(out).toEqual({
364377
addressParameters: {
365378
addressType: Trezor.PROTO.CardanoAddressType.BASE,
@@ -374,7 +387,10 @@ describe('txOut', () => {
374387
});
375388

376389
it('can map a simple transaction output with reference script and inline datum', async () => {
377-
const out = toTxOut(txOutWithReferenceScriptAndInlineDatum, contextWithKnownAddresses);
390+
const out = toTxOut(txOutWithReferenceScriptAndInlineDatum, {
391+
...contextWithKnownAddresses,
392+
useBabbageOutputs: true
393+
});
378394
expect(out).toEqual({
379395
addressParameters: {
380396
addressType: Trezor.PROTO.CardanoAddressType.BASE,

0 commit comments

Comments
 (0)