-
Notifications
You must be signed in to change notification settings - Fork 1
/
clarity.ts
616 lines (543 loc) · 16.5 KB
/
clarity.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/* This is free and unencumbered software released into the public domain. */
type bool = boolean
type buff = Uint8Array
type expr<T> = () => T
type int = number
type list<E> = Array<E>
type optional<T> = T | typeof none
type principal = string
type err<T> = Err<T>
type response<T, E> = T | err<E>
type trait = string
type tuple = Map<String, any>
type uint = number
interface Seq {
length: uint
}
function hash(algorithm: string, value: buff | uint | int): buff {
if (Number.isInteger(value)) {
let buff = new Uint8Array(16) // 128 bits
let view = new DataView(buff.buffer)
view.setBigUint64(0, BigInt(value as number), true)
value = buff
}
if (value instanceof Uint8Array) {
let buffer = null
switch (algorithm) {
case 'keccak256':
throw new Error("not implemented yet") // TODO
default:
throw new Error("not implemented yet") // TODO
//buffer = crypto.createHash(algorithm).update(value).digest()
}
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength)
}
throw new TypeError()
}
const txSenderStack: Array<principal> = []
export var SmartWeave: any = null
export class Panic<T> extends Error {
public constructor(message: string) {
super(message)
Object.setPrototypeOf(this, Panic.prototype)
}
}
/**
* @link https://docs.blockstack.org/references/language-clarity#clarity-type-system
*/
export class Err<T> extends Error {
public constructor(public value: T) {
super("") // TODO
Object.setPrototypeOf(this, Err.prototype)
}
}
export function requireVersion(version: string): void {
// TODO: throw an error if the version isn't supported
}
export function requireFeature(feature: string): void {
// TODO: throw an error if the feature isn't supported
}
/**
* @link https://docs.blockstack.org/references/language-clarity#-add
*/
export function add(...args: int[] | uint[]): int | uint {
return args.reduce((sum, operand) => sum + operand, 0)
}
/**
* @link https://docs.blockstack.org/references/language-clarity#--subtract
*/
export function sub(...args: int[] | uint[]): int | uint {
return args.slice(1).reduce((difference, operand) => difference - operand, args[0])
}
/**
* @link https://docs.blockstack.org/references/language-clarity#-multiply
*/
export function mul(...args: int[] | uint[]): int | uint {
return args.reduce((product, operand) => product * operand, 1)
}
/**
* @link https://docs.blockstack.org/references/language-clarity#-divide
*/
export function div(...args: int[] | uint[]): int | uint {
return Math.trunc(args.slice(1).reduce((quotient, operand) => quotient / operand, args[0]))
}
/**
* @link https://docs.blockstack.org/references/language-clarity#-less-than
*/
export function lt<T>(a: T, b: T): bool {
return a < b
}
/**
* @link https://docs.blockstack.org/references/language-clarity#-less-than-or-equal
*/
export function le<T>(a: T, b: T): bool {
return a <= b
}
/**
* @link https://docs.blockstack.org/references/language-clarity#-greater-than
*/
export function gt<T>(a: T, b: T): bool {
return a > b
}
/**
* @link https://docs.blockstack.org/references/language-clarity#-greater-than-or-equal
*/
export function ge<T>(a: T, b: T): bool {
return a >= b
}
/**
* @link https://docs.blockstack.org/references/language-clarity#append
*/
export function append<T>(list: list<T>, value: T): list<T> {
return [...list, value]
}
/**
* @link https://docs.blockstack.org/references/language-clarity#as-contract
*/
export function asContract<A>(expr: expr<A>): A {
if (SmartWeave) {
try {
txSenderStack.unshift(SmartWeave.contract.id)
return expr()
}
finally {
txSenderStack.shift()
}
}
throw new Error("as-contract not supported")
}
/**
* @link https://docs.blockstack.org/references/language-clarity#as-max-len
*/
export function asMaxLen<T extends Seq>(value: T, length: uint): optional<T> {
return value.length <= length ? some(value) : none
}
/**
* @link https://docs.blockstack.org/references/language-clarity#at-block
*/
export function atBlock<A>(blockHash: buff, expr: expr<A>): A {
if (SmartWeave) {
throw new Error("at-block not supported on SmartWeave")
}
throw new Error("not implemented yet") // TODO
}
/**
* @link https://docs.blockstack.org/references/language-clarity#block-height
*/
export function blockHeight(): uint {
if (SmartWeave) {
return SmartWeave!.block.height
}
throw new Error("block-height not supported")
}
/**
* @link https://docs.blockstack.org/references/language-clarity#concat
*/
export function concat<T>(a: list<T> | buff, b: list<T> | buff): list<T> | buff {
if (a instanceof Array && b instanceof Array) {
return ([] as list<T>).concat(a, b)
}
if (a instanceof Uint8Array && b instanceof Uint8Array) {
const result = new Uint8Array(a.byteLength + b.byteLength)
result.set(a, 0)
result.set(b, a.byteLength)
return result
}
throw new TypeError()
}
/**
* @link https://docs.blockstack.org/references/language-clarity#contract-call
*/
export function contractCall<A, B>(contractName: trait, functionName: string, ...args: any): response<A, B> {
if (SmartWeave) {
throw new Error("contract-call? not supported on SmartWeave")
}
throw new Error("not implemented yet") // TODO
}
/**
* @link https://docs.blockstack.org/references/language-clarity#contract-caller
*/
export function contractCaller(): principal {
if (SmartWeave) {
return txSender()
}
throw new Error("contract-caller not supported")
}
/**
* @link https://docs.blockstack.org/references/language-clarity#contract-of
*/
export function contractOf(contractName: trait): principal {
if (SmartWeave) {
throw new Error("contract-of not supported on SmartWeave")
}
throw new Error("not implemented yet") // TODO
}
/**
* @link https://docs.blockstack.org/references/language-clarity#default-to
*/
export function defaultTo<T>(defaultValue: T, optionValue: optional<T>): T {
return optionValue ?? defaultValue
}
/**
* @link https://docs.blockstack.org/references/language-clarity#err
*/
export function err<T>(value: T) {
return new Err(value)
}
/**
* @link https://docs.blockstack.org/references/language-clarity#filter
*/
export function filter<A>(func: (a: A) => bool, list: list<A>): list<A> {
if (list instanceof Array) {
return list.filter(func)
}
throw new TypeError()
}
/**
* @link https://docs.blockstack.org/references/language-clarity#fold
*/
export function fold<A, B>(func: (a: A, b: B) => B, list: list<A>, initialValue: B): B {
if (list instanceof Array) {
return list.reduce((accumulator, currentValue) => func(currentValue, accumulator), initialValue)
}
throw new TypeError()
}
/**
* @link https://docs.blockstack.org/references/language-clarity#ft-get-balance
*/
export function ftGetBalance(tokenName: string, principal: principal): uint {
throw new Error("not implemented yet") // TODO
}
/**
* @link https://docs.blockstack.org/references/language-clarity#ft-mint
*/
export function ftMint(tokenName: string, amount: uint, recipient: principal): response<bool, uint> {
throw new Error("not implemented yet") // TODO
}
/**
* @link https://docs.blockstack.org/references/language-clarity#ft-transfer
*/
export function ftTransfer(tokenName: string, amount: uint, sender: principal, recipient: principal): response<bool, uint> {
throw new Error("not implemented yet") // TODO
}
/**
* @link https://docs.blockstack.org/references/language-clarity#get
*/
export function get<T>(keyName: string, tuple: tuple | optional<tuple>): T | optional<T> {
return isNone(tuple) ? none : tuple.get(keyName)
}
/**
* @link https://docs.blockstack.org/references/language-clarity#get-block-info
*/
export function getBlockInfo(propName: string, blockHeight: uint): optional<buff> | optional<uint> {
if (SmartWeave) {
throw new Error("get-block-info? not supported on SmartWeave")
}
throw new Error("not implemented yet") // TODO
}
/**
* @link https://docs.blockstack.org/references/language-clarity#hash160
*/
export function hash160(value: buff | uint | int): buff {
if (Number.isInteger(value)) {
let buff = new Uint8Array(16) // 128 bits
let view = new DataView(buff.buffer)
view.setBigUint64(0, BigInt(value as number), true)
value = buff
}
if (value instanceof Uint8Array) {
throw new Error("not implemented yet") // TODO
//const sha256 = crypto.createHash('sha256').update(value).digest()
//const buffer = crypto.createHash('ripemd160').update(sha256).digest()
//return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength)
}
throw new TypeError()
}
/**
* @link https://docs.blockstack.org/references/language-clarity#is-eq
*/
export function isEq(...values: any[]): bool {
if (values.length > 0 && values.every((value) => typeof value === typeof values[0])) {
return values.every((value) => value === values[0])
}
throw new TypeError()
}
/**
* @link https://docs.blockstack.org/references/language-clarity#is-err
*/
export function isErr(value: any): bool {
return value instanceof Err
}
/**
* @link https://docs.blockstack.org/references/language-clarity#is-none
*/
export function isNone(value: any): bool {
return value === none
}
/**
* @link https://docs.blockstack.org/references/language-clarity#is-ok
*/
export function isOk(value: any): bool {
return !(value instanceof Err)
}
/**
* @link https://docs.blockstack.org/references/language-clarity#is-some
*/
export function isSome(value: any): bool {
return value !== none
}
/**
* @link https://docs.blockstack.org/references/language-clarity#keccak256
*/
export function keccak256(value: buff | uint | int): buff {
return hash('keccak256', value)
}
/**
* @link https://docs.blockstack.org/references/language-clarity#len
*/
export function len<T>(value: list<T> | buff | string): uint {
return value.length
}
/**
* @link https://docs.blockstack.org/references/language-clarity#list
*/
export function list<T>(...values: T[]): T[] {
if (values.length > 0 && values.some((value) => typeof value !== typeof values[0])) {
throw new TypeError()
}
return values
}
/**
* @link https://docs.blockstack.org/references/language-clarity#map
*/
export function map<A, B>(func: (a: A) => B, list: list<A>): list<B> {
if (list instanceof Array) {
return list.map(func)
}
throw new TypeError()
}
/**
* @link https://docs.blockstack.org/references/language-clarity#map-delete
*/
export function mapDelete(map: Map<tuple, tuple>, key: tuple): bool {
return map.delete(key)
}
/**
* @link https://docs.blockstack.org/references/language-clarity#map-get
*/
export function mapGet(map: Map<tuple, tuple>, key: tuple): optional<tuple> {
const value = map.get(key)
return value ? some(value) : none
}
/**
* @link https://docs.blockstack.org/references/language-clarity#map-insert
*/
export function mapInsert(map: Map<tuple, tuple>, key: tuple, value: tuple): bool {
if (map.has(key)) return false
map.set(key, value)
return true
}
/**
* @link https://docs.blockstack.org/references/language-clarity#map-set
*/
export function mapSet(map: Map<tuple, tuple>, key: tuple, value: tuple): bool {
map.set(key, value)
return true
}
/**
* @link https://docs.blockstack.org/references/language-clarity#match
*/
export function match<T, E>(input: optional<T> | response<T, E>, okBranch: (_: T) => any, errBranch: (_: E) => any): any {
if (isNone(input) || isErr(input)) {
return errBranch(input)
}
return okBranch(input)
}
/**
* @link https://docs.blockstack.org/references/language-clarity#mod
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder
*/
export function mod(a: int | uint, b: int | uint): int | uint {
if (b === 0) {
throw new RangeError("division by zero")
}
return a % b
}
/**
* @link https://docs.blockstack.org/references/language-clarity#nft-get-owner
*/
export function nftGetOwner(assetClass: string, assetID: string): optional<principal> {
throw new Error("not implemented yet") // TODO
}
/**
* @link https://docs.blockstack.org/references/language-clarity#nft-mint
*/
export function nftMint(assetClass: string, assetID: string, recipient: principal): response<bool, uint> {
throw new Error("not implemented yet") // TODO
}
/**
* @link https://docs.blockstack.org/references/language-clarity#nft-transfer
*/
export function nftTransfer(assetClass: string, assetID: string, sender: principal, recipient: principal): response<bool, uint> {
throw new Error("not implemented yet") // TODO
}
/**
* @link https://docs.blockstack.org/references/language-clarity#none
*/
export const none: any = null
/**
* @link https://docs.blockstack.org/references/language-clarity#not
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT
*/
export function not(value: bool): bool {
return !value
}
/**
* @link https://docs.blockstack.org/references/language-clarity#ok
*/
export function ok<T, E>(value: T): response<T, E> {
return value
}
/**
* @link https://docs.blockstack.org/references/language-clarity#pow
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Exponentiation
*/
export function pow(a: int | uint, b: int | uint): int | uint {
return a ** b // TODO: handle overflow
}
/**
* @link https://docs.blockstack.org/references/language-clarity#print
*/
export function print<T>(value: T): T {
console.log(value)
return value
}
/**
* @link https://docs.blockstack.org/references/language-clarity#sha256
*/
export function sha256(value: buff | uint | int): buff {
return hash('sha256', value)
}
/**
* @link https://docs.blockstack.org/references/language-clarity#sha512
*/
export function sha512(value: buff | uint | int): buff {
return hash('sha512', value)
}
/**
* @link https://docs.blockstack.org/references/language-clarity#sha512256
*/
export function sha512_256(value: buff | uint | int): buff {
return hash('sha512-256', value)
}
/**
* @link https://docs.blockstack.org/references/language-clarity#some
*/
export function some<T>(value: T): optional<T> {
return value
}
/**
* @link https://docs.blockstack.org/references/language-clarity#to-int
*/
export function toInt(value: uint): int {
return value // TODO
}
/**
* @link https://docs.blockstack.org/references/language-clarity#to-uint
*/
export function toUint(value: int): uint {
return value // TODO
}
/**
* @link https://docs.blockstack.org/references/language-clarity#try
*/
export function tryUnwrap<A, B>(optionInput: optional<A> | response<A, B>): A | B | null {
if (isSome(optionInput) || isOk(optionInput)) {
return optionInput
}
if (isErr(optionInput)) {
return (optionInput as Err<B>).value // TODO: local exit
}
return none // TODO: local exit
}
/**
* @link https://docs.blockstack.org/references/language-clarity#tuple
*/
export function tuple(...pairs: Array<any>[]): tuple {
return pairs.reduce((tuple, [k, v]) => tuple.set(k, v), new Map())
}
/**
* @link https://docs.blockstack.org/references/language-clarity#tx-sender
*/
export function txSender(): principal {
if (SmartWeave) {
if (txSenderStack.length > 0) {
return txSenderStack[0] // see asContract()
}
return SmartWeave!.transaction.owner
}
throw new Error("tx-sender not supported")
}
/**
* @link https://docs.blockstack.org/references/language-clarity#unwrap
*/
export function unwrap<A, B>(optionInput: optional<A> | response<A, B>, thrownValue: A): A {
if (isNone(optionInput) || isErr(optionInput)) {
return thrownValue
}
return optionInput // TODO: local exit
}
/**
* @link https://docs.blockstack.org/references/language-clarity#unwrap-err
*/
export function unwrapErr<A, B>(responseInput: response<A, B>, thrownValue: B): B {
if (isErr(responseInput)) {
return (responseInput as Err<B>).value
}
return thrownValue // TODO: local exit
}
/**
* @link https://docs.blockstack.org/references/language-clarity#unwrap-err-panic
*/
export function unwrapErrPanic<A, B>(responseInput: response<A, B>): B {
if (isErr(responseInput)) {
return (responseInput as Err<B>).value
}
throw new Panic("unwrapErrPanic")
}
/**
* @link https://docs.blockstack.org/references/language-clarity#unwrap-panic
*/
export function unwrapPanic<A, B>(optionInput: optional<A> | response<A, B>): A {
if (isNone(optionInput) || isErr(optionInput)) {
throw new Panic("unwrapPanic")
}
return optionInput
}
/**
* @link https://docs.blockstack.org/references/language-clarity#xor
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_XOR
*/
export function xor(a: int | uint, b: int | uint): int | uint {
return a ^ b
}