|
| 1 | +import { beginCell } from '../boc/Builder'; |
| 2 | +import { Cell } from '../boc/Cell'; |
| 3 | +import { Slice } from '../boc/Slice'; |
| 4 | +import { DictionaryKeyTypes, Dictionary, DictionaryKey } from './Dictionary'; |
| 5 | +import { readUnaryLength } from './utils/readUnaryLength'; |
| 6 | + |
| 7 | +function convertToPrunedBranch(c: Cell): Cell { |
| 8 | + return new Cell({ |
| 9 | + exotic: true, |
| 10 | + bits: beginCell() |
| 11 | + .storeUint(1, 8) |
| 12 | + .storeUint(1, 8) |
| 13 | + .storeBuffer(c.hash(0)) |
| 14 | + .storeUint(c.depth(0), 16) |
| 15 | + .endCell() |
| 16 | + .beginParse() |
| 17 | + .loadBits(288), |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +function convertToMerkleProof(c: Cell): Cell { |
| 22 | + return new Cell({ |
| 23 | + exotic: true, |
| 24 | + bits: beginCell() |
| 25 | + .storeUint(3, 8) |
| 26 | + .storeBuffer(c.hash(0)) |
| 27 | + .storeUint(c.depth(0), 16) |
| 28 | + .endCell() |
| 29 | + .beginParse() |
| 30 | + .loadBits(280), |
| 31 | + refs: [c], |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +function doGenerateMerkleProof( |
| 36 | + prefix: string, |
| 37 | + slice: Slice, |
| 38 | + n: number, |
| 39 | + key: string |
| 40 | +): Cell { |
| 41 | + // Reading label |
| 42 | + const originalCell = slice.asCell(); |
| 43 | + |
| 44 | + let lb0 = slice.loadBit() ? 1 : 0; |
| 45 | + let prefixLength = 0; |
| 46 | + let pp = prefix; |
| 47 | + |
| 48 | + if (lb0 === 0) { |
| 49 | + // Short label detected |
| 50 | + |
| 51 | + // Read |
| 52 | + prefixLength = readUnaryLength(slice); |
| 53 | + |
| 54 | + // Read prefix |
| 55 | + for (let i = 0; i < prefixLength; i++) { |
| 56 | + pp += slice.loadBit() ? '1' : '0'; |
| 57 | + } |
| 58 | + } else { |
| 59 | + let lb1 = slice.loadBit() ? 1 : 0; |
| 60 | + if (lb1 === 0) { |
| 61 | + // Long label detected |
| 62 | + prefixLength = slice.loadUint(Math.ceil(Math.log2(n + 1))); |
| 63 | + for (let i = 0; i < prefixLength; i++) { |
| 64 | + pp += slice.loadBit() ? '1' : '0'; |
| 65 | + } |
| 66 | + } else { |
| 67 | + // Same label detected |
| 68 | + let bit = slice.loadBit() ? '1' : '0'; |
| 69 | + prefixLength = slice.loadUint(Math.ceil(Math.log2(n + 1))); |
| 70 | + for (let i = 0; i < prefixLength; i++) { |
| 71 | + pp += bit; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (n - prefixLength === 0) { |
| 77 | + return originalCell; |
| 78 | + } else { |
| 79 | + let sl = originalCell.beginParse(); |
| 80 | + let left = sl.loadRef(); |
| 81 | + let right = sl.loadRef(); |
| 82 | + // NOTE: Left and right branches are implicitly contain prefixes '0' and '1' |
| 83 | + if (!left.isExotic) { |
| 84 | + if (pp + '0' === key.slice(0, pp.length + 1)) { |
| 85 | + left = doGenerateMerkleProof( |
| 86 | + pp + '0', |
| 87 | + left.beginParse(), |
| 88 | + n - prefixLength - 1, |
| 89 | + key |
| 90 | + ); |
| 91 | + } else { |
| 92 | + left = convertToPrunedBranch(left); |
| 93 | + } |
| 94 | + } |
| 95 | + if (!right.isExotic) { |
| 96 | + if (pp + '1' === key.slice(0, pp.length + 1)) { |
| 97 | + right = doGenerateMerkleProof( |
| 98 | + pp + '1', |
| 99 | + right.beginParse(), |
| 100 | + n - prefixLength - 1, |
| 101 | + key |
| 102 | + ); |
| 103 | + } else { |
| 104 | + right = convertToPrunedBranch(right); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return beginCell() |
| 109 | + .storeSlice(sl) |
| 110 | + .storeRef(left) |
| 111 | + .storeRef(right) |
| 112 | + .endCell(); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +export function generateMerkleProof<K extends DictionaryKeyTypes, V>( |
| 117 | + dict: Dictionary<K, V>, |
| 118 | + key: K, |
| 119 | + keyObject: DictionaryKey<K> |
| 120 | +): Cell { |
| 121 | + const s = beginCell().storeDictDirect(dict).endCell().beginParse(); |
| 122 | + return convertToMerkleProof( |
| 123 | + doGenerateMerkleProof( |
| 124 | + '', |
| 125 | + s, |
| 126 | + keyObject.bits, |
| 127 | + keyObject.serialize(key).toString(2).padStart(keyObject.bits, '0') |
| 128 | + ) |
| 129 | + ); |
| 130 | +} |
0 commit comments