-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Constrain token payment by auth entries #31
Changes from 4 commits
64daed1
64a2869
2b943fb
e9db9a5
3272c3c
939623c
99d3e33
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const ERROR = { | ||
ACCOUNT_NOT_SOURCE: | ||
"Transfer contains authorization entry for a different account", | ||
FOREIGN_CONTRACT_AUTH: | ||
"Transfer contains authorization entry for another contract", | ||
SUB_INVOCATIONS: "Transfer contains sub-invocations to another contract", | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,8 +25,10 @@ import { | |
Transaction, | ||
TransactionBuilder, | ||
XdrLargeInt, | ||
xdr, | ||
} from "stellar-sdk"; | ||
import { buildTransfer, simulateTx } from "../helper/soroban-rpc"; | ||
import { ERROR } from "../helper/error"; | ||
|
||
const API_VERSION = "v1"; | ||
|
||
|
@@ -545,17 +547,37 @@ export async function initApiServer( | |
const simulationResponse = (await server.simulateTransaction( | ||
tx | ||
)) as SorobanRpc.Api.SimulateTransactionSuccessResponse; | ||
|
||
const preparedTransaction = SorobanRpc.assembleTransaction( | ||
tx, | ||
simulationResponse | ||
); | ||
|
||
const built = preparedTransaction.build(); | ||
const sorobanOp = built | ||
.operations[0] as Operation.InvokeHostFunction; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we inspect all operations or is only looking at the first one always sufficient? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the case of a tx with an InvokeHostFunction, there can only be 1 operation. We could check for the operation type here but only the InvokeHostFunction type has auth. I'll add an explicit check for the op type though, that seems a bit more clear. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added an explicit check for op type in 3272c3c |
||
const auths = sorobanOp.auth || []; | ||
|
||
for (const auth of auths) { | ||
if ( | ||
auth.credentials().switch() !== | ||
xdr.SorobanCredentialsType.sorobanCredentialsSourceAccount() | ||
) { | ||
throw new Error(ERROR.ACCOUNT_NOT_SOURCE); | ||
} | ||
|
||
if (auth.rootInvocation().subInvocations().length) { | ||
throw new Error(ERROR.SUB_INVOCATIONS); | ||
} | ||
} | ||
|
||
const data = { | ||
simulationResponse, | ||
preparedTransaction: preparedTransaction.build().toXDR(), | ||
preparedTransaction: built.toXDR(), | ||
}; | ||
reply.code(200).send(data); | ||
} catch (error) { | ||
reply.code(400).send(JSON.stringify(error)); | ||
reply.code(400).send(error); | ||
} | ||
}, | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did we mean to use this error in this pull request or are you planning to use it in a follow-up change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I'll remove that one, @sisuresh pointed how that check was already covered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed in 3272c3c