MerkleTree compatible with version from [circomlib](https://github.com/iden3/circomlib).
MerkleTree compatible with version from [circomlib](https://github.com/iden3/circomlib).
Adaptation of the merkletree from https://github.com/iden3/go-iden3-core/tree/v0.0.8
Adaptation of the merkletree from https://github.com/iden3/go-iden3-core/tree/v0.0.8 with several changes and more functionalities.
## Usage
## Usage
More detailed examples can be found at the [tests](https://github.com/iden3/go-merkletree/blob/master/merkletree_test.go), and in the [documentation](https://godoc.org/github.com/iden3/go-merkletree).
More detailed examples can be found at the [tests](https://github.com/iden3/go-merkletree/blob/master/merkletree_test.go), and in the [documentation](https://godoc.org/github.com/iden3/go-merkletree).
// Delete removes the specified Key from the MerkleTree, and updates the pad from the delted key to the Root with the new values.
// This method removes the key from the MerkleTree, but does not remove the old nodes from the key-value database; this means that if the tree is accessed by an old Root where the key was not deleted yet, the key will still exist. If is desired to remove the key-values from the database that are not under the current Root, an option could be to dump all the claims and import them in a new MerkleTree in a new database, but this will loose all the Root history of the MerkleTree
func(mt*MerkleTree)Delete(k*big.Int)error{
// verify that the MerkleTree is writable
if!mt.writable{
returnErrNotWritable
}
// verfy that the ElemBytes are valid and fit inside the Finite Field.
if!cryptoUtils.CheckBigIntInField(k){
returnerrors.New("Key not inside the Finite Field")
}
tx,err:=mt.db.NewTx()
iferr!=nil{
returnerr
}
mt.Lock()
defermt.Unlock()
kHash:=NewHashFromBigInt(k)
path:=getPath(mt.maxLevels,kHash[:])
nextKey:=mt.rootKey
varsiblings[]*Hash
fori:=0;i<mt.maxLevels;i++{
n,err:=mt.GetNode(nextKey)
iferr!=nil{
returnerr
}
switchn.Type{
caseNodeTypeEmpty:
returnnil
caseNodeTypeLeaf:
ifbytes.Equal(kHash[:],n.Entry[0][:]){
// remove and go up with the sibling
err=mt.rmAndUpload(tx,path,kHash,siblings)
returnerr
}else{
returnErrKeyNotFound
}
caseNodeTypeMiddle:
ifpath[i]{
nextKey=n.ChildR
siblings=append(siblings,n.ChildL)
}else{
nextKey=n.ChildL
siblings=append(siblings,n.ChildR)
}
default:
returnErrInvalidNodeFound
}
}
returnnil
}
// rmAndUpload removes the key, and goes up until the root updating all the nodes with the new values.