SnarkyJS Reference
Table of contents
References
Namespaces
Classes
- AccountUpdate
- Bool
- Character
- Circuit
- CircuitString
- CircuitValue
- Field
- Group
- Int64
- Keypair
- Ledger
- PrivateKey
- Proof
- Scalar
- SelfProof
- Sign
- Signature
- SmartContract
- Token
- UInt32
- UInt64
- VerificationKey
Interfaces
Type Aliases
Variables
Functions
- Account
- State
- addCachedAccount
- arrayProp
- circuitMain
- circuitValue
- declareMethods
- declareState
- deploy
- fetchAccount
- fetchLastBlock
- getSrs
- matrixProp
- method
- prop
- public_
- recoverVerificationKey
- sendZkapp
- serializeVerificationKey
- setGraphqlEndpoint
- shutdown
- signFeePayer
- state
- verify
- zkappCommandToJson
References
PublicKey
Re-exports PublicKey
Type Aliases
DeployArgs
Ƭ DeployArgs: { verificationKey?
: { data
: string
; hash
: string
| Field
} ; zkappKey?
: PrivateKey
} | undefined
Defined in
State
Ƭ State<A
>: Object
Gettable and settable state that can be checked for equality.
Type parameters
Name |
---|
A |
Type declaration
Name | Type |
---|---|
assertEquals | (a : A ) => void |
assertNothing | () => void |
fetch | () => Promise <undefined | A > |
get | () => A |
set | (a : A ) => void |
Defined in
TokenSymbol
Ƭ TokenSymbol: Object
Type declaration
Name | Type |
---|---|
field | Field |
symbol | string |
Defined in
ZkappPublicInput
Ƭ ZkappPublicInput: Object
The public input for zkApps consists of certain hashes of the proving AccountUpdate (and its child accountUpdates) which is constructed during method execution.
For SmartContract proving, a method is run twice: First outside the proof, to obtain the public input, and once in the prover, which takes the public input as input. The current transaction is hashed again inside the prover, which asserts that the result equals the input public input, as part of the snark circuit. The block producer will also hash the transaction they receive and pass it as a public input to the verifier. Thus, the transaction is fully constrained by the proof - the proof couldn't be used to attest to a different transaction.
Type declaration
Name | Type |
---|---|
accountUpdate | Field |
calls | Field |
Defined in
Variables
Permissions
• Permissions: Object
Type declaration
Name | Type |
---|---|
default | () => Permissions |
impossible | () => AuthRequired |
initial | () => Permissions |
none | () => AuthRequired |
proof | () => AuthRequired |
proofOrSignature | () => AuthRequired |
signature | () => AuthRequired |
Defined in
Poseidon
• Const
Poseidon: Object
Type declaration
Name | Type |
---|---|
Sponge | typeof Sponge |
get initialState() | [Field , Field , Field ] |
hash | (input : Field []) => Field |
update | (state : [Field , Field , Field ], input : Field []) => [Field , Field , Field ] |
Defined in
TokenSymbol
• TokenSymbol: Object
Type declaration
Name | Type |
---|---|
empty | { field : Field = Field.zero; symbol : string = '' } |
empty.field | Field |
empty.symbol | string |
check | (value : TokenSymbol ) => void |
from | (symbol : string ) => TokenSymbol |
fromFields | (fields : Field [], aux : any []) => TokenSymbol |
sizeInFields | () => number |
toAuxiliary | (value? : TokenSymbol ) => any [] |
toFields | (value : TokenSymbol ) => Field [] |
toInput | (value : TokenSymbol ) => HashInput |
toJSON | (value : TokenSymbol ) => string |
Defined in
ZkappPublicInput
• ZkappPublicInput: AsFieldsExtended
<ZkappPublicInput
>
Defined in
isReady
• isReady: Promise
<undefined
>
A Promise that resolves when SnarkyJS is ready to be used
Defined in
Functions
Account
▸ Account(address
, tokenId?
): PreconditionClassType
<AccountPrecondition
>
Parameters
Name | Type |
---|---|
address | PublicKey |
tokenId? | Field |
Returns
PreconditionClassType
<AccountPrecondition
>
Defined in
State
▸ State<A
>(): State
<A
>
Type parameters
Name |
---|
A |
Returns
State
<A
>
Defined in
addCachedAccount
▸ addCachedAccount(account
, graphqlEndpoint?
): void
Parameters
Name | Type | Default value |
---|---|---|
account | Object | undefined |
account.balance? | string | number | UInt64 | undefined |
account.nonce | string | number | UInt32 | undefined |
account.publicKey | string | PublicKey | undefined |
account.tokenId | string | undefined |
account.zkapp? | Object | undefined |
account.zkapp.appState | (string | number | Field )[] | undefined |
graphqlEndpoint | string | defaultGraphqlEndpoint |
Returns
void
Defined in
arrayProp
▸ arrayProp<T
>(elementType
, length
): (target
: any
, key
: string
) => void
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
elementType | AsFieldElements <T > |
length | number |
Returns
fn
▸ (target
, key
): void
Parameters
Name | Type |
---|---|
target | any |
key | string |
Returns
void
Defined in
circuitMain
▸ circuitMain(target
, propertyName
, _descriptor?
): any
Parameters
Name | Type |
---|---|
target | any |
propertyName | string |
_descriptor? | PropertyDescriptor |
Returns
any
Defined in
circuitValue
▸ circuitValue<T
>(typeObj
, options?
): AsFieldsExtended
<T
>
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
typeObj | any |
options? | Object |
options.customObjectKeys | string [] |
Returns
AsFieldsExtended
<T
>
Defined in
declareMethods
▸ declareMethods<T
>(SmartContract
, methodArguments
): void
declareMethods
can be used in place of the @method
decorator
to declare SmartContract methods along with their list of arguments.
It should be placed after the class declaration.
Here is an example of declaring a method update
, which takes a single argument of type Field
:
class MyContract extends SmartContract {
// ...
update(x: Field) {
// ...
}
}
declareMethods(MyContract, { update: [Field] }); // `[Field]` is the list of arguments!
Note that a method of the same name must still be defined on the class, just without the decorator.
Type parameters
Name | Type |
---|---|
T | extends typeof SmartContract |
Parameters
Name | Type |
---|---|
SmartContract | T |
methodArguments | Record <string , AsFieldElements <unknown >[]> |
Returns
void
Defined in
declareState
▸ declareState<T
>(SmartContract
, states
): void
declareState
can be used in place of the @state
decorator to declare on-chain state on a SmartContract.
It should be placed after the class declaration.
Here is an example of declaring a state property x
of type Field
.
class MyContract extends SmartContract {
x = State<Field>();
// ...
}
declareState(MyContract, { x: Field });
If you're using pure JS, it's not possible to use the built-in class field syntax, i.e. the following will not work:
// THIS IS WRONG IN JS!
class MyContract extends SmartContract {
x = State();
}
declareState(MyContract, { x: Field });
Instead, add a constructor where you assign the property:
class MyContract extends SmartContract {
constructor(x) {
super();
this.x = State();
}
}
declareState(MyContract, { x: Field });
Type parameters
Name | Type |
---|---|
T | extends typeof SmartContract |
Parameters
Name | Type |
---|---|
SmartContract | T |
states | Record <string , AsFieldElements <unknown >> |
Returns
void
Defined in
deploy
▸ deploy<S
>(SmartContract
, __namedParameters
): Promise
<string
>
Type parameters
Name | Type |
---|---|
S | extends typeof SmartContract |
Parameters
Name | Type |
---|---|
SmartContract | S |
__namedParameters | Object |
__namedParameters.feePayer? | FeePayerSpec |
__namedParameters.initialBalance? | string | number |
__namedParameters.tokenId? | Field |
__namedParameters.verificationKey | Object |
__namedParameters.verificationKey.data | string |
__namedParameters.verificationKey.hash | string | Field |
__namedParameters.zkappKey | PrivateKey |
Returns
Promise
<string
>
Defined in
fetchAccount
▸ fetchAccount(accountInfo
, graphqlEndpoint?
, config?
): Promise
<{ account
: Account
; error
: undefined
} | { account
: undefined
; error
: FetchError
}>
Gets account information on the specified publicKey by performing a GraphQL query to the specified endpoint. This will call the 'GetAccountInfo' query which fetches zkapp related account information.
If an error is returned by the specified endpoint, an error is thrown. Otherwise, the data is returned.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
accountInfo | Object | undefined | - |
accountInfo.publicKey | string | PublicKey | undefined | - |
accountInfo.tokenId? | string | undefined | - |
graphqlEndpoint | string | defaultGraphqlEndpoint | The graphql endpoint to fetch from |
config | Object | {} | An object that exposes an additional timeout option |
config.timeout | undefined | number | undefined | - |
Returns
Promise
<{ account
: Account
; error
: undefined
} | { account
: undefined
; error
: FetchError
}>
zkapp information on the specified account or an error is thrown
Defined in
fetchLastBlock
▸ fetchLastBlock(graphqlEndpoint?
): Promise
<PreconditionBaseTypes
<{ blockchainLength
: { isSome
: Bool
; value
: { lower
: UInt32
; upper
: UInt32
} } ; globalSlotSinceGenesis
: { isSome
: Bool
; value
: { lower
: UInt32
; upper
: UInt32
} } ; globalSlotSinceHardFork
: { isSome
: Bool
; value
: { lower
: UInt32
; upper
: UInt32
} } ; minWindowDensity
: { isSome
: Bool
; value
: { lower
: UInt32
; upper
: UInt32
} } ; nextEpochData
: { epochLength
: { isSome
: Bool
; value
: { lower
: UInt32
; upper
: UInt32
} } ; ledger
: { hash
: { isSome
: Bool
; value
: Field
} ; totalCurrency
: { isSome
: Bool
; value
: { lower
: UInt64
; upper
: UInt64
} } } ; lockCheckpoint
: { isSome
: Bool
; value
: Field
} ; seed
: { isSome
: Bool
; value
: Field
} ; startCheckpoint
: { isSome
: Bool
; value
: Field
} } ; snarkedLedgerHash
: { isSome
: Bool
; value
: Field
} ; stakingEpochData
: { epochLength
: { isSome
: Bool
; value
: { lower
: UInt32
; upper
: UInt32
} } ; ledger
: { hash
: { isSome
: Bool
; value
: Field
} ; totalCurrency
: { isSome
: Bool
; value
: { lower
: UInt64
; upper
: UInt64
} } } ; lockCheckpoint
: { isSome
: Bool
; value
: Field
} ; seed
: { isSome
: Bool
; value
: Field
} ; startCheckpoint
: { isSome
: Bool
; value
: Field
} } ; timestamp
: { isSome
: Bool
; value
: { lower
: UInt64
; upper
: UInt64
} } ; totalCurrency
: { isSome
: Bool
; value
: { lower
: UInt64
; upper
: UInt64
} } }>>
Parameters
Name | Type | Default value |
---|---|---|
graphqlEndpoint | string | defaultGraphqlEndpoint |
Returns
Promise
<PreconditionBaseTypes
<{ blockchainLength
: { isSome
: Bool
; value
: { lower
: UInt32
; upper
: UInt32
} } ; globalSlotSinceGenesis
: { isSome
: Bool
; value
: { lower
: UInt32
; upper
: UInt32
} } ; globalSlotSinceHardFork
: { isSome
: Bool
; value
: { lower
: UInt32
; upper
: UInt32
} } ; minWindowDensity
: { isSome
: Bool
; value
: { lower
: UInt32
; upper
: UInt32
} } ; nextEpochData
: { epochLength
: { isSome
: Bool
; value
: { lower
: UInt32
; upper
: UInt32
} } ; ledger
: { hash
: { isSome
: Bool
; value
: Field
} ; totalCurrency
: { isSome
: Bool
; value
: { lower
: UInt64
; upper
: UInt64
} } } ; lockCheckpoint
: { isSome
: Bool
; value
: Field
} ; seed
: { isSome
: Bool
; value
: Field
} ; startCheckpoint
: { isSome
: Bool
; value
: Field
} } ; snarkedLedgerHash
: { isSome
: Bool
; value
: Field
} ; stakingEpochData
: { epochLength
: { isSome
: Bool
; value
: { lower
: UInt32
; upper
: UInt32
} } ; ledger
: { hash
: { isSome
: Bool
; value
: Field
} ; totalCurrency
: { isSome
: Bool
; value
: { lower
: UInt64
; upper
: UInt64
} } } ; lockCheckpoint
: { isSome
: Bool
; value
: Field
} ; seed
: { isSome
: Bool
; value
: Field
} ; startCheckpoint
: { isSome
: Bool
; value
: Field
} } ; timestamp
: { isSome
: Bool
; value
: { lower
: UInt64
; upper
: UInt64
} } ; totalCurrency
: { isSome
: Bool
; value
: { lower
: UInt64
; upper
: UInt64
} } }>>
Defined in
getSrs
▸ getSrs(keypair
): any
Parameters
Name | Type | Description |
---|---|---|
keypair | any | SNARK keypair, as returned by Circuit.generateKeypair |
Returns
any
The SRS (structured reference string), needed to reconstruct the keypair later
Defined in
matrixProp
▸ matrixProp<T
>(elementType
, nRows
, nColumns
): (target
: any
, key
: string
) => void
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
elementType | AsFieldElements <T > |
nRows | number |
nColumns | number |
Returns
fn
▸ (target
, key
): void
Parameters
Name | Type |
---|---|
target | any |
key | string |
Returns
void
Defined in
method
▸ method<T
>(target
, methodName
, descriptor
): void
A decorator to use in a zkapp to mark a method as callable by anyone. You can use inside your zkapp class as:
@method myMethod(someArg: Field) {
// your code here
}
Type parameters
Name | Type |
---|---|
T | extends SmartContract <T > |
Parameters
Name | Type |
---|---|
target | T & { constructor : any } |
methodName | keyof T & string |
descriptor | PropertyDescriptor |
Returns
void
Defined in
prop
▸ prop(this
, target
, key
): void
Parameters
Name | Type |
---|---|
this | any |
target | any |
key | string |
Returns
void
Defined in
public_
▸ public_(target
, _key
, index
): void
Parameters
Name | Type |
---|---|
target | any |
_key | string | symbol |
index | number |
Returns
void
Defined in
recoverVerificationKey
▸ recoverVerificationKey(srs
, serializedVk
): any
Parameters
Name | Type | Description |
---|---|---|
srs | any | the "structured reference string", a set of precomputed values needed for verifying proofs |
serializedVk | string | string representation of a Circuit verification key |
Returns
any
the recovered verification key
Defined in
sendZkapp
▸ sendZkapp(json
, graphqlEndpoint?
, __namedParameters?
): Promise
<[FetchResponse
, undefined
] | [undefined
, FetchError
]>
Parameters
Name | Type | Default value |
---|---|---|
json | string | undefined |
graphqlEndpoint | string | defaultGraphqlEndpoint |
__namedParameters | Object | {} |
__namedParameters.timeout | undefined | number | undefined |
Returns
Promise
<[FetchResponse
, undefined
] | [undefined
, FetchError
]>
Defined in
serializeVerificationKey
▸ serializeVerificationKey(verificationKey
): string
Parameters
Name | Type | Description |
---|---|---|
verificationKey | any | the verification key of a Circuit |
Returns
string
string representation of the verification key
Defined in
setGraphqlEndpoint
▸ setGraphqlEndpoint(graphqlEndpoint
): void
Parameters
Name | Type |
---|---|
graphqlEndpoint | string |
Returns
void
Defined in
shutdown
▸ shutdown(): Promise
<undefined
>
This function must be called at the end of a nodejs program, otherwise the worker threads will continue running and the program will never terminate. From web applications, this function is a no-op.
Returns
Promise
<undefined
>
Defined in
signFeePayer
▸ signFeePayer(transactionJson
, feePayerKey
, __namedParameters
): string
Parameters
Name | Type |
---|---|
transactionJson | string |
feePayerKey | string | PrivateKey |
__namedParameters | Object |
Returns
string
Defined in
state
▸ state<A
>(stateType
): (target
: SmartContract
& { constructor
: any
}, key
: string
, _descriptor?
: PropertyDescriptor
) => void
A decorator to use within a zkapp to indicate what will be stored on-chain.
For example, if you want to store a field element some_state
in a zkapp,
you can use the following in the declaration of your zkapp:
@state(Field) some_state = State<Field>();
Type parameters
Name |
---|
A |
Parameters
Name | Type |
---|---|
stateType | AsFieldElements <A > |
Returns
fn
▸ (target
, key
, _descriptor?
): void
Parameters
Name | Type |
---|---|
target | SmartContract & { constructor : any } |
key | string |
_descriptor? | PropertyDescriptor |
Returns
void
Defined in
verify
▸ verify(proof
, verificationKey
): Promise
<boolean
>
Parameters
Name | Type |
---|---|
proof | JsonProof | Proof <any > |
verificationKey | string |
Returns
Promise
<boolean
>
Defined in
zkappCommandToJson
▸ zkappCommandToJson(__namedParameters
): ZkappCommand
Parameters
Name | Type |
---|---|
__namedParameters | ZkappCommand |