-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.ts
243 lines (200 loc) · 5.19 KB
/
types.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
export interface Context extends ContextOpts {
arguments?: object;
}
export interface ContextOpts {
now?: number;
state?: object;
transaction?: Transaction;
balance?: Balance
contract?: ContextContractConstants
}
export interface ContextContractConstants {
address?: Address;
type?: TransactionType;
genesis?: Address;
data?: TransactionData;
}
export class TokenBalance {
tokenAddress!: Address;
tokenId: number = 0;
amount!: number;
}
export class Balance {
uco: number = 0;
token: TokenBalance[] = []
}
export namespace TransactionType {
export const Contract = "contract";
export const Transfer = "transfer";
export const Data = "data";
export const Token = "token";
}
export type TransactionType = string;
export class Transaction {
address!: Address;
type!: TransactionType;
data!: TransactionData;
previousPublicKey!: PublicKey;
genesis!: Address;
validationStamp?: ValidationStamp;
};
export type TransactionData = {
content?: string;
code?: string;
ledger?: Ledger;
};
export type Ledger = {
uco?: UCOLedger;
token?: TokenLedger;
};
export type UCOLedger = {
transfers: UCOTransfer[];
};
export type UCOTransfer = {
to: Address;
amount: number;
};
export type TokenLedger = {
transfers: TokenTransfer[];
};
export type TokenTransfer = {
to: Address;
amount: number;
tokenAddress: Address;
tokenId: number;
};
export type ValidationStamp = {
ledgerOperations: LedgerOperations;
};
export type LedgerOperations = {
unspentOutputs: UTXO[];
};
export type UTXO = UCOUTXO | TokenUTXO | StateUTXO;
export interface UnspentOutput {
amount?: number;
from: string;
}
export interface StateUTXO extends UnspentOutput {
state: object;
type: "state";
}
export interface UCOUTXO extends UnspentOutput {
type: "UCO";
}
export interface TokenUTXO extends UnspentOutput {
type: "token";
tokenAddress: string;
tokenIndex: number;
}
type FunctionWithParams = (input: any, opts?: ContextOpts) => FunctionResult
type FunctionWithOptions = (opts?: ContextOpts) => FunctionResult
export type ContractFunction = FunctionWithOptions & FunctionWithParams
export type ContractFunctions = Record<string, ContractFunction>;
export type TransactionResult = {
transaction?: Transaction;
state?: Record<string, any>;
};
export type FunctionResult = Record<string, any> | TransactionResult | undefined;
export class Nullable<T> {
value!: T
}
export class Result<T> {
ok: Nullable<T> | null = null
error: string | null = null
constructor(okValue: Nullable<T> | null, errValue: string | null) {
if (okValue != null) this.ok = okValue
this.error = errValue
}
static wrapOk<T>(value: T): Result<T> {
return new Result<T>({ value }, null)
}
static wrapError<T>(message: string): Result<T> {
return new Result<T>(null, message)
}
unwrap(): T {
const okValue = this.ok
const errValue = this.error
if (okValue != null) { return okValue.value }
if (errValue != null) throw new Error(`unwrap failed: ${errValue}`)
throw new Error("unwrap failed: invalid Result")
}
unwrapWithDefault(def: T): T {
const okValue = this.ok
if (okValue != null) { return okValue.value }
return def;
}
map<A>(fun: (r: T) => A): Result<A> {
const okValue = this.ok
const errValue = this.error
if (okValue != null) {
return Result.wrapOk(fun(okValue.value))
}
if (errValue != null) {
return Result.wrapError<A>(errValue)
}
throw new Error("map failed: invalid Result")
}
}
export class Hex {
hex: string;
constructor(hex: string) {
if (!/^[0-9a-fA-F]+$/.test(hex) || hex.length % 2 == 1) {
throw new Error("not an hexadecimal")
}
this.hex = hex.toUpperCase();
}
static compare(a: Hex, b: Hex): boolean {
return a.hex == b.hex
}
toString(): string {
return this.hex;
}
}
export class Address extends Hex { }
export class PublicKey extends Hex { }
export class HttpRequest {
uri!: string;
method: Method = Method.GET;
headers: HttpHeader[] = [];
body: string = "";
}
class HttpHeader {
key!: string;
value!: string;
}
export class HttpResponse {
status!: number;
body!: string
}
export enum Method {
GET, //= "GET",
PUT, //= "PUT",
POST, //= "POST",
PATCH, //= "PATCH",
DELETE, //= "DELETE",
}
export enum HashFunction {
SHA256,
SHA512,
SHA3_256,
SHA3_512,
// BLAKE2B,
// KECCAK256 ,
}
export interface IOMock {
getBalance?(address: Address): Balance
getGenesisAddress?(address: Address): Address
getFirstTransactionAddress?(address: Address): Address
getBurnAddress?(): Address
getLastAddress?(address: Address): Address
getPreviousAddress?(previousPublicKey: PublicKey): Address
getGenesisPublicKey?(publicKey: PublicKey): PublicKey
getTransaction?(address: Address): Result<Transaction>
getLastTransaction?(address: Address): Result<Transaction>
callFunction?<A, R>(address: Address, functionName: string, args: A): Result<R>
hmacWithStorageNonce?(data: Uint8Array, hashFunction: HashFunction): Uint8Array
signWithRecovery?(data: Uint8Array): Uint8Array
decryptWithStorageNonce?(cipher: Uint8Array): Uint8Array
request?(req: HttpRequest): HttpResponse
requestMany?(reqs: HttpRequest[]): HttpResponse[]
}