1
1
import { State , StateMachine } from "@stackr/sdk/machine" ;
2
- import { merklize } from "@stackr/sdk " ;
3
- import { solidityPacked } from "ethers " ;
2
+ import { keccak256 , solidityPackedKeccak256 , ZeroHash } from "ethers " ;
3
+ import { MerkleTree } from "merkletreejs " ;
4
4
import genesisState from "../genesis-state.json" ;
5
5
import { transitions } from "./transitions" ;
6
6
@@ -25,11 +25,14 @@ export class AppState extends State<RawState, WrappedState> {
25
25
transformer ( ) {
26
26
return {
27
27
wrap : ( ) => {
28
- const games = this . state . games . reduce < WrappedState [ 'games' ] > ( ( acc , game ) => {
29
- const { id, ...rest } = game ;
30
- acc [ id ] = { ...rest } ;
31
- return acc ;
32
- } , { } ) ;
28
+ const games = this . state . games . reduce < WrappedState [ "games" ] > (
29
+ ( acc , game ) => {
30
+ const { id, ...rest } = game ;
31
+ acc [ id ] = { ...rest } ;
32
+ return acc ;
33
+ } ,
34
+ { }
35
+ ) ;
33
36
return { games } ;
34
37
} ,
35
38
unwrap : ( wrappedState : WrappedState ) => {
@@ -44,11 +47,17 @@ export class AppState extends State<RawState, WrappedState> {
44
47
}
45
48
46
49
getRootHash ( ) : string {
47
- const leaves = this . state . games . map (
48
- ( { id, player, score } ) =>
49
- solidityPacked ( [ "string" , "address" , "uint256" ] , [ id , player , score ] )
50
+ const leaves = this . state . games . map ( ( { id, player, score } ) =>
51
+ solidityPackedKeccak256 (
52
+ [ "string" , "address" , "uint256" ] ,
53
+ [ id , player , score ]
54
+ )
50
55
) ;
51
- return merklize ( leaves ) ;
56
+ if ( leaves . length === 0 ) {
57
+ return ZeroHash ;
58
+ }
59
+ const tree = new MerkleTree ( leaves , keccak256 ) ;
60
+ return tree . getHexRoot ( ) ;
52
61
}
53
62
}
54
63
0 commit comments