Skip to content
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

Add check for from address in AccountManager.sendTransaction #2103

Merged
merged 4 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,6 @@ describe("send transaction", () => {
})

test("send transaction mainnet", async () => {
const user = mockUser().mock
const accountManager = new AccountManager(user)

const mockTxResult = {
onceExecuted: jest.fn().mockResolvedValue({
events: [
Expand All @@ -219,6 +216,10 @@ describe("send transaction", () => {

jest.mocked(fcl.tx).mockReturnValue(mockTxResult)
jest.mocked(fcl.mutate).mockResolvedValue("1111")
jest.mocked(fcl.query).mockResolvedValue("0x1234")

const user = mockUser({addr: "0x4444"} as CurrentUser).mock
const accountManager = new AccountManager(user)

const tx = {
to: "0x1234",
Expand Down Expand Up @@ -247,9 +248,6 @@ describe("send transaction", () => {
})

test("send transaction testnet", async () => {
const user = mockUser()
const accountManager = new AccountManager(user.mock)

const mockTxResult = {
onceExecuted: jest.fn().mockResolvedValue({
events: [
Expand All @@ -265,6 +263,10 @@ describe("send transaction", () => {

jest.mocked(fcl.tx).mockReturnValue(mockTxResult)
jest.mocked(fcl.mutate).mockResolvedValue("1111")
jest.mocked(fcl.query).mockResolvedValue("0x1234")

const user = mockUser({addr: "0x4444"} as CurrentUser)
const accountManager = new AccountManager(user.mock)

const tx = {
to: "0x1234",
Expand Down Expand Up @@ -293,9 +295,6 @@ describe("send transaction", () => {
})

test("throws error if no executed event not found", async () => {
const user = mockUser()
const accountManager = new AccountManager(user.mock)

const mockTxResult = {
onceExecuted: jest.fn().mockResolvedValue({
events: [],
Expand All @@ -304,6 +303,10 @@ describe("send transaction", () => {

jest.mocked(fcl.tx).mockReturnValue(mockTxResult)
jest.mocked(fcl.mutate).mockResolvedValue("1111")
jest.mocked(fcl.query).mockResolvedValue("0x1234")

const user = mockUser({addr: "0x4444"} as CurrentUser)
const accountManager = new AccountManager(user.mock)

const tx = {
to: "0x1234",
Expand All @@ -319,6 +322,28 @@ describe("send transaction", () => {
"EVM transaction hash not found"
)
})

test("throws error if from address does not match user address", async () => {
jest.mocked(fcl.query).mockResolvedValue("0x1234")
const user = mockUser({addr: "0x4444"} as CurrentUser)
const accountManager = new AccountManager(user.mock)

const tx = {
to: "0x1234",
from: "0x4567",
value: "0",
data: "0x1234",
nonce: "0",
gas: "0",
chainId: "646",
}

await expect(accountManager.sendTransaction(tx)).rejects.toThrow(
`From address does not match authenticated user address.\nUser: 0x1234\nFrom: 0x4567`
)

expect(fcl.mutate).not.toHaveBeenCalled()
})
})

describe("signMessage", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ export class AccountManager {
FLOW_CONTRACTS[ContractType.EVM][flowNetwork]
)

// Check if the from address matches the authenticated COA address
const expectedCOAAddress = await this.getCOAAddress()
if (from.toLowerCase() !== expectedCOAAddress?.toLowerCase()) {
throw new Error(
`From address does not match authenticated user address.\nUser: ${expectedCOAAddress}\nFrom: ${from}`
)
}

const txId = await fcl.mutate({
cadence: `import EVM from ${evmContractAddress}

Expand Down