LocalState
@midnight/zswap v3.0.2 β’ Readme | API
@midnight/zswap v3.0.2 / LocalState
Class: LocalState
The local state of a user/wallet, consisting of their secret key, and a set of unspent coins.
It also keeps track of coins that are in-flight, either expecting to spend or expecting to receive, and a local copy of the global coin commitment Merkle tree to generate proofs against.
Constructorsβ
new LocalState()β
new LocalState(): LocalState
Creates a new state with a randomly sampled secret key
Returnsβ
Propertiesβ
coinPublicKeyβ
readonly coinPublicKey: string;
The coin public key of this wallet
coinsβ
readonly coins: Set<QualifiedCoinInfo>;
The set of spendable coins of this wallet
encryptionPublicKeyβ
readonly encryptionPublicKey: string;
The encryption public key of this wallet
firstFreeβ
readonly firstFree: bigint;
The first free index in the internal coin commitments Merkle tree. This may be used to identify which merkle tree updates are necessary.
pendingOutputsβ
readonly pendingOutputs: Map<string, CoinInfo>;
The outputs that this wallet is expecting to receive in the future
pendingSpendsβ
readonly pendingSpends: Map<string, QualifiedCoinInfo>;
The spends that this wallet is expecting to be finalized on-chain in the future
Methodsβ
apply()β
apply(offer): LocalState
Locally applies an offer to the current state, returning the updated state
Parametersβ
β’ offer: Offer
Returnsβ
applyCollapsedUpdate()β
applyCollapsedUpdate(update): LocalState
Applies a collapsed Merkle tree update to the current local state, fast forwarding through the indices included in it, if it is a correct update.
The general flow for usage if Alice is in state A, and wants to ask Bob how to reach the new state B, is:
- Find where she left off β what's her firstFree?
- Find out where she's going β ask for Bob's firstFree.
- Find what contents she does care about β ask Bob for the filtered entries she want to include proper in her tree.
- In order, of Merkle tree indicies:
- Insert (with
applyoffers Alice cares about). - Skip (with this method) sections Alice does not care about, obtaining
the collapsed update covering the gap from Bob.
Note that
firstFreeis not included in the tree itself, and both ends of updates are included.
- Insert (with
Parametersβ
β’ update: MerkleTreeCollapsedUpdate
Returnsβ
applyFailed()β
applyFailed(offer): LocalState
Locally marks an offer as failed, allowing inputs used in it to be spendable once more.
Parametersβ
β’ offer: Offer
Returnsβ
applyFailedProofErased()β
applyFailedProofErased(offer): LocalState
Locally marks an proof-erased offer as failed, allowing inputs used in it to be spendable once more.
Parametersβ
β’ offer: ProofErasedOffer
Returnsβ
applyProofErased()β
applyProofErased(offer): LocalState
Locally applies a proof-erased offer to the current state, returning the updated state
Parametersβ
β’ offer: ProofErasedOffer
Returnsβ
applyProofErasedTx()β
applyProofErasedTx(tx, res): LocalState
Locally applies a proof-erased transaction to the current state, returning the updated state
Parametersβ
β’ tx: ProofErasedTransaction
β’ res: "success" | "partialSuccess" | "failure"
The result type of applying this transaction against the ledger state
Returnsβ
applySystemTx()β
applySystemTx(tx): LocalState
Locally applies a system transaction to the current state, returning the updated state
Parametersβ
β’ tx: SystemTransaction
Returnsβ
applyTx()β
applyTx(tx, res): LocalState
Locally applies a transaction to the current state, returning the updated state
Parametersβ
β’ tx: Transaction
β’ res: "success" | "partialSuccess" | "failure"
The result type of applying this transaction against the ledger state
Returnsβ
serialize()β
serialize(netid): Uint8Array
Parametersβ
β’ netid: NetworkId
Returnsβ
Uint8Array
spend()β
spend(coin): [LocalState, UnprovenInput]
Initiates a new spend of a specific coin, outputting the corresponding UnprovenInput, and the updated state marking this coin as in-flight.
Parametersβ
β’ coin: QualifiedCoinInfo
Returnsβ
spendFromOutput()β
spendFromOutput(coin, output): [LocalState, UnprovenTransient]
Initiates a new spend of a new-yet-received output, outputting the corresponding UnprovenTransient, and the updated state marking this coin as in-flight.
Parametersβ
β’ coin: QualifiedCoinInfo
β’ output: UnprovenOutput
Returnsβ
[LocalState, UnprovenTransient]
toString()β
toString(compact?): string
Parametersβ
β’ compact?: boolean
Returnsβ
string
watchFor()β
watchFor(coin): LocalState
Adds a coin to the list of coins that are expected to be received
This should be used if an output is creating a coin for this wallet, which does not contain a ciphertext to detect it. In this case, the wallet must know the commitment ahead of time to notice the receipt.
Parametersβ
β’ coin: CoinInfo
Returnsβ
yesIKnowTheSecurityImplicationsOfThis_encryptionSecretKey()β
yesIKnowTheSecurityImplicationsOfThis_encryptionSecretKey(): EncryptionSecretKey
Returnsβ
deserialize()β
static deserialize(raw, netid): LocalState
Parametersβ
β’ raw: Uint8Array
β’ netid: NetworkId
Returnsβ
fromSeed()β
static fromSeed(seed): LocalState
Creates a new state from a predefined random seed (which can act as a recovery phrase)
Parametersβ
β’ seed: Uint8Array