/
key.ts
299 lines (269 loc) · 9.67 KB
/
key.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import * as externals from "./externals";
import {readHostBuffer} from ".";
import {UREF_SERIALIZED_LENGTH} from "./constants";
import {URef} from "./uref";
import {CLValue} from "./clvalue";
import {Error, ErrorCode} from "./error";
import {checkTypedArrayEqual, typedToArray, encodeUTF8, checkArraysEqual} from "./utils";
import {Ref} from "./ref";
import {Result, Error as BytesreprError} from "./bytesrepr";
import {PublicKey} from "./public_key";
import {RuntimeArgs} from "./runtime_args";
import * as runtime from "./runtime";
/**
* Enum representing a variant of a [[Key]] - Account, Hash or URef.
*/
export enum KeyVariant {
/** The Account variant */
ACCOUNT_ID = 0,
/** The Hash variant */
HASH_ID = 1,
/** The URef variant */
UREF_ID = 2,
}
/** A cryptographic public key. */
export class AccountHash {
/**
* Constructs a new `AccountHash`.
*
* @param bytes The bytes constituting the public key.
*/
constructor(public bytes: Array<u8>) {}
/** Checks whether two `AccountHash`s are equal. */
@operator("==")
equalsTo(other: AccountHash): bool {
return checkArraysEqual<u8, Array<u8>>(this.bytes, other.bytes);
}
/** Checks whether two `AccountHash`s are not equal. */
@operator("!=")
notEqualsTo(other: AccountHash): bool {
return !this.equalsTo(other);
}
static fromPublicKey(publicKey: PublicKey) : AccountHash {
let algorithmName = publicKey.getAlgorithmName();
let algorithmNameBytes = Uint8Array.wrap(encodeUTF8(algorithmName));
let publicKeyBytes = publicKey.getRawBytes();
let dataLength = algorithmNameBytes.length + publicKeyBytes.length + 1;
let data = new Array<u8>(dataLength);
for (let i=0; i < algorithmNameBytes.length; i++){
data[i] = algorithmNameBytes[i];
}
data[algorithmNameBytes.length] = 0;
for (let i=0; i < publicKeyBytes.length; i++) {
data[algorithmNameBytes.length + 1 + i] = publicKeyBytes[i];
}
const accountHashBytes = runtime.blake2b(data);
return new AccountHash(accountHashBytes);
}
/** Deserializes a `AccountHash` from an array of bytes. */
static fromBytes(bytes: Array<u8>): Result<AccountHash> {
if (bytes.length < 32) {
return new Result<AccountHash>(null, BytesreprError.EarlyEndOfStream, 0);
}
let accountHashBytes = bytes.slice(0, 32);
let accountHash = new AccountHash(accountHashBytes);
let ref = new Ref<AccountHash>(accountHash);
return new Result<AccountHash>(ref, BytesreprError.Ok, 32);
}
/** Serializes a `AccountHash` into an array of bytes. */
toBytes(): Array<u8> {
return this.bytes;
}
}
/**
* The type under which data (e.g. [[CLValue]]s, smart contracts, user accounts)
* are indexed on the network.
*/
export class Key {
variant: KeyVariant;
hash: StaticArray<u8> | null;
uref: URef | null;
account: AccountHash | null;
/** Creates a `Key` from a given [[URef]]. */
static fromURef(uref: URef): Key {
let key = new Key();
key.variant = KeyVariant.UREF_ID;
key.uref = uref;
return key;
}
/** Creates a `Key` from a given hash. */
static fromHash(hash: StaticArray<u8>): Key {
let key = new Key();
key.variant = KeyVariant.HASH_ID;
key.hash = hash;
return key;
}
/** Creates a `Key` from a [[<AccountHash>]] representing an account. */
static fromAccount(account: AccountHash): Key {
let key = new Key();
key.variant = KeyVariant.ACCOUNT_ID;
key.account = account;
return key;
}
/**
* Attempts to write `value` under a new Key::URef
*
* If a key is returned it is always of [[KeyVariant]].UREF_ID
*/
static create(value: CLValue): Key | null {
const valueBytes = value.toBytes();
let urefBytes = new StaticArray<u8>(UREF_SERIALIZED_LENGTH);
externals.new_uref(
changetype<usize>(urefBytes),
valueBytes.dataStart,
valueBytes.length
);
const urefResult = URef.fromBytes(urefBytes);
if (urefResult.hasError()) {
return null;
}
return Key.fromURef(urefResult.value);
}
/** Deserializes a `Key` from an array of bytes. */
static fromBytes(bytes: StaticArray<u8>): Result<Key> {
if (bytes.length < 1) {
return new Result<Key>(null, BytesreprError.EarlyEndOfStream, 0);
}
const tag = bytes[0];
let currentPos = 1;
if (tag == KeyVariant.HASH_ID) {
var hashBytes = bytes.slice(1, 32 + 1);
currentPos += 32;
let key = Key.fromHash(StaticArray.fromArray(hashBytes));
let ref = new Ref<Key>(key);
return new Result<Key>(ref, BytesreprError.Ok, currentPos);
}
else if (tag == KeyVariant.UREF_ID) {
var urefBytes = bytes.slice(1);
var urefResult = URef.fromBytes(StaticArray.fromArray(urefBytes));
if (urefResult.error != BytesreprError.Ok) {
return new Result<Key>(null, urefResult.error, 0);
}
let key = Key.fromURef(urefResult.value);
let ref = new Ref<Key>(key);
return new Result<Key>(ref, BytesreprError.Ok, currentPos + urefResult.position);
}
else if (tag == KeyVariant.ACCOUNT_ID) {
let accountHashBytes = bytes.slice(1);
let accountHashResult = AccountHash.fromBytes(accountHashBytes);
if (accountHashResult.hasError()) {
return new Result<Key>(null, accountHashResult.error, currentPos);
}
currentPos += accountHashResult.position;
let key = Key.fromAccount(accountHashResult.value);
let ref = new Ref<Key>(key);
return new Result<Key>(ref, BytesreprError.Ok, currentPos);
}
else {
return new Result<Key>(null, BytesreprError.FormattingError, currentPos);
}
}
/** Serializes a `Key` into an array of bytes. */
toBytes(): Array<u8> {
if(this.variant == KeyVariant.UREF_ID){
let bytes = new Array<u8>();
bytes.push(<u8>this.variant)
bytes = bytes.concat((<URef>this.uref).toBytes());
return bytes;
}
else if (this.variant == KeyVariant.HASH_ID) {
var hashBytes = <StaticArray<u8>>this.hash;
let bytes = new Array<u8>(1 + hashBytes.length);
bytes[0] = <u8>this.variant;
for (let i = 0; i < hashBytes.length; i++) {
bytes[i + 1] = hashBytes[i];
}
return bytes;
}
else if (this.variant == KeyVariant.ACCOUNT_ID) {
let bytes = new Array<u8>();
bytes.push(<u8>this.variant);
bytes = bytes.concat((<AccountHash>this.account).toBytes());
return bytes;
}
unreachable();
return [];
}
/** Checks whether the `Key` is of [[KeyVariant]].UREF_ID. */
isURef(): bool {
return this.variant == KeyVariant.UREF_ID;
}
/** Converts the `Key` into `URef`. */
toURef(): URef {
return <URef>this.uref;
}
/** Reads the data stored under this `Key`. */
read(): StaticArray<u8> | null {
const keyBytes = this.toBytes();
let valueSize = new Uint8Array(1);
const ret = externals.read_value(keyBytes.dataStart, keyBytes.length, valueSize.dataStart);
const error = Error.fromResult(ret);
if (error != null) {
if (error.value() == ErrorCode.ValueNotFound) {
return null;
}
error.revert();
unreachable();
}
// TODO: How can we have `read<T>` that would deserialize host bytes into T?
return readHostBuffer(valueSize[0]);
}
/** Stores a [[CLValue]] under this `Key`. */
write(value: CLValue): void {
const keyBytes = this.toBytes();
const valueBytes = value.toBytes();
externals.write(
keyBytes.dataStart,
keyBytes.length,
valueBytes.dataStart,
valueBytes.length
);
}
/** Adds the given `CLValue` to a value already stored under this `Key`. */
add(value: CLValue): void {
const keyBytes = this.toBytes();
const valueBytes = value.toBytes();
externals.add(
keyBytes.dataStart,
keyBytes.length,
valueBytes.dataStart,
valueBytes.length
);
}
/** Checks whether two `Key`s are equal. */
@operator("==")
equalsTo(other: Key): bool {
if (this.variant == KeyVariant.UREF_ID) {
if (other.variant == KeyVariant.UREF_ID) {
return <URef>this.uref == <URef>other.uref;
}
else {
return false;
}
}
else if (this.variant == KeyVariant.HASH_ID) {
if (other.variant == KeyVariant.HASH_ID) {
return checkArraysEqual<u8, StaticArray<u8>>(<StaticArray<u8>>this.hash, <StaticArray<u8>>other.hash);
}
else {
return false;
}
}
else if (this.variant == KeyVariant.ACCOUNT_ID) {
if (other.variant == KeyVariant.ACCOUNT_ID) {
return <AccountHash>this.account == <AccountHash>other.account;
}
else {
return false;
}
}
else {
return false;
}
}
/** Checks whether two keys are not equal. */
@operator("!=")
notEqualsTo(other: Key): bool {
return !this.equalsTo(other);
}
}