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

Prevent account permissions for receive and access from being changed #58

Merged
merged 3 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 21 additions & 0 deletions FungibleToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Int64,
method,
Mina,
Permissions,
PublicKey,
SmartContract,
State,
Expand Down Expand Up @@ -423,6 +424,26 @@ describe("token integration", () => {
})
})

describe("account permissions", () => {
it("should reject a transaction that's changing the account permission for receive", async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice test!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

const permissions = localChain.getAccount(sender, tokenAContract.deriveTokenId()).permissions
permissions.receive = Permissions.impossible()
const updateSend = AccountUpdate.createSigned(
sender,
tokenAContract.deriveTokenId(),
)
updateSend.account.permissions.set(permissions)
await rejects(() =>
Mina.transaction({
sender: sender,
fee: 1e8,
}, async () => {
await tokenAContract.approveBase(AccountUpdateForest.fromFlatArray([updateSend]))
})
)
})
})

describe("pausing/resuming", () => {
const sendAmount = UInt64.from(1)

Expand Down
22 changes: 22 additions & 0 deletions FungibleToken.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import {
AccountUpdate,
AccountUpdateForest,
assert,
Bool,
DeployArgs,
Field,
Int64,
MerkleList,
method,
Permissions,
PublicKey,
Reducer,
State,
state,
Struct,
TokenContract,
Types,
UInt64,
UInt8,
} from "o1js"
Expand Down Expand Up @@ -127,10 +130,29 @@ export class FungibleToken extends TokenContract implements FungibleTokenLike {
this.emitEvent("Transfer", new TransferEvent({ from, to, amount }))
}

private permissionEquals(p1: Types.AuthRequired, p2: Types.AuthRequired) {
return p1.constant
.equals(p2.constant)
.and(p1.signatureNecessary.equals(p2.signatureNecessary))
.and(p1.signatureSufficient.equals(p2.signatureSufficient))
}

private checkPermissionsUpdate(update: AccountUpdate) {
let permissions = update.update.permissions

let { access, receive } = permissions.value
let accessIsNone = this.permissionEquals(access, Permissions.none())
let receiveIsNone = this.permissionEquals(receive, Permissions.none())
let updateAllowed = accessIsNone.and(receiveIsNone)

assert(updateAllowed.or(permissions.isSome.not()))
}

@method
async approveBase(updates: AccountUpdateForest): Promise<void> {
this.paused.getAndRequireEquals().assertFalse()
this.checkZeroBalanceChange(updates)
this.forEachUpdate(updates, (update, _usesToken) => this.checkPermissionsUpdate(update))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do it like the wMina example and combine the logic into a single call to this.forEachUpdate(). Stepping through the tree of account updates comes with a lot of overhead (hashing of account updates!), about 10k constraints IIRC, so it's better to do it just once

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's a good point! I'll change that.

}

@method.returns(UInt64)
Expand Down