Skip to content

Commit

Permalink
Rename to sequence parser
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBCarter committed Dec 13, 2024
1 parent 704ddde commit 16c9f2b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { assert } from 'vitest'

import { StorageMetaConstants } from './StorageMeta.ts'

export class StorageMetaWrapper {
export class SequenceParser {
private readonly data: Readonly<Uint8Array>

protected constructor(hexString: string) {
Expand Down Expand Up @@ -50,14 +50,14 @@ export class StorageMetaWrapper {
return toHex(this.getBytesSection(start, length).buffer, { prefix: false })
}

static from(timestamp: Hex, hash: Hex, address?: Hex): StorageMetaWrapper
static from(timestamp: number, hash: Hex, address?: Hex): StorageMetaWrapper
static from(timestamp: Hex | number, hash: Hex, address?: Hex): StorageMetaWrapper
static from(timestamp: Hex, hash: Hash, address?: Hex): StorageMetaWrapper
static from(timestamp: number, hash: Hex, address?: Hex): StorageMetaWrapper
static from(timestamp: Hex | number, hash: Hash | Hex, address?: Hex): StorageMetaWrapper {
const epoch = StorageMetaWrapper.timestampToEpoch(timestamp)
const nonce = StorageMetaWrapper.hashToNonce(hash)
static from(timestamp: Hex, hash: Hex, address?: Hex): SequenceParser
static from(timestamp: number, hash: Hex, address?: Hex): SequenceParser
static from(timestamp: Hex | number, hash: Hex, address?: Hex): SequenceParser
static from(timestamp: Hex, hash: Hash, address?: Hex): SequenceParser
static from(timestamp: number, hash: Hex, address?: Hex): SequenceParser
static from(timestamp: Hex | number, hash: Hash | Hex, address?: Hex): SequenceParser {
const epoch = SequenceParser.timestampToEpoch(timestamp)
const nonce = SequenceParser.hashToNonce(hash)
let hexString = epoch + nonce
if (address) {
const paddedAddressHex = address.padStart(StorageMetaConstants.addressBytes * 2, '0')
Expand All @@ -72,7 +72,7 @@ export class StorageMetaWrapper {
return truncatedHashHex
}

static parse(hexString: string): StorageMetaWrapper {
static parse(hexString: string): SequenceParser {
return new this(hexString)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './SequenceParser.ts'
export * from './StorageMeta.ts'
export * from './StorageMetaWrapper.ts'
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ import {
describe, expect, it,
} from 'vitest'

import { StorageMetaWrapper } from '../StorageMetaWrapper.ts'
import { SequenceParser } from '../SequenceParser.ts'

describe('StorageMetaWrapper', () => {
const hash: Hash = toHex('1269b95d3ebf1b1258a82ccca0b365fabf4b8c99bf8fc852e5045e30ad20fbb1')
const address: Address = 'b36d327210f67ad98be881ddf6ad1f1b3e2c5137'
const timestamp = 1_234_567_890_123
const wrapper: StorageMetaWrapper = StorageMetaWrapper.from(timestamp, hash, address)
const wrapper: SequenceParser = SequenceParser.from(timestamp, hash, address)

describe('epoch', () => {
it('converts timestamp to epoch correctly', () => {
expect(wrapper.epoch).toBe(StorageMetaWrapper.timestampToEpoch(timestamp))
expect(wrapper.epoch).toBe(SequenceParser.timestampToEpoch(timestamp))
expect(wrapper.epoch).toBe('0000011f71fb04cb')
})
})

describe('nonce', () => {
it('derives nonce from hash correctly', () => {
expect(wrapper.nonce).toBe(StorageMetaWrapper.hashToNonce(hash))
expect(wrapper.nonce).toBe(SequenceParser.hashToNonce(hash))
expect(wrapper.nonce).toBe(hash.slice(-8 * 2))
expect(wrapper.nonce).toBe('e5045e30ad20fbb1')
})
Expand Down

0 comments on commit 16c9f2b

Please sign in to comment.