-
Notifications
You must be signed in to change notification settings - Fork 54
/
Block.ts
127 lines (112 loc) · 3.59 KB
/
Block.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { CML } from '../..';
import { Ed25519PublicKey } from './Key';
import { Hash28ByteBase16, Hash32ByteBase16, OpaqueString, typedBech32 } from '../util/primitives';
import { InvalidStringError } from '../../errors';
import { Lovelace } from './Value';
import { PoolId } from './StakePool/primitives';
import { TxAlonzo } from './Transaction';
/**
* The block size in bytes
*/
export type BlockSize = number;
/**
* The block number.
*/
export type BlockNo = number;
/**
* The epoch number.
*/
export type EpochNo = number;
/**
* Smallest time period in the blockchain
*/
export type Slot = number;
/**
* block hash as hex string
*/
export type BlockId = OpaqueString<'BlockId'>;
export interface PartialBlockHeader {
blockNo: BlockNo;
slot: Slot;
/** Block header hash */
hash: BlockId;
}
export type Tip = PartialBlockHeader;
/**
* @param {string} value block hash as hex string
* @throws InvalidStringError
*/
export const BlockId = (value: string): BlockId => Hash32ByteBase16(value) as unknown as BlockId;
/**
* 32 byte ed25519 verification key as bech32 string.
*/
export type VrfVkBech32 = OpaqueString<'VrfVkBech32'>;
export const VrfVkBech32 = (value: string) => typedBech32<VrfVkBech32>(value, 'vrf_vk', 52);
/**
* Shelley genesis delegate
* Either a 28 byte hex string, or 'ShelleyGenesis-[8byte-hex-string]'
*/
export type GenesisDelegate = OpaqueString<'GenesisDelegate'>;
export const GenesisDelegate = (value: string): GenesisDelegate => {
// eslint-disable-next-line wrap-regex
if (/ShelleyGenesis-[\da-f]{16}/.test(value)) {
return value as unknown as GenesisDelegate;
}
return Hash28ByteBase16(value) as unknown as GenesisDelegate;
};
export type SlotLeader = PoolId | GenesisDelegate;
export const SlotLeader = (value: string): SlotLeader => {
try {
return PoolId(value);
} catch {
try {
return GenesisDelegate(value);
} catch (error) {
throw new InvalidStringError('Expected either PoolId or GenesisDelegate', error);
}
}
};
/**
* Get Bech32 encoded VRF verification key from base64 encoded string
*
* @param value is a Base64 string
* @returns Bech32 encoded vrf_vk
*/
export const VrfVkBech32FromBase64 = (value: string) =>
VrfVkBech32(CML.VRFVKey.from_bytes(Buffer.from(value, 'base64')).to_bech32('vrf_vk'));
/** Minimal Block type meant as a base for the more complete version `Block` */
// TODO: optionals (except previousBlock) are there because they are not calculated for Byron yet.
// Remove them once calculation is done and remove the Required<BlockMinimal> from interface Block
export interface BlockInfo {
header: PartialBlockHeader;
/** Byron blocks fee not calculated yet */
fees?: Lovelace;
totalOutput: Lovelace;
txCount: number;
/** Byron blocks size not calculated yet */
size?: BlockSize;
previousBlock?: BlockId;
vrf?: VrfVkBech32;
/**
* This is the operational cold verification key of the stake pool
* Leaving as undefined for Byron blocks until we figure out how/if we can use the genesisKey field
*/
issuerVk?: Ed25519PublicKey;
}
export interface Block extends BlockInfo {
body: TxAlonzo[];
}
export interface ExtendedBlockInfo
extends Required<Omit<BlockInfo, 'issuerVk' | 'previousBlock'>>,
Pick<BlockInfo, 'previousBlock'> {
/**
* In case of blocks produced by BFT nodes, the SlotLeader the issuerVk hash
* For blocks produced by stake pools, it is the Bech32 encoded value of issuerVk hash
*/
slotLeader: SlotLeader; // TODO: move to CompactBlockInfo and make nullable
date: Date;
epoch: EpochNo;
epochSlot: number;
nextBlock?: BlockId;
confirmations: number;
}