Skip to content

Commit

Permalink
Throw error if you try to sign transactions that need contract signat…
Browse files Browse the repository at this point in the history
…ures. (#576)

* Attempting to add an error if you try to sign a transaction that needs contract signatures

* Use the parent class' .operations

---------

Co-authored-by: George Kudrayvtsev <george@stellar.org>
  • Loading branch information
Paul Bellamy and Shaptic authored Feb 14, 2023
1 parent 0095ea5 commit 123279a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/fee_bump_transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ export class FeeBumpTransaction extends TransactionBase {
return this._innerTransaction;
}

/**
* @type {Operation[]}
* @readonly
*/
get operations() {
return this._innerTransaction.operations;
}

/**
* @type {string}
* @readonly
Expand Down
11 changes: 11 additions & 0 deletions src/transaction_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ export class TransactionBase {
* @returns {void}
*/
sign(...keypairs) {
// Temporary warning for contract auth-next signatures not being supported.
const requiresContractSignatures = (this.operations || []).some(
(op) =>
op.type === 'invokeHostFunction' &&
op.auth.some((a) => a.addressWithNonce())
);
if (requiresContractSignatures) {
throw new Error(
'Soroban contract signatures are not supported in this version of the SDK.'
);
}
const txHash = this.hash();
keypairs.forEach((kp) => {
const sig = kp.signDecorated(txHash);
Expand Down
49 changes: 49 additions & 0 deletions test/unit/transaction_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,55 @@ describe('Transaction', function() {
);
});

it('returns error when transaction includes soroban contract auth', function() {
let source = new StellarBase.Account(
'GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB',
'0'
);
let signer = StellarBase.Keypair.master(StellarBase.Networks.TESTNET);

let tx = new StellarBase.TransactionBuilder(source, {
fee: 100,
networkPassphrase: StellarBase.Networks.TESTNET
})
.addOperation(
StellarBase.Operation.invokeHostFunction({
function: StellarBase.xdr.HostFunction.hostFunctionTypeInvokeContract(
[]
),
footprint: new StellarBase.xdr.LedgerFootprint({
readOnly: [],
readWrite: []
}),
auth: [
new StellarBase.xdr.ContractAuth({
// Include an AddressWithNonce to trigger this
addressWithNonce: new StellarBase.xdr.AddressWithNonce({
address: new StellarBase.Address(
source.accountId()
).toScAddress(),
nonce: StellarBase.xdr.Uint64.fromString('0')
}),
// Rest of params are irrelevant
rootInvocation: new StellarBase.xdr.AuthorizedInvocation({
contractId: new Buffer(32),
functionName: 'test',
args: [],
subInvocations: []
}),
signatureArgs: []
})
]
})
)
.setTimeout(StellarBase.TimeoutInfinite)
.build();

expect(() => tx.sign(signer)).to.throw(
/Soroban contract signatures are not supported in this version of the SDK./
);
});

it('adds signature correctly', function() {
const sourceKey =
'GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB';
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ export namespace Operation {
function: xdr.HostFunction;
parameters: xdr.ScVal[];
footprint: xdr.LedgerFootprint;
auth: xdr.ContractAuth[];
}
function invokeHostFunction(
options: OperationOptions.InvokeHostFunction
Expand Down

0 comments on commit 123279a

Please sign in to comment.