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

Error message if token symbol length too long #479

Merged
merged 2 commits into from
Oct 12, 2022
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
18 changes: 17 additions & 1 deletion src/lib/circuit_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'reflect-metadata';
import { Circuit, JSONValue, ProvablePure, Provable } from '../snarky.js';
import { Field, Bool } from './core.js';
import { Context } from './global-context.js';
import { HashInput } from './hash.js';
import { inCheckedComputation, snarkContext } from './proof_system.js';

// external API
Expand Down Expand Up @@ -33,6 +32,7 @@ export {
witness,
InferCircuitValue,
Provables,
HashInput,
};

type Constructor<T> = new (...args: any) => T;
Expand All @@ -43,6 +43,22 @@ type NonMethodKeys<T> = {
}[keyof T];
type NonMethods<T> = Pick<T, NonMethodKeys<T>>;

type HashInput = { fields?: Field[]; packed?: [Field, number][] };
const HashInput = {
get empty() {
return {};
},
append(input1: HashInput, input2: HashInput) {
if (input2.fields !== undefined) {
(input1.fields ??= []).push(...input2.fields);
}
if (input2.packed !== undefined) {
(input1.packed ??= []).push(...input2.packed);
}
return input1;
},
};

/**
* @deprecated `CircuitValue` is deprecated in favor of {@link Struct}, which features a simpler API and better typing.
*/
Expand Down
40 changes: 13 additions & 27 deletions src/lib/hash.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProvableExtended } from './circuit_value.js';
import { HashInput, ProvableExtended, Struct } from './circuit_value.js';
import { Poseidon as Poseidon_, Field } from '../snarky.js';
import { inCheckedComputation } from './proof_system.js';

Expand Down Expand Up @@ -121,25 +121,10 @@ function packToFields({ fields = [], packed = [] }: HashInput) {
return fields.concat(packedBits);
}

type HashInput = { fields?: Field[]; packed?: [Field, number][] };
const HashInput = {
get empty() {
return {};
},
append(input1: HashInput, input2: HashInput) {
if (input2.fields !== undefined) {
(input1.fields ??= []).push(...input2.fields);
}
if (input2.packed !== undefined) {
(input1.packed ??= []).push(...input2.packed);
}
return input1;
},
};

type TokenSymbol = { symbol: string; field: Field };

const TokenSymbolPure: ProvableExtended<TokenSymbol, string> = {
const TokenSymbolPure: ProvableExtended<
{ symbol: string; field: Field },
string
> = {
toFields({ field }) {
return [field];
},
Expand All @@ -163,17 +148,18 @@ const TokenSymbolPure: ProvableExtended<TokenSymbol, string> = {
return { packed: [[field, 48]] };
},
};
const TokenSymbol = {
...TokenSymbolPure,
get empty() {
class TokenSymbol extends Struct(TokenSymbolPure) {
static get empty() {
return { symbol: '', field: Field.zero };
},
}

from(symbol: string): TokenSymbol {
static from(symbol: string): TokenSymbol {
if (symbol.length > 6)
throw Error('Token symbol length should be a maximum of 6');
let field = prefixToField(symbol);
return { symbol, field };
},
};
}
}

function emptyReceiptChainHash() {
return emptyHashWithPrefix('CodaReceiptEmpty');
Expand Down
2 changes: 1 addition & 1 deletion src/lib/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
Ledger,
} from 'snarkyjs';

const tokenSymbol = 'MY_TOKEN';
const tokenSymbol = 'TOKEN';

class TokenContract extends SmartContract {
@state(UInt64) totalAmountInCirculation = State<UInt64>();
Expand Down