@cryptoeconomicslab/merkle-tree
Advanced tools
Comparing version 0.0.39 to 0.0.40
@@ -1,2 +0,2 @@ | ||
import { Bytes, Codable } from '@cryptoeconomicslab/primitives'; | ||
import { FixedBytes, Codable } from '@cryptoeconomicslab/primitives'; | ||
import { Hash } from '@cryptoeconomicslab/hash'; | ||
@@ -10,4 +10,4 @@ import { MerkleTreeInterface, MerkleTreeNode, InclusionProof } from './MerkleTreeInterface'; | ||
private calculateRoot; | ||
getRoot(): Bytes; | ||
findIndex(leaf: Bytes): number | null; | ||
getRoot(): FixedBytes; | ||
findIndex(leaf: FixedBytes): number | null; | ||
getLeaf(index: number): T; | ||
@@ -21,5 +21,5 @@ getInclusionProof(index: number): InclusionProof<B, T>; | ||
constructor(hashAlgorythm?: Hash); | ||
verifyInclusion(leaf: T, intervalStart: B, intervalEnd: B, root: Bytes, inclusionProof: InclusionProof<B, T>): boolean; | ||
verifyInclusion(leaf: T, intervalStart: B, intervalEnd: B, root: FixedBytes, inclusionProof: InclusionProof<B, T>): boolean; | ||
computeRootFromInclusionProof(leaf: T, merklePath: string, proofElement: T[]): { | ||
root: Bytes; | ||
root: FixedBytes; | ||
implicitEnd: B; | ||
@@ -26,0 +26,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
import { Bytes, Address, Struct } from '@cryptoeconomicslab/primitives'; | ||
import { Bytes, FixedBytes, Address, Struct } from '@cryptoeconomicslab/primitives'; | ||
import { AbstractMerkleTree, AbstractMerkleVerifier } from './AbstractMerkleTree'; | ||
@@ -6,7 +6,10 @@ import { MerkleTreeNode, InclusionProof } from './MerkleTreeInterface'; | ||
address: Address; | ||
data: Bytes; | ||
constructor(address: Address, data: Bytes); | ||
data: FixedBytes; | ||
constructor(address: Address, data: FixedBytes); | ||
getInterval(): Address; | ||
static decode(b: Bytes): AddressTreeNode; | ||
encode(): Bytes; | ||
getInterval(): Address; | ||
static getParamType(): Struct; | ||
static fromStruct(struct: Struct): AddressTreeNode; | ||
toStruct(): Struct; | ||
} | ||
@@ -13,0 +16,0 @@ export declare class AddressTreeInclusionProof extends InclusionProof<Address, AddressTreeNode> { |
@@ -10,15 +10,37 @@ "use strict"; | ||
this.data = data; | ||
if (data.data.length !== 32) | ||
if (data.size !== 32) | ||
throw new Error('data length is not 32 bytes.'); | ||
} | ||
getInterval() { | ||
return this.address; | ||
} | ||
static decode(b) { | ||
const d = b.split(32); | ||
return new AddressTreeNode(primitives_1.Address.from(d[1].toHexString()), d[0]); | ||
return AddressTreeNode.fromStruct(ovmContext.coder.decode(AddressTreeNode.getParamType(), b)); | ||
} | ||
encode() { | ||
return primitives_1.Bytes.concat([this.data, primitives_1.Bytes.fromHexString(this.address.data)]); | ||
return ovmContext.coder.encode(this.toStruct()); | ||
} | ||
getInterval() { | ||
return this.address; | ||
static getParamType() { | ||
return new primitives_1.Struct([ | ||
{ key: 'data', value: primitives_1.FixedBytes.default(32) }, | ||
{ key: 'address', value: primitives_1.Address.default() } | ||
]); | ||
} | ||
static fromStruct(struct) { | ||
const data = struct.data[0].value; | ||
const address = struct.data[1].value; | ||
return new AddressTreeNode(address, data); | ||
} | ||
toStruct() { | ||
return new primitives_1.Struct([ | ||
{ | ||
key: 'data', | ||
value: this.data | ||
}, | ||
{ | ||
key: 'address', | ||
value: this.address | ||
} | ||
]); | ||
} | ||
} | ||
@@ -61,6 +83,9 @@ exports.AddressTreeNode = AddressTreeNode; | ||
computeParent(a, b) { | ||
return new AddressTreeNode(a.address, this.hashAlgorythm.hash(primitives_1.Bytes.concat(a.encode(), b.encode()))); | ||
if (a.address.data > b.address.data) { | ||
throw new Error('right.address is greater than left.address'); | ||
} | ||
return new AddressTreeNode(a.address, primitives_1.FixedBytes.from(32, this.hashAlgorythm.hash(primitives_1.Bytes.concat(a.encode(), b.encode())).data)); | ||
} | ||
createEmptyNode() { | ||
return new AddressTreeNode(primitives_1.Address.default(), primitives_1.Bytes.default()); | ||
return new AddressTreeNode(primitives_1.Address.default(), primitives_1.FixedBytes.default(32)); | ||
} | ||
@@ -67,0 +92,0 @@ compare(a, b) { |
@@ -1,2 +0,2 @@ | ||
import { Bytes, Address, BigNumber, Range, Struct } from '@cryptoeconomicslab/primitives'; | ||
import { Bytes, Address, BigNumber, Range, Struct, FixedBytes } from '@cryptoeconomicslab/primitives'; | ||
import { MerkleTreeInterface, MerkleTreeGenerator, MerkleTreeNode } from './MerkleTreeInterface'; | ||
@@ -21,6 +21,6 @@ import { AddressTree, AddressTreeInclusionProof } from './AddressTree'; | ||
start: BigNumber; | ||
data: Bytes; | ||
constructor(address: Address, start: BigNumber, data: Bytes); | ||
data: FixedBytes; | ||
constructor(address: Address, start: BigNumber, data: FixedBytes); | ||
encode(): Bytes; | ||
getData(): Bytes; | ||
getData(): FixedBytes; | ||
getInterval(): { | ||
@@ -30,2 +30,3 @@ address: Address; | ||
}; | ||
toStruct(): Struct; | ||
} | ||
@@ -40,4 +41,4 @@ export declare class DoubleLayerTreeGenerator implements MerkleTreeGenerator<DoubleLayerInterval, DoubleLayerTreeLeaf> { | ||
constructor(leaves: DoubleLayerTreeLeaf[]); | ||
getRoot(): Bytes; | ||
findIndex(leaf: Bytes): number | null; | ||
getRoot(): FixedBytes; | ||
findIndex(leaf: FixedBytes): number | null; | ||
getLeaf(index: number): DoubleLayerTreeLeaf; | ||
@@ -48,5 +49,5 @@ getLeaves(address: Address, start: JSBI, end: JSBI): number[]; | ||
export interface DoubleLayerTreeVerifier { | ||
verifyInclusion(leaf: DoubleLayerTreeLeaf, range: Range, root: Bytes, inclusionProof: DoubleLayerInclusionProof): boolean; | ||
verifyInclusion(leaf: DoubleLayerTreeLeaf, range: Range, root: FixedBytes, inclusionProof: DoubleLayerInclusionProof): boolean; | ||
} | ||
export declare class DoubleLayerTreeVerifier implements DoubleLayerTreeVerifier { | ||
} |
@@ -49,3 +49,3 @@ "use strict"; | ||
encode() { | ||
return primitives_1.Bytes.concat([this.data, primitives_1.Bytes.fromHexString(this.address.data)]); | ||
return ovmContext.coder.encode(this.toStruct()); | ||
} | ||
@@ -61,2 +61,14 @@ getData() { | ||
} | ||
toStruct() { | ||
return new primitives_1.Struct([ | ||
{ | ||
key: 'data', | ||
value: this.data | ||
}, | ||
{ | ||
key: 'address', | ||
value: this.address | ||
} | ||
]); | ||
} | ||
} | ||
@@ -77,2 +89,17 @@ exports.DoubleLayerTreeLeaf = DoubleLayerTreeLeaf; | ||
this.intervalTreeMap = new Map(); | ||
const sorted = leaves.reduce((sorted, current, currentIndex, array) => { | ||
if (!sorted) | ||
return false; | ||
if (currentIndex === array.length - 1) | ||
return sorted; | ||
const next = array[currentIndex + 1]; | ||
return ((current.address.data === next.address.data && | ||
jsbi_1.default.lessThan(current.start.data, next.start.data)) || | ||
current.address.data < next.address.data); | ||
}, true); | ||
if (!sorted) | ||
throw new Error('Invalid ordered leaves'); | ||
const addresses = leaves | ||
.map(leaf => leaf.address.data) | ||
.filter((v, i, a) => a.indexOf(v) === i); | ||
const addressTreeLeaves = []; | ||
@@ -88,3 +115,4 @@ const addressLeavesMap = leaves.reduce((newMap, l) => { | ||
}, new Map()); | ||
for (const [key, value] of addressLeavesMap.entries()) { | ||
for (const key of addresses) { | ||
const value = addressLeavesMap.get(key); | ||
const intervalTree = new IntervalTree_1.IntervalTree(value); | ||
@@ -91,0 +119,0 @@ this.intervalTreeMap.set(key, intervalTree); |
@@ -1,2 +0,2 @@ | ||
import { Bytes, BigNumber, Struct } from '@cryptoeconomicslab/primitives'; | ||
import { FixedBytes, Bytes, BigNumber, Struct } from '@cryptoeconomicslab/primitives'; | ||
import { AbstractMerkleTree, AbstractMerkleVerifier } from './AbstractMerkleTree'; | ||
@@ -7,7 +7,10 @@ import { MerkleTreeNode, InclusionProof } from './MerkleTreeInterface'; | ||
start: BigNumber; | ||
data: Bytes; | ||
constructor(start: BigNumber, data: Bytes); | ||
data: FixedBytes; | ||
constructor(start: BigNumber, data: FixedBytes); | ||
getInterval(): BigNumber; | ||
static decode(b: Bytes): IntervalTreeNode; | ||
encode(): Bytes; | ||
static getParamType(): Struct; | ||
static fromStruct(struct: Struct): IntervalTreeNode; | ||
toStruct(): Struct; | ||
} | ||
@@ -14,0 +17,0 @@ export declare class IntervalTreeInclusionProof extends InclusionProof<BigNumber, IntervalTreeNode> { |
@@ -13,3 +13,3 @@ "use strict"; | ||
this.data = data; | ||
if (data.data.length !== 32) | ||
if (data.size !== 32) | ||
throw new Error('data length is not 32 bytes.'); | ||
@@ -21,11 +21,30 @@ } | ||
static decode(b) { | ||
const d = b.split(32); | ||
return new IntervalTreeNode(primitives_1.BigNumber.fromHexString(d[1].toHexString()), d[0]); | ||
return IntervalTreeNode.fromStruct(ovmContext.coder.decode(IntervalTreeNode.getParamType(), b)); | ||
} | ||
encode() { | ||
return primitives_1.Bytes.concat([ | ||
this.data, | ||
primitives_1.Bytes.fromHexString(this.start.data.toString(16)).padZero(32) | ||
return ovmContext.coder.encode(this.toStruct()); | ||
} | ||
static getParamType() { | ||
return new primitives_1.Struct([ | ||
{ key: 'data', value: primitives_1.FixedBytes.default(32) }, | ||
{ key: 'start', value: primitives_1.BigNumber.default() } | ||
]); | ||
} | ||
static fromStruct(struct) { | ||
const data = struct.data[0].value; | ||
const start = struct.data[1].value; | ||
return new IntervalTreeNode(start, data); | ||
} | ||
toStruct() { | ||
return new primitives_1.Struct([ | ||
{ | ||
key: 'data', | ||
value: this.data | ||
}, | ||
{ | ||
key: 'start', | ||
value: this.start | ||
} | ||
]); | ||
} | ||
} | ||
@@ -82,6 +101,6 @@ exports.IntervalTreeNode = IntervalTreeNode; | ||
} | ||
return new IntervalTreeNode(b.start, this.hashAlgorythm.hash(primitives_1.Bytes.concat([a.encode(), b.encode()]))); | ||
return new IntervalTreeNode(b.start, primitives_1.FixedBytes.from(32, this.hashAlgorythm.hash(primitives_1.Bytes.concat([a.encode(), b.encode()])).data)); | ||
} | ||
createEmptyNode() { | ||
return new IntervalTreeNode(primitives_1.BigNumber.MAX_NUMBER, this.hashAlgorythm.hash(primitives_1.Bytes.default())); | ||
return new IntervalTreeNode(primitives_1.BigNumber.MAX_NUMBER, primitives_1.FixedBytes.from(32, this.hashAlgorythm.hash(primitives_1.Bytes.default()).data)); | ||
} | ||
@@ -88,0 +107,0 @@ compare(a, b) { |
@@ -1,4 +0,4 @@ | ||
import { Bytes, Struct, Codable } from '@cryptoeconomicslab/primitives'; | ||
import { Bytes, Struct, Codable, FixedBytes } from '@cryptoeconomicslab/primitives'; | ||
export interface MerkleTreeNode<T> { | ||
readonly data: Bytes; | ||
readonly data: FixedBytes; | ||
getInterval(): T; | ||
@@ -11,8 +11,8 @@ encode(): Bytes; | ||
export interface MerkleTreeInterface<I, T extends MerkleTreeNode<I>> { | ||
getRoot(): Bytes; | ||
findIndex(leaf: Bytes): number | null; | ||
getRoot(): FixedBytes; | ||
findIndex(leaf: FixedBytes): number | null; | ||
getLeaf(index: number): T; | ||
} | ||
export interface MerkleTreeVerifier<B, T extends MerkleTreeNode<I>, I> { | ||
verifyInclusion(leaf: T, interval: B, root: Bytes, inclusionProof: I): boolean; | ||
verifyInclusion(leaf: T, interval: B, root: FixedBytes, inclusionProof: I): boolean; | ||
} | ||
@@ -19,0 +19,0 @@ export declare class InclusionProof<I extends Codable, T extends MerkleTreeNode<I>> { |
{ | ||
"name": "@cryptoeconomicslab/merkle-tree", | ||
"version": "0.0.39", | ||
"version": "0.0.40", | ||
"description": "merkle tree implementation", | ||
@@ -32,9 +32,11 @@ "author": { | ||
"dependencies": { | ||
"@cryptoeconomicslab/hash": "^0.0.39", | ||
"@cryptoeconomicslab/utils": "^0.0.39", | ||
"@cryptoeconomicslab/hash": "^0.0.40", | ||
"@cryptoeconomicslab/utils": "^0.0.40", | ||
"jsbi": "^3.1.2" | ||
}, | ||
"devDependencies": { | ||
"@cryptoeconomicslab/coder": "^0.0.39", | ||
"@cryptoeconomicslab/primitives": "^0.0.39" | ||
"@cryptoeconomicslab/coder": "^0.0.40", | ||
"@cryptoeconomicslab/context": "^0.0.40", | ||
"@cryptoeconomicslab/eth-coder": "^0.0.40", | ||
"@cryptoeconomicslab/primitives": "^0.0.40" | ||
}, | ||
@@ -44,3 +46,3 @@ "peerDependencies": { | ||
}, | ||
"gitHead": "d1a8f15e7edae904aac7603fa988035825a367dd" | ||
"gitHead": "f0eb6f958451991a243c76671b56ee55adadabf7" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
58295
683
4
+ Added@cryptoeconomicslab/hash@0.0.40(transitive)
+ Added@cryptoeconomicslab/utils@0.0.40(transitive)
- Removed@cryptoeconomicslab/hash@0.0.39(transitive)
- Removed@cryptoeconomicslab/utils@0.0.39(transitive)