UNPKG

@ethereumjs/binarytree

Version:
38 lines 1.47 kB
import { RLP } from '@ethereumjs/rlp'; import { EthereumJSErrorWithoutCode } from '@ethereumjs/util'; import { InternalBinaryNode } from "./internalNode.js"; import { StemBinaryNode } from "./stemNode.js"; import { BinaryNodeType } from "./types.js"; /** Decode a raw RLP node array into a typed binary tree node. */ export function decodeRawBinaryNode(raw) { const nodeType = raw[0][0]; switch (nodeType) { case BinaryNodeType.Internal: return InternalBinaryNode.fromRawNode(raw); case BinaryNodeType.Stem: return StemBinaryNode.fromRawNode(raw); default: throw EthereumJSErrorWithoutCode('Invalid node type'); } } /** Decode an RLP-serialized binary tree node from bytes. */ export function decodeBinaryNode(raw) { const decoded = RLP.decode(Uint8Array.from(raw)); if (!Array.isArray(decoded)) { throw EthereumJSErrorWithoutCode('Invalid node'); } return decodeRawBinaryNode(decoded); } /** Type guard: value is a raw (non-RLP-wrapped) node array. */ export function isRawBinaryNode(node) { return Array.isArray(node) && !(node instanceof Uint8Array); } /** Type guard for {@link InternalBinaryNode}. */ export function isInternalBinaryNode(node) { return node.type === BinaryNodeType.Internal; } /** Type guard for {@link StemBinaryNode}. */ export function isStemBinaryNode(node) { return node.type === BinaryNodeType.Stem; } //# sourceMappingURL=util.js.map