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 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
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
29 changes: 28 additions & 1 deletion FungibleToken.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import {
AccountUpdate,
AccountUpdateForest,
assert,
Bool,
DeployArgs,
Field,
Int64,
MerkleList,
method,
Permissions,
Provable,
PublicKey,
Reducer,
State,
state,
Struct,
TokenContract,
Types,
UInt64,
UInt8,
} from "o1js"
Expand Down Expand Up @@ -127,10 +131,33 @@ 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)
let totalBalance = Int64.from(0)
this.forEachUpdate(updates, (update, usesToken) => {
this.checkPermissionsUpdate(update)
totalBalance = Provable.if(usesToken, totalBalance.add(update.balanceChange), totalBalance)
})
totalBalance.assertEquals(Int64.zero)
}

@method.returns(UInt64)
Expand Down