Skip to content

Commit 98ea89a

Browse files
mukeshspclaude
andcommitted
fix(sdk-coin-algo): surface rekey and block blind signing of rekeyed txs
- explainTransaction now includes reKeyTo in the explanation and displayOrder for pay, axfer and keyreg transactions carrying a rekey - toJson() emits reKeyTo so decoded rekeys are visible to all consumers - signTransaction throws InvalidTransactionError when the decoded transaction sets reKeyTo unless the caller passes allowReKeyTo: true - unit tests cover explain (pay/axfer) and sign (reject + opt-in) Ticket: CSHLD-2171 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 6aef749 commit 98ea89a

4 files changed

Lines changed: 129 additions & 1 deletion

File tree

‎modules/sdk-coin-algo/src/algo.ts‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
TransactionExplanation,
3030
TransactionRecipient,
3131
TransactionType,
32+
InvalidTransactionError,
3233
UnexpectedAddressError,
3334
VerifyAddressOptions,
3435
VerifyTransactionOptions,
@@ -74,6 +75,7 @@ export interface VerifyAlgoAddressOptions extends VerifyAddressOptions {
7475
export interface AlgoTransactionExplanation extends TransactionExplanation {
7576
memo?: string;
7677
type?: string | number;
78+
reKeyTo?: string;
7779
voteKey?: string;
7880
selectionKey?: string;
7981
voteFirst?: number;
@@ -91,6 +93,12 @@ export interface TransactionOperation {
9193
export interface SignTransactionOptions extends BaseSignTransactionOptions {
9294
txPrebuild: TransactionPrebuild;
9395
prv: string;
96+
/**
97+
* Explicit opt-in required to sign a transaction that sets `reKeyTo`. A rekey hands
98+
* spending authority of the account to another address; callers must confirm the
99+
* rekey is intended before passing this flag.
100+
*/
101+
allowReKeyTo?: boolean;
94102
}
95103

96104
export interface TransactionPrebuild {
@@ -412,6 +420,13 @@ export class Algo extends BaseCoin {
412420
operations,
413421
};
414422

423+
// A rekey silently reassigns spending authority of the account, so it must never
424+
// be hidden from the explanation shown for approval.
425+
if (txJson.reKeyTo) {
426+
displayOrder.push('reKeyTo');
427+
explanationResult.reKeyTo = txJson.reKeyTo;
428+
}
429+
415430
if (txJson.tokenId) {
416431
explanationResult.tokenId = txJson.tokenId;
417432
}
@@ -432,7 +447,7 @@ export class Algo extends BaseCoin {
432447
'voteKeyDilution',
433448
];
434449

435-
return {
450+
const explanationResult: AlgoTransactionExplanation = {
436451
displayOrder,
437452
id: txJson.id,
438453
outputAmount: '0',
@@ -448,6 +463,14 @@ export class Algo extends BaseCoin {
448463
voteLast: txJson.voteLast,
449464
voteKeyDilution: txJson.voteKeyDilution,
450465
};
466+
467+
// A keyreg transaction can also carry a rekey; surface it like in the send case.
468+
if (txJson.reKeyTo) {
469+
displayOrder.push('reKeyTo');
470+
explanationResult.reKeyTo = txJson.reKeyTo;
471+
}
472+
473+
return explanationResult;
451474
}
452475
}
453476

@@ -550,6 +573,17 @@ export class Algo extends BaseCoin {
550573
*/
551574
async signTransaction(params: SignTransactionOptions): Promise<SignedTransaction> {
552575
const { txHex, signers, prv, isHalfSigned, numberSigners } = this.verifySignTransactionParams(params);
576+
// A rekey reassigns spending authority of the account; refuse to sign it unless the
577+
// caller has explicitly confirmed the rekey is intended (e.g. it was displayed for
578+
// approval). Prevents approving what looks like a payment but signs away the account.
579+
const { txn } = Utils.decodeAlgoTxn(txHex);
580+
if (txn.reKeyTo && !params.allowReKeyTo) {
581+
throw new InvalidTransactionError(
582+
`transaction rekeys the account to ${algosdk.encodeAddress(
583+
txn.reKeyTo.publicKey
584+
)}: refusing to sign unless the rekey is confirmed intentional (pass allowReKeyTo: true)`
585+
);
586+
}
553587
const factory = this.getBuilder();
554588
const txBuilder = factory.from(txHex);
555589
txBuilder.numberOfRequiredSigners(numberSigners);

‎modules/sdk-coin-algo/src/lib/ifaces.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface TxData {
2020
genesisID: string;
2121
genesisHash: string | Buffer;
2222
closeRemainderTo?: string;
23+
reKeyTo?: string;
2324
nonParticipation?: boolean;
2425
txType?: string;
2526
tokenName?: string;

‎modules/sdk-coin-algo/src/lib/transaction.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ export class Transaction extends BaseTransaction {
252252
if (this._algoTransaction.closeRemainderTo) {
253253
result.closeRemainderTo = algosdk.encodeAddress(this._algoTransaction.closeRemainderTo.publicKey);
254254
}
255+
if (this._algoTransaction.reKeyTo) {
256+
result.reKeyTo = algosdk.encodeAddress(this._algoTransaction.reKeyTo.publicKey);
257+
}
255258
if (this.type === TransactionType.Send) {
256259
result.to = algosdk.encodeAddress(this._algoTransaction.to.publicKey);
257260
result.amount = this._algoTransaction.amount.toString();

‎modules/sdk-coin-algo/test/unit/algo.ts‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,23 @@ describe('ALGO:', function () {
233233
explain.outputs[0].amount.should.equal(amount.toString());
234234
explain.outputs[0].address.should.equal(destination);
235235
});
236+
237+
it('should surface the rekey target when explaining a transfer transaction that rekeys the account', async function () {
238+
const sender = AlgoResources.accounts.account1.address;
239+
const destination = AlgoResources.accounts.account2.address;
240+
const rekeyAddress = AlgoResources.accounts.account3.address;
241+
const txBuilder = buildBaseTransferTransaction({ sender, destination, amount: 10000 });
242+
txBuilder.reKeyTo({ address: rekeyAddress });
243+
const unsignedTransaction = await txBuilder.build();
244+
const unsignedHex = Buffer.from(unsignedTransaction.toBroadcastFormat()).toString('hex');
245+
const explain = await basecoin.explainTransaction({
246+
txHex: unsignedHex,
247+
feeInfo: { fee: '1000' },
248+
});
249+
explain.reKeyTo.should.equal(rekeyAddress);
250+
explain.displayOrder.should.containEql('reKeyTo');
251+
explain.outputs[0].address.should.equal(destination);
252+
});
236253
});
237254

238255
describe('Asset Transfer Builder: ', () => {
@@ -349,6 +366,23 @@ describe('ALGO:', function () {
349366
explain.outputs[0].address.should.equal(destination);
350367
explain.tokenId.should.equal(1);
351368
});
369+
370+
it('should surface the rekey target when explaining an asset transfer transaction that rekeys the account', async function () {
371+
const sender = AlgoResources.accounts.account1.address;
372+
const destination = AlgoResources.accounts.account2.address;
373+
const rekeyAddress = AlgoResources.accounts.account3.address;
374+
const txBuilder = buildBaseAssetTransferTransaction({ sender, destination, amount: 10000, tokenId: 1 });
375+
txBuilder.reKeyTo({ address: rekeyAddress });
376+
const unsignedTransaction = await txBuilder.build();
377+
const unsignedHex = Buffer.from(unsignedTransaction.toBroadcastFormat()).toString('hex');
378+
const explain = await basecoin.explainTransaction({
379+
txHex: unsignedHex,
380+
feeInfo: { fee: '1000' },
381+
});
382+
explain.reKeyTo.should.equal(rekeyAddress);
383+
explain.displayOrder.should.containEql('reKeyTo');
384+
explain.outputs[0].address.should.equal(destination);
385+
});
352386
});
353387

354388
describe('Wallet Init Builder: ', () => {
@@ -648,6 +682,62 @@ describe('ALGO:', function () {
648682
});
649683
signed.txHex.should.equal(AlgoResources.rawTx.transfer.multisig);
650684
});
685+
686+
it('should refuse to sign a transaction that rekeys the account', async function () {
687+
const factory = new TransactionBuilderFactory(coins.get('algo'));
688+
const txBuilder = factory
689+
.getTransferBuilder()
690+
.sender({ address: AlgoResources.accounts.account1.address })
691+
.to({ address: AlgoResources.accounts.account2.address })
692+
.amount(10000)
693+
.isFlatFee(true)
694+
.fee({ fee: '1000' })
695+
.firstRound(1)
696+
.lastRound(100)
697+
.testnet();
698+
txBuilder.reKeyTo({ address: AlgoResources.accounts.account3.address });
699+
const unsignedTx = await txBuilder.build();
700+
const unsignedHex = Buffer.from(unsignedTx.toBroadcastFormat()).toString('hex');
701+
702+
await basecoin
703+
.signTransaction({
704+
txPrebuild: {
705+
txHex: unsignedHex,
706+
keys: [AlgoResources.accounts.account1.pubKey.toString('hex')],
707+
addressVersion: 1,
708+
},
709+
prv: AlgoResources.accounts.account1.prvKey,
710+
})
711+
.should.be.rejectedWith(/refusing to sign unless the rekey is confirmed intentional/);
712+
});
713+
714+
it('should sign a transaction that rekeys the account when the rekey is explicitly allowed', async function () {
715+
const factory = new TransactionBuilderFactory(coins.get('algo'));
716+
const txBuilder = factory
717+
.getTransferBuilder()
718+
.sender({ address: AlgoResources.accounts.account1.address })
719+
.to({ address: AlgoResources.accounts.account2.address })
720+
.amount(10000)
721+
.isFlatFee(true)
722+
.fee({ fee: '1000' })
723+
.firstRound(1)
724+
.lastRound(100)
725+
.testnet();
726+
txBuilder.reKeyTo({ address: AlgoResources.accounts.account3.address });
727+
const unsignedTx = await txBuilder.build();
728+
const unsignedHex = Buffer.from(unsignedTx.toBroadcastFormat()).toString('hex');
729+
730+
const signed = await basecoin.signTransaction({
731+
txPrebuild: {
732+
txHex: unsignedHex,
733+
keys: [AlgoResources.accounts.account1.pubKey.toString('hex')],
734+
addressVersion: 1,
735+
},
736+
prv: AlgoResources.accounts.account1.prvKey,
737+
allowReKeyTo: true,
738+
});
739+
assert.strictEqual(typeof signed.txHex, 'string');
740+
});
651741
});
652742

653743
describe('Sign message', () => {

0 commit comments

Comments
 (0)