-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
163 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/protocol/packages/payload/packages/model/src/StorageMeta/DataHash.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { type Hash, isHash } from '@xylabs/hex' | ||
import { AsObjectFactory } from '@xylabs/object' | ||
|
||
import type { Payload } from '../Payload.ts' | ||
|
||
export interface DataHashStorageMeta { | ||
_dataHash: Hash | ||
} | ||
|
||
export type WithDataHashStorageMeta<T extends Payload = Payload> = T & DataHashStorageMeta | ||
export type WithPartialDataHashStorageMeta<T extends Payload = Payload> = Partial<WithDataHashStorageMeta<T>> | ||
|
||
export const isDataHashStorageMeta = (value: unknown): value is DataHashStorageMeta => { | ||
return isHash((value as WithDataHashStorageMeta)?._dataHash) | ||
} | ||
|
||
export const asDataHashStorageMeta = AsObjectFactory.create<DataHashStorageMeta>(isDataHashStorageMeta) | ||
export const asOptionalDataHashStorageMeta = AsObjectFactory.createOptional<DataHashStorageMeta>(isDataHashStorageMeta) |
19 changes: 19 additions & 0 deletions
19
packages/protocol/packages/payload/packages/model/src/StorageMeta/Hash.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { type Hash, isHash } from '@xylabs/hex' | ||
import { AsObjectFactory } from '@xylabs/object' | ||
|
||
import type { Payload } from '../Payload.ts' | ||
import { type DataHashStorageMeta, isDataHashStorageMeta } from './DataHash.ts' | ||
|
||
export interface HashStorageMeta extends DataHashStorageMeta { | ||
_hash: Hash | ||
} | ||
|
||
export type WithHashStorageMeta<T extends Payload = Payload> = T & HashStorageMeta | ||
export type WithPartialHashStorageMeta<T extends Payload = Payload> = Partial<WithHashStorageMeta<T>> | ||
|
||
export const isHashStorageMeta = (value: unknown): value is HashStorageMeta => { | ||
return isDataHashStorageMeta(value) && isHash((value as WithHashStorageMeta)?._hash) | ||
} | ||
|
||
export const asHashStorageMeta = AsObjectFactory.create<HashStorageMeta>(isHashStorageMeta) | ||
export const asOptionalHashStorageMeta = AsObjectFactory.createOptional<HashStorageMeta>(isHashStorageMeta) |
82 changes: 11 additions & 71 deletions
82
packages/protocol/packages/payload/packages/model/src/StorageMeta/Sequence.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,18 @@ | ||
import type { Address, Hex } from '@xylabs/hex' | ||
import { isHex } from '@xylabs/hex' | ||
import { AsObjectFactory } from '@xylabs/object' | ||
|
||
// we use Exclude to intentionally make the type not equal to string | ||
export type LocalSequence = Hex & Exclude<string, 'reserved-local-sequence-value'> | ||
export type QualifiedSequence = Hex & Exclude<string, 'reserved-qualified-sequence-value'> | ||
export type Sequence = LocalSequence | QualifiedSequence | ||
import type { Payload } from '../Payload.ts' | ||
import type { Sequence } from './sequence/index.ts' | ||
|
||
export type Epoch = Hex & Exclude<string, 'reserved-epoch-sequence-value'> | ||
|
||
export const isEpoch = (value: unknown): value is Epoch => { | ||
return isHex(value) && (value as string).length === SequenceConstants.epochBytes * 2 | ||
} | ||
|
||
export type Nonce = Hex & Exclude<string, 'reserved-nonce-sequence-value'> | ||
|
||
export const isNonce = (value: unknown): value is Epoch => { | ||
return isHex(value) && (value as string).length === SequenceConstants.nonceBytes * 2 | ||
} | ||
|
||
export const isLocalSequence = (value: unknown): value is Sequence => { | ||
return isHex(value) && (value as string).length === SequenceConstants.localSequenceBytes * 2 | ||
} | ||
|
||
export const isQualifiedSequence = (value: unknown): value is Sequence => { | ||
return isHex(value) && (value as string).length === SequenceConstants.qualifiedSequenceBytes * 2 | ||
} | ||
|
||
export const isSequence = (value: unknown): value is Sequence => { | ||
return isLocalSequence(value) || isQualifiedSequence(value) | ||
export interface SequenceStorageMeta { | ||
_sequence: Sequence | ||
} | ||
|
||
export const SequenceNonceComponentLengths = { | ||
nonceIndexBytes: 4, | ||
nonceHashBytes: 4, | ||
} | ||
|
||
export const SequenceComponentLengths = { | ||
...SequenceNonceComponentLengths, | ||
epochBytes: 8, | ||
nonceBytes: SequenceNonceComponentLengths.nonceIndexBytes + SequenceNonceComponentLengths.nonceHashBytes, | ||
addressBytes: 20, | ||
} | ||
|
||
export const SequenceComponentMinMax = { | ||
minEpoch: '0'.repeat(SequenceComponentLengths.epochBytes * 2) as Epoch, | ||
maxEpoch: 'f'.repeat(SequenceComponentLengths.epochBytes * 2) as Epoch, | ||
minNonce: '0'.repeat(SequenceComponentLengths.nonceBytes * 2) as Nonce, | ||
maxNonce: 'f'.repeat(SequenceComponentLengths.nonceBytes * 2) as Nonce, | ||
minAddress: '0'.repeat(SequenceComponentLengths.addressBytes * 2) as Address, | ||
maxAddress: 'f'.repeat(SequenceComponentLengths.addressBytes * 2) as Address, | ||
} | ||
export type WithSequenceStorageMeta<T extends Payload = Payload> = T & SequenceStorageMeta | ||
export type WithPartialSequenceStorageMeta<T extends Payload = Payload> = Partial<WithSequenceStorageMeta<T>> | ||
|
||
export const LocalSequenceConstants = { | ||
...SequenceComponentLengths, | ||
...SequenceComponentMinMax, | ||
localSequenceBytes: SequenceComponentLengths.epochBytes + SequenceComponentLengths.nonceBytes, | ||
minLocalSequence: SequenceComponentMinMax.minEpoch + SequenceComponentMinMax.minNonce as LocalSequence, | ||
maxLocalSequence: SequenceComponentMinMax.maxEpoch + SequenceComponentMinMax.maxNonce as LocalSequence, | ||
export const isSequenceStorageMeta = (value: unknown): value is SequenceStorageMeta => { | ||
return (value as WithSequenceStorageMeta)?._sequence !== undefined | ||
} | ||
|
||
export const QualifiedSequenceConstants = { | ||
qualifiedSequenceBytes: LocalSequenceConstants.localSequenceBytes + SequenceComponentLengths.addressBytes, | ||
minQualifiedSequence: LocalSequenceConstants.minLocalSequence + SequenceComponentMinMax.minAddress as QualifiedSequence, | ||
maxQualifiedSequence: LocalSequenceConstants.maxLocalSequence + SequenceComponentMinMax.maxAddress as QualifiedSequence, | ||
} | ||
|
||
export const SequenceConstants = { | ||
...LocalSequenceConstants, | ||
...QualifiedSequenceConstants, | ||
} | ||
|
||
// "11111111111111112222222222222222" is and example of a local sequence string | ||
|
||
// "111111111111111122222222222222223333333333333333333333333333333333333333" is and example of a local sequence string | ||
// epoch = "1111111111111111" | ||
// nonce = "2222222222222222" | ||
// address = "3333333333333333333333333333333333333333" | ||
export const asSequenceStorageMeta = AsObjectFactory.create(isSequenceStorageMeta) | ||
export const asOptionalSequenceStorageMeta = AsObjectFactory.createOptional(isSequenceStorageMeta) |
46 changes: 7 additions & 39 deletions
46
packages/protocol/packages/payload/packages/model/src/StorageMeta/StorageMeta.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,17 @@ | ||
import { type Hash, isHash } from '@xylabs/hex' | ||
import { AsObjectFactory } from '@xylabs/object' | ||
|
||
import type { Payload } from '../Payload.ts' | ||
import type { Sequence } from './Sequence.ts' | ||
import { type HashStorageMeta, isHashStorageMeta } from './Hash.ts' | ||
import { isSequenceStorageMeta, type SequenceStorageMeta } from './Sequence.ts' | ||
|
||
export interface SequenceMeta { | ||
_sequence: Sequence | ||
} | ||
|
||
export type WithPartialSequenceMeta<T extends Payload = Payload> = Partial<WithSequenceMeta<T>> | ||
|
||
export type WithSequenceMeta<T extends Payload = Payload> = T & SequenceMeta | ||
|
||
export interface HashMeta { | ||
_dataHash: Hash | ||
_hash: Hash | ||
} | ||
|
||
export type WithPartialHashMeta<T extends Payload = Payload> = Partial<WithHashMeta<T>> | ||
|
||
export type WithHashMeta<T extends Payload = Payload> = T & HashMeta | ||
|
||
export interface StorageMeta extends SequenceMeta, HashMeta {} | ||
|
||
export type WithPartialStorageMeta<T extends Payload = Payload> = Partial<WithStorageMeta<T>> | ||
export interface StorageMeta extends SequenceStorageMeta, HashStorageMeta {} | ||
|
||
export type WithStorageMeta<T extends Payload = Payload> = T & StorageMeta | ||
|
||
export const isSequenceMeta = (value: unknown): value is SequenceMeta => { | ||
return (value as WithSequenceMeta)?._sequence !== undefined | ||
} | ||
|
||
export const isHashMeta = (value: unknown): value is HashMeta => { | ||
return isHash((value as WithHashMeta)?._hash) && isHash((value as WithHashMeta)?._dataHash) | ||
} | ||
export type WithPartialStorageMeta<T extends Payload = Payload> = Partial<WithStorageMeta<T>> | ||
|
||
export const isStorageMeta = (value: unknown): value is StorageMeta => { | ||
return isSequenceMeta(value) && isHashMeta(value) | ||
return isSequenceStorageMeta(value) && isHashStorageMeta(value) | ||
} | ||
export const asStorageMeta = AsObjectFactory.create(isStorageMeta) | ||
export const asOptionalStorageMeta = AsObjectFactory.createOptional(isStorageMeta) | ||
|
||
// "00005a7f354762f3ac1bc5ddc6cfd08d14" is and example of a local sequence string | ||
|
||
// "00005a7f354762f3ac1bc5ddc6cfd08d14a123456789abcdef0123" is and example of a local sequence string | ||
// epoch = "00005a7f354762f3ac" | ||
// nonce = "1bc5ddc6cfd08d14" | ||
// address = "a123456789abcdef0123" | ||
export const asStorageStorageMeta = AsObjectFactory.create(isStorageMeta) | ||
export const asOptionalStorageMeta = AsObjectFactory.createOptional(isStorageMeta) |
5 changes: 3 additions & 2 deletions
5
packages/protocol/packages/payload/packages/model/src/StorageMeta/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './DataHash.ts' | ||
export * from './Hash.ts' | ||
export * from './Sequence.ts' | ||
export * from './SequenceComparer.ts' | ||
export * from './SequenceParser.ts' | ||
export * from './sequence/index.ts' | ||
export * from './StorageMeta.ts' |
2 changes: 1 addition & 1 deletion
2
...model/src/StorageMeta/SequenceComparer.ts → ...odel/src/StorageMeta/sequence/Comparer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
78 changes: 78 additions & 0 deletions
78
packages/protocol/packages/payload/packages/model/src/StorageMeta/sequence/Sequence.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import type { Address, Hex } from '@xylabs/hex' | ||
import { isHex } from '@xylabs/hex' | ||
|
||
// we use Exclude to intentionally make the type not equal to string | ||
export type LocalSequence = Hex & Exclude<string, 'reserved-local-sequence-value'> | ||
export type QualifiedSequence = Hex & Exclude<string, 'reserved-qualified-sequence-value'> | ||
export type Sequence = LocalSequence | QualifiedSequence | ||
|
||
export type Epoch = Hex & Exclude<string, 'reserved-epoch-sequence-value'> | ||
|
||
export const isEpoch = (value: unknown): value is Epoch => { | ||
return isHex(value) && (value as string).length === SequenceConstants.epochBytes * 2 | ||
} | ||
|
||
export type Nonce = Hex & Exclude<string, 'reserved-nonce-sequence-value'> | ||
|
||
export const isNonce = (value: unknown): value is Epoch => { | ||
return isHex(value) && (value as string).length === SequenceConstants.nonceBytes * 2 | ||
} | ||
|
||
export const isLocalSequence = (value: unknown): value is Sequence => { | ||
return isHex(value) && (value as string).length === SequenceConstants.localSequenceBytes * 2 | ||
} | ||
|
||
export const isQualifiedSequence = (value: unknown): value is Sequence => { | ||
return isHex(value) && (value as string).length === SequenceConstants.qualifiedSequenceBytes * 2 | ||
} | ||
|
||
export const isSequence = (value: unknown): value is Sequence => { | ||
return isLocalSequence(value) || isQualifiedSequence(value) | ||
} | ||
|
||
export const SequenceNonceComponentLengths = { | ||
nonceIndexBytes: 4, | ||
nonceHashBytes: 4, | ||
} | ||
|
||
export const SequenceComponentLengths = { | ||
...SequenceNonceComponentLengths, | ||
epochBytes: 8, | ||
nonceBytes: SequenceNonceComponentLengths.nonceIndexBytes + SequenceNonceComponentLengths.nonceHashBytes, | ||
addressBytes: 20, | ||
} | ||
|
||
export const SequenceComponentMinMax = { | ||
minEpoch: '0'.repeat(SequenceComponentLengths.epochBytes * 2) as Epoch, | ||
maxEpoch: 'f'.repeat(SequenceComponentLengths.epochBytes * 2) as Epoch, | ||
minNonce: '0'.repeat(SequenceComponentLengths.nonceBytes * 2) as Nonce, | ||
maxNonce: 'f'.repeat(SequenceComponentLengths.nonceBytes * 2) as Nonce, | ||
minAddress: '0'.repeat(SequenceComponentLengths.addressBytes * 2) as Address, | ||
maxAddress: 'f'.repeat(SequenceComponentLengths.addressBytes * 2) as Address, | ||
} | ||
|
||
export const LocalSequenceConstants = { | ||
...SequenceComponentLengths, | ||
...SequenceComponentMinMax, | ||
localSequenceBytes: SequenceComponentLengths.epochBytes + SequenceComponentLengths.nonceBytes, | ||
minLocalSequence: SequenceComponentMinMax.minEpoch + SequenceComponentMinMax.minNonce as LocalSequence, | ||
maxLocalSequence: SequenceComponentMinMax.maxEpoch + SequenceComponentMinMax.maxNonce as LocalSequence, | ||
} | ||
|
||
export const QualifiedSequenceConstants = { | ||
qualifiedSequenceBytes: LocalSequenceConstants.localSequenceBytes + SequenceComponentLengths.addressBytes, | ||
minQualifiedSequence: LocalSequenceConstants.minLocalSequence + SequenceComponentMinMax.minAddress as QualifiedSequence, | ||
maxQualifiedSequence: LocalSequenceConstants.maxLocalSequence + SequenceComponentMinMax.maxAddress as QualifiedSequence, | ||
} | ||
|
||
export const SequenceConstants = { | ||
...LocalSequenceConstants, | ||
...QualifiedSequenceConstants, | ||
} | ||
|
||
// "11111111111111112222222222222222" is and example of a local sequence string | ||
|
||
// "111111111111111122222222222222223333333333333333333333333333333333333333" is and example of a local sequence string | ||
// epoch = "1111111111111111" | ||
// nonce = "2222222222222222" | ||
// address = "3333333333333333333333333333333333333333" |
3 changes: 3 additions & 0 deletions
3
packages/protocol/packages/payload/packages/model/src/StorageMeta/sequence/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './Comparer.ts' | ||
export * from './Parser.ts' | ||
export * from './Sequence.ts' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.