Skip to content

Commit

Permalink
Add proper error messages
Browse files Browse the repository at this point in the history
Also, tried to add a test that demonstrates that verifying a nonexistent token
fails. However, the error that's raised within the smart contract does not get
caught by the test, and the test fails (although running the test shows that the
right thing happens).
  • Loading branch information
kantp committed Jan 30, 2024
1 parent 7be26bc commit 8d53f37
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/SoulboundErrors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const SoulboundErrors = {
invalidToken:
'This token is not valid. It might have been revoked, or has not been issued.',
alreadyExists:
'Cannot issue token, because it has already been issued.',
wrongPolicy:
'RevocationPolicy does not match what is expected by the issuer',
};
export { SoulboundErrors };
11 changes: 8 additions & 3 deletions src/SoulboundToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
} from 'o1js';
import { RevocationPolicy } from './RevocationPolicy';
import { SoulboundMetadata } from './SoulboundMetadata';
import { SoulboundErrors } from './SoulboundErrors';

class TokenState extends Struct({
issueState: Field
Expand Down Expand Up @@ -70,7 +71,11 @@ class SoulboundToken
this.currentSlot.requireBetween(lower, upper);

// Check that the `RevocationPolicy` has the right value
metadata.revocationPolicy.type.assertEquals(this.revocationPolicy.get().type);
metadata.revocationPolicy.type
.assertEquals(
this.revocationPolicy.get().type,
SoulboundErrors.wrongPolicy
);

// Check signature of holder
signature.verify(metadata.holderKey, SoulboundMetadata.toFields(metadata)).assertEquals(Bool(true));
Expand Down Expand Up @@ -115,8 +120,8 @@ class SoulboundToken

const expectedKey = metadata.hash();
const [root, key] = witness.computeRootAndKey(TokenState.types.issued)
this.root.requireEquals(root);
expectedKey.assertEquals(key);
root.assertEquals(this.root.get(), SoulboundErrors.invalidToken);
expectedKey.assertEquals(key, SoulboundErrors.invalidToken);
}
}

Expand Down
14 changes: 10 additions & 4 deletions test/SoulboundToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SoulboundToken } from "../src/SoulboundToken";
import { RevocationPolicy } from "../src/RevocationPolicy";
import { SoulboundMetadata } from "../src/SoulboundMetadata";
import { SoulboundTokenDriver } from "./SoulboundTokenDriver";
import { SoulboundErrors } from "../src/SoulboundErrors";

const accountCreationFee = 0;
const proofsEnabled = false;
Expand Down Expand Up @@ -74,10 +75,15 @@ describe('SoulboundToken', () => {
await driver.issue(validMetadata, signature);
await driver.verify(validMetadata);
});
it('fails to validate a nonexistent token',async () => {
await driver.deploy();
await driver.verify(validMetadata);
})

// For some reason, this test does not work:
// The error is thrown, but not caught by `toThrow()`
// it('fails to validate a nonexistent token', async () => {
// await driver.deploy();
// await expect(async () => {
// await driver.verify(validMetadata)
// }).rejects.toThrow()
// })
it.todo('revokes an issued token');
it.todo('fails to verify a revoked token');
it.todo('fails to revoke a token that has not been issued');
Expand Down

0 comments on commit 8d53f37

Please sign in to comment.