@alephium/web3 1.4.0 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/dist/alephium-web3.min.js +1 -1
  2. package/dist/alephium-web3.min.js.map +1 -1
  3. package/dist/src/address/address.js +5 -5
  4. package/dist/src/api/api-alephium.d.ts +1 -1
  5. package/dist/src/api/api-alephium.js +1 -1
  6. package/dist/src/codec/array-codec.d.ts +4 -12
  7. package/dist/src/codec/array-codec.js +15 -28
  8. package/dist/src/codec/asset-output-codec.d.ts +6 -11
  9. package/dist/src/codec/asset-output-codec.js +32 -71
  10. package/dist/src/codec/bigint-codec.d.ts +2 -1
  11. package/dist/src/codec/bigint-codec.js +14 -2
  12. package/dist/src/codec/bytestring-codec.d.ts +6 -11
  13. package/dist/src/codec/bytestring-codec.js +9 -23
  14. package/dist/src/codec/codec.d.ts +54 -5
  15. package/dist/src/codec/codec.js +112 -14
  16. package/dist/src/codec/compact-int-codec.d.ts +65 -44
  17. package/dist/src/codec/compact-int-codec.js +222 -204
  18. package/dist/src/codec/contract-codec.d.ts +5 -8
  19. package/dist/src/codec/contract-codec.js +15 -29
  20. package/dist/src/codec/contract-output-codec.d.ts +4 -10
  21. package/dist/src/codec/contract-output-codec.js +20 -40
  22. package/dist/src/codec/contract-output-ref-codec.d.ts +2 -8
  23. package/dist/src/codec/contract-output-ref-codec.js +7 -17
  24. package/dist/src/codec/either-codec.d.ts +8 -15
  25. package/dist/src/codec/either-codec.js +5 -46
  26. package/dist/src/codec/index.d.ts +4 -3
  27. package/dist/src/codec/index.js +7 -4
  28. package/dist/src/codec/input-codec.d.ts +4 -10
  29. package/dist/src/codec/input-codec.js +11 -46
  30. package/dist/src/codec/instr-codec.d.ts +633 -40
  31. package/dist/src/codec/instr-codec.js +1040 -434
  32. package/dist/src/codec/int-as-4bytes-codec.d.ts +7 -0
  33. package/dist/src/codec/{signed-int-codec.js → int-as-4bytes-codec.js} +6 -12
  34. package/dist/src/codec/lockup-script-codec.d.ts +23 -26
  35. package/dist/src/codec/lockup-script-codec.js +12 -58
  36. package/dist/src/codec/method-codec.d.ts +6 -18
  37. package/dist/src/codec/method-codec.js +20 -48
  38. package/dist/src/codec/option-codec.d.ts +8 -13
  39. package/dist/src/codec/option-codec.js +14 -32
  40. package/dist/src/codec/output-codec.d.ts +2 -2
  41. package/dist/src/codec/output-codec.js +1 -1
  42. package/dist/src/codec/reader.d.ts +8 -0
  43. package/dist/src/codec/reader.js +48 -0
  44. package/dist/src/codec/script-codec.d.ts +6 -14
  45. package/dist/src/codec/script-codec.js +6 -22
  46. package/dist/src/codec/signature-codec.d.ts +4 -12
  47. package/dist/src/codec/signature-codec.js +3 -15
  48. package/dist/src/codec/timestamp-codec.d.ts +8 -0
  49. package/dist/src/codec/timestamp-codec.js +39 -0
  50. package/dist/src/codec/token-codec.d.ts +3 -10
  51. package/dist/src/codec/token-codec.js +6 -24
  52. package/dist/src/codec/transaction-codec.d.ts +6 -11
  53. package/dist/src/codec/transaction-codec.js +24 -49
  54. package/dist/src/codec/unlock-script-codec.d.ts +25 -36
  55. package/dist/src/codec/unlock-script-codec.js +26 -147
  56. package/dist/src/codec/unsigned-tx-codec.d.ts +8 -14
  57. package/dist/src/codec/unsigned-tx-codec.js +24 -66
  58. package/dist/src/codec/val.d.ts +27 -0
  59. package/dist/src/codec/val.js +33 -0
  60. package/dist/src/contract/contract.js +6 -6
  61. package/dist/src/contract/index.d.ts +1 -0
  62. package/dist/src/contract/index.js +1 -0
  63. package/dist/src/contract/ralph.d.ts +0 -4
  64. package/dist/src/contract/ralph.js +50 -179
  65. package/dist/src/contract/script-simulator.d.ts +27 -0
  66. package/dist/src/contract/script-simulator.js +757 -0
  67. package/dist/src/exchange/exchange.js +1 -1
  68. package/package.json +3 -4
  69. package/src/address/address.ts +8 -8
  70. package/src/api/api-alephium.ts +1 -1
  71. package/src/codec/array-codec.ts +16 -34
  72. package/src/codec/asset-output-codec.ts +38 -83
  73. package/src/codec/bigint-codec.ts +16 -2
  74. package/src/codec/bytestring-codec.ts +10 -28
  75. package/src/codec/codec.ts +121 -15
  76. package/src/codec/compact-int-codec.ts +230 -207
  77. package/src/codec/contract-codec.ts +20 -33
  78. package/src/codec/contract-output-codec.ts +22 -48
  79. package/src/codec/contract-output-ref-codec.ts +6 -17
  80. package/src/codec/either-codec.ts +4 -53
  81. package/src/codec/index.ts +4 -3
  82. package/src/codec/input-codec.ts +14 -36
  83. package/src/codec/instr-codec.ts +1229 -455
  84. package/src/codec/{signed-int-codec.ts → int-as-4bytes-codec.ts} +6 -10
  85. package/src/codec/lockup-script-codec.ts +28 -76
  86. package/src/codec/method-codec.ts +23 -61
  87. package/src/codec/option-codec.ts +13 -36
  88. package/src/codec/output-codec.ts +2 -2
  89. package/src/codec/reader.ts +56 -0
  90. package/src/codec/script-codec.ts +9 -31
  91. package/src/codec/signature-codec.ts +3 -18
  92. package/src/codec/timestamp-codec.ts +42 -0
  93. package/src/codec/token-codec.ts +7 -26
  94. package/src/codec/transaction-codec.ts +29 -58
  95. package/src/codec/unlock-script-codec.ts +44 -171
  96. package/src/codec/unsigned-tx-codec.ts +34 -63
  97. package/src/codec/val.ts +40 -0
  98. package/src/contract/contract.ts +9 -13
  99. package/src/contract/index.ts +1 -0
  100. package/src/contract/ralph.ts +76 -172
  101. package/src/contract/script-simulator.ts +838 -0
  102. package/src/exchange/exchange.ts +1 -1
  103. package/dist/src/codec/long-codec.d.ts +0 -8
  104. package/dist/src/codec/long-codec.js +0 -55
  105. package/dist/src/codec/signed-int-codec.d.ts +0 -8
  106. package/src/codec/long-codec.ts +0 -58
@@ -15,22 +15,18 @@ GNU Lesser General Public License for more details.
15
15
  You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
- import { Parser } from 'binary-parser'
19
- import { Codec, assert } from './codec'
20
-
21
- export class SignedIntCodec implements Codec<number> {
22
- parser = Parser.start().buffer('value', {
23
- length: 4
24
- })
18
+ import { Codec } from './codec'
19
+ import { Reader } from './reader'
25
20
 
21
+ export class IntAs4BytesCodec extends Codec<number> {
26
22
  encode(value: number): Uint8Array {
27
23
  return new Uint8Array([(value >> 24) & 0xff, (value >> 16) & 0xff, (value >> 8) & 0xff, value & 0xff])
28
24
  }
29
25
 
30
- decode(bytes: Uint8Array): number {
31
- assert(bytes.length === 4, 'Length should be 4')
26
+ _decode(input: Reader): number {
27
+ const bytes = input.consumeBytes(4)
32
28
  return ((bytes[0] & 0xff) << 24) | ((bytes[1] & 0xff) << 16) | ((bytes[2] & 0xff) << 8) | (bytes[3] & 0xff)
33
29
  }
34
30
  }
35
31
 
36
- export const signedIntCodec = new SignedIntCodec()
32
+ export const intAs4BytesCodec = new IntAs4BytesCodec()
@@ -15,84 +15,36 @@ GNU Lesser General Public License for more details.
15
15
  You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
- import { Parser } from 'binary-parser'
19
- import { DecodedCompactInt, compactSignedIntCodec } from './compact-int-codec'
20
- import { Codec, fixedSizeBytes } from './codec'
21
- import { ArrayCodec, DecodedArray } from './array-codec'
18
+ import { i32Codec } from './compact-int-codec'
19
+ import { byte32Codec, EnumCodec, ObjectCodec } from './codec'
20
+ import { ArrayCodec } from './array-codec'
22
21
 
23
- export interface PublicKeyHash {
24
- publicKeyHash: Uint8Array
25
- }
26
-
27
- class PublicKeyHashCodec implements Codec<PublicKeyHash> {
28
- parser = fixedSizeBytes('publicKeyHash', 32)
29
-
30
- encode(input: PublicKeyHash): Uint8Array {
31
- return input.publicKeyHash
32
- }
33
-
34
- decode(input: Uint8Array): PublicKeyHash {
35
- return this.parser.parse(input)
36
- }
37
- }
38
-
39
- const publicKeyHashCodec = new PublicKeyHashCodec()
40
- const publicKeyHashesCodec = new ArrayCodec(publicKeyHashCodec)
41
- const multiSigParser = Parser.start()
42
- .nest('publicKeyHashes', { type: publicKeyHashesCodec.parser })
43
- .nest('m', { type: compactSignedIntCodec.parser })
44
- export interface MultiSig {
45
- publicKeyHashes: DecodedArray<PublicKeyHash>
46
- m: DecodedCompactInt
47
- }
48
-
49
- export interface P2SH {
50
- scriptHash: Uint8Array
51
- }
52
-
53
- export interface P2C {
54
- contractId: Uint8Array
55
- }
56
-
57
- export interface LockupScript {
58
- scriptType: number
59
- script: PublicKeyHash | MultiSig | P2SH | P2C
60
- }
61
-
62
- export class LockupScriptCodec implements Codec<LockupScript> {
63
- parser = Parser.start()
64
- .uint8('scriptType')
65
- .choice('script', {
66
- tag: 'scriptType',
67
- choices: {
68
- 0: publicKeyHashCodec.parser,
69
- 1: multiSigParser,
70
- 2: Parser.start().buffer('scriptHash', { length: 32 }),
71
- 3: Parser.start().buffer('contractId', { length: 32 })
72
- }
73
- })
74
-
75
- encode(input: LockupScript): Uint8Array {
76
- const result: number[] = [input.scriptType]
77
- if (input.scriptType === 0) {
78
- result.push(...(input.script as PublicKeyHash).publicKeyHash)
79
- } else if (input.scriptType === 1) {
80
- result.push(...publicKeyHashesCodec.encode((input.script as MultiSig).publicKeyHashes.value))
81
- result.push(...compactSignedIntCodec.encode((input.script as MultiSig).m))
82
- } else if (input.scriptType === 2) {
83
- result.push(...(input.script as P2SH).scriptHash)
84
- } else if (input.scriptType === 3) {
85
- result.push(...(input.script as P2C).contractId)
86
- } else {
87
- throw new Error(`Unsupported script type: ${input.scriptType}`)
88
- }
22
+ export type PublicKeyHash = Uint8Array
23
+ export type P2PKH = Uint8Array
24
+ export type P2SH = Uint8Array
25
+ export type P2C = Uint8Array
89
26
 
90
- return new Uint8Array(result)
91
- }
27
+ export const p2cCodec = byte32Codec
92
28
 
93
- decode(input: Uint8Array): LockupScript {
94
- return this.parser.parse(input)
95
- }
29
+ export interface P2MPKH {
30
+ publicKeyHashes: PublicKeyHash[]
31
+ m: number
96
32
  }
97
33
 
98
- export const lockupScriptCodec = new LockupScriptCodec()
34
+ const p2mpkhCodec = new ObjectCodec<P2MPKH>({
35
+ publicKeyHashes: new ArrayCodec(byte32Codec),
36
+ m: i32Codec
37
+ })
38
+
39
+ export type LockupScript =
40
+ | { kind: 'P2PKH'; value: P2PKH }
41
+ | { kind: 'P2MPKH'; value: P2MPKH }
42
+ | { kind: 'P2SH'; value: P2SH }
43
+ | { kind: 'P2C'; value: P2C }
44
+
45
+ export const lockupScriptCodec = new EnumCodec<LockupScript>('lockup script', {
46
+ P2PKH: byte32Codec,
47
+ P2MPKH: p2mpkhCodec,
48
+ P2SH: byte32Codec,
49
+ P2C: byte32Codec
50
+ })
@@ -15,20 +15,12 @@ GNU Lesser General Public License for more details.
15
15
  You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
- import { Parser } from 'binary-parser'
19
- import { ArrayCodec, DecodedArray } from './array-codec'
20
- import { DecodedCompactInt, compactSignedIntCodec } from './compact-int-codec'
21
- import { Codec } from './codec'
18
+ import { ArrayCodec } from './array-codec'
19
+ import { i32Codec } from './compact-int-codec'
20
+ import { boolCodec, byteCodec, Codec } from './codec'
22
21
  import { instrsCodec, Instr } from './instr-codec'
23
-
24
- export interface DecodedMethod {
25
- isPublic: number
26
- assetModifier: number
27
- argsLength: DecodedCompactInt
28
- localsLength: DecodedCompactInt
29
- returnLength: DecodedCompactInt
30
- instrs: DecodedArray<Instr>
31
- }
22
+ import { Reader } from './reader'
23
+ import { concatBytes } from '../utils'
32
24
 
33
25
  export interface Method {
34
26
  isPublic: boolean
@@ -77,56 +69,26 @@ function encodeAssetModifier(arg: {
77
69
  return encoded | (arg.usePayToContractOnly ? 4 : 0)
78
70
  }
79
71
 
80
- export class MethodCodec implements Codec<DecodedMethod> {
81
- parser = Parser.start()
82
- .uint8('isPublic')
83
- .uint8('assetModifier')
84
- .nest('argsLength', {
85
- type: compactSignedIntCodec.parser
86
- })
87
- .nest('localsLength', {
88
- type: compactSignedIntCodec.parser
89
- })
90
- .nest('returnLength', {
91
- type: compactSignedIntCodec.parser
92
- })
93
- .nest('instrs', {
94
- type: instrsCodec.parser
95
- })
96
-
97
- encode(input: DecodedMethod): Uint8Array {
98
- const result = [input.isPublic, input.assetModifier]
99
- result.push(...compactSignedIntCodec.encode(input.argsLength))
100
- result.push(...compactSignedIntCodec.encode(input.localsLength))
101
- result.push(...compactSignedIntCodec.encode(input.returnLength))
102
- result.push(...instrsCodec.encode(input.instrs.value))
103
- return new Uint8Array(result)
104
- }
105
-
106
- decode(input: Uint8Array): DecodedMethod {
107
- return this.parser.parse(input)
108
- }
109
-
110
- static toMethod(decodedMethod: DecodedMethod): Method {
111
- return {
112
- isPublic: decodedMethod.isPublic === 1,
113
- ...decodeAssetModifier(decodedMethod.assetModifier),
114
- argsLength: compactSignedIntCodec.toI32(decodedMethod.argsLength),
115
- localsLength: compactSignedIntCodec.toI32(decodedMethod.localsLength),
116
- returnLength: compactSignedIntCodec.toI32(decodedMethod.returnLength),
117
- instrs: decodedMethod.instrs.value
118
- }
72
+ export class MethodCodec extends Codec<Method> {
73
+ encode(method: Method): Uint8Array {
74
+ const bytes: Uint8Array[] = []
75
+ bytes.push(boolCodec.encode(method.isPublic))
76
+ bytes.push(new Uint8Array([encodeAssetModifier(method)]))
77
+ bytes.push(i32Codec.encode(method.argsLength))
78
+ bytes.push(i32Codec.encode(method.localsLength))
79
+ bytes.push(i32Codec.encode(method.returnLength))
80
+ bytes.push(instrsCodec.encode(method.instrs))
81
+ return concatBytes(bytes)
119
82
  }
120
83
 
121
- static fromMethod(method: Method): DecodedMethod {
122
- return {
123
- isPublic: method.isPublic ? 1 : 0,
124
- assetModifier: encodeAssetModifier(method),
125
- argsLength: compactSignedIntCodec.fromI32(method.argsLength),
126
- localsLength: compactSignedIntCodec.fromI32(method.localsLength),
127
- returnLength: compactSignedIntCodec.fromI32(method.returnLength),
128
- instrs: instrsCodec.fromArray(method.instrs)
129
- }
84
+ _decode(input: Reader): Method {
85
+ const isPublic = boolCodec._decode(input)
86
+ const assetModifier = decodeAssetModifier(byteCodec._decode(input))
87
+ const argsLength = i32Codec._decode(input)
88
+ const localsLength = i32Codec._decode(input)
89
+ const returnLength = i32Codec._decode(input)
90
+ const instrs = instrsCodec._decode(input)
91
+ return { ...assetModifier, isPublic, argsLength, localsLength, returnLength, instrs }
130
92
  }
131
93
  }
132
94
 
@@ -15,45 +15,22 @@ GNU Lesser General Public License for more details.
15
15
  You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
- import { Parser } from 'binary-parser'
19
- import { Codec } from './codec'
18
+ import { Codec, EnumCodec } from './codec'
20
19
 
21
- export interface Option<T> {
22
- option: number
23
- value?: T
24
- }
25
- export class OptionCodec<T> implements Codec<Option<T>> {
26
- constructor(
27
- private childCodec: Codec<T>,
28
- public parser = new Parser().uint8('option').choice('value', {
29
- tag: 'option',
30
- choices: {
31
- 0: new Parser(),
32
- 1: childCodec.parser
33
- }
34
- })
35
- ) {}
20
+ export type Option<T> = { kind: 'None'; value: undefined } | { kind: 'Some'; value: T }
36
21
 
37
- encode(input: Option<T>): Uint8Array {
38
- const result = [input.option]
39
- if (input.option === 1) {
40
- result.push(...this.childCodec.encode(input.value!))
41
- }
42
- return new Uint8Array(result)
22
+ const undefinedCodec = new (class extends Codec<undefined> {
23
+ encode(): Uint8Array {
24
+ return new Uint8Array([])
43
25
  }
44
-
45
- decode(input: Uint8Array): Option<T> {
46
- const result = this.parser.parse(input)
47
- return {
48
- ...result,
49
- value: result.option ? this.childCodec.decode(result.value.value) : undefined
50
- }
26
+ _decode(): undefined {
27
+ return undefined
51
28
  }
29
+ })()
52
30
 
53
- fromBytes(input?: Uint8Array): Option<T> {
54
- return {
55
- option: input ? 1 : 0,
56
- value: input ? this.childCodec.decode(input) : undefined
57
- }
58
- }
31
+ export function option<T>(codec: Codec<T>): Codec<Option<T>> {
32
+ return new EnumCodec<Option<T>>('option', {
33
+ None: undefinedCodec,
34
+ Some: codec
35
+ })
59
36
  }
@@ -17,10 +17,10 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
19
  import { ArrayCodec } from './array-codec'
20
- import { Either, EitherCodec } from './either-codec'
20
+ import { either, Either } from './either-codec'
21
21
  import { AssetOutput, assetOutputCodec } from './asset-output-codec'
22
22
  import { ContractOutput, contractOutputCodec } from './contract-output-codec'
23
23
 
24
24
  export type Output = Either<AssetOutput, ContractOutput>
25
- export const outputCodec = new EitherCodec<AssetOutput, ContractOutput>(assetOutputCodec, contractOutputCodec)
25
+ export const outputCodec = either('output', assetOutputCodec, contractOutputCodec)
26
26
  export const outputsCodec = new ArrayCodec(outputCodec)
@@ -0,0 +1,56 @@
1
+ /*
2
+ Copyright 2018 - 2022 The Alephium Authors
3
+ This file is part of the alephium project.
4
+
5
+ The library is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ The library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+ export class Reader {
20
+ private index: number
21
+ private bytes: Uint8Array
22
+
23
+ constructor(bytes: Uint8Array) {
24
+ this.index = 0
25
+ this.bytes = bytes
26
+ }
27
+
28
+ consumeByte(): number {
29
+ if (this.index >= this.bytes.length) {
30
+ throw new Error(
31
+ `Index out of range: unable to consume byte at index ${this.index}, data length: ${this.bytes.length}`
32
+ )
33
+ }
34
+
35
+ const byte = this.bytes[`${this.index}`]
36
+ this.index += 1
37
+ return byte
38
+ }
39
+
40
+ consumeBytes(num: number): Uint8Array {
41
+ const from = this.index
42
+ const to = this.index + num
43
+ if (from > to || to > this.bytes.length) {
44
+ throw new Error(
45
+ `Index out of range: unable to consume bytes from index ${from} to ${to}, data length: ${this.bytes.length}`
46
+ )
47
+ }
48
+ const bytes = this.bytes.slice(from, to)
49
+ this.index = to
50
+ return bytes
51
+ }
52
+
53
+ consumeAll(): Uint8Array {
54
+ return this.consumeBytes(this.bytes.length - this.index)
55
+ }
56
+ }
@@ -16,46 +16,24 @@ You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
- import { Parser } from 'binary-parser'
20
- import { DecodedArray } from './array-codec'
21
19
  import { Codec } from './codec'
22
- import { DecodedMethod, methodsCodec, Method, MethodCodec } from './method-codec'
23
- import { OptionCodec } from './option-codec'
24
- import { compactSignedIntCodec } from './compact-int-codec'
25
-
26
- export interface DecodedScript {
27
- methods: DecodedArray<DecodedMethod>
28
- }
20
+ import { methodsCodec, Method } from './method-codec'
21
+ import { option } from './option-codec'
22
+ import { Reader } from './reader'
29
23
 
30
24
  export interface Script {
31
25
  methods: Method[]
32
26
  }
33
27
 
34
- export class ScriptCodec implements Codec<DecodedScript> {
35
- parser = Parser.start().nest('methods', {
36
- type: methodsCodec.parser
37
- })
38
-
39
- encode(input: DecodedScript): Uint8Array {
40
- return methodsCodec.encode(input.methods.value)
41
- }
42
-
43
- decode(input: Uint8Array): DecodedScript {
44
- return this.parser.parse(input)
45
- }
46
-
47
- decodeScript(input: Uint8Array): Script {
48
- const decodedTxScript = this.decode(input)
49
- const methods = decodedTxScript.methods.value.map((decodedMethod) => MethodCodec.toMethod(decodedMethod))
50
- return { methods }
28
+ export class ScriptCodec extends Codec<Script> {
29
+ encode(input: Script): Uint8Array {
30
+ return methodsCodec.encode(input.methods)
51
31
  }
52
32
 
53
- encodeScript(inputTxScript: Script): Uint8Array {
54
- const methodLength = compactSignedIntCodec.fromI32(inputTxScript.methods.length)
55
- const decodedMethods = inputTxScript.methods.map((method) => MethodCodec.fromMethod(method))
56
- return this.encode({ methods: { value: decodedMethods, length: methodLength } })
33
+ _decode(input: Reader): Script {
34
+ return { methods: methodsCodec._decode(input) }
57
35
  }
58
36
  }
59
37
 
60
38
  export const scriptCodec = new ScriptCodec()
61
- export const statefulScriptCodecOpt = new OptionCodec(scriptCodec)
39
+ export const statefulScriptCodecOpt = option(scriptCodec)
@@ -15,25 +15,10 @@ GNU Lesser General Public License for more details.
15
15
  You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
- import { Parser } from 'binary-parser'
19
18
  import { ArrayCodec } from './array-codec'
20
- import { Codec } from './codec'
19
+ import { FixedSizeCodec } from './codec'
21
20
 
22
- export interface Signature {
23
- value: Uint8Array
24
- }
21
+ export type Signature = Uint8Array
25
22
 
26
- export class SignatureCodec implements Codec<Signature> {
27
- parser = Parser.start().buffer('value', { length: 64 })
28
-
29
- encode(input: Signature): Uint8Array {
30
- return input.value
31
- }
32
-
33
- decode(input: Uint8Array): Signature {
34
- return this.parser.parse(input)
35
- }
36
- }
37
-
38
- export const signatureCodec = new SignatureCodec()
23
+ export const signatureCodec = new FixedSizeCodec(64)
39
24
  export const signaturesCodec = new ArrayCodec(signatureCodec)
@@ -0,0 +1,42 @@
1
+ /*
2
+ Copyright 2018 - 2022 The Alephium Authors
3
+ This file is part of the alephium project.
4
+
5
+ The library is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ The library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+ import { binToHex } from '../utils'
19
+ import { Codec, assert } from './codec'
20
+ import { Reader } from './reader'
21
+
22
+ export class TimestampCodec extends Codec<bigint> {
23
+ static max = 1n << 64n
24
+
25
+ encode(input: bigint): Uint8Array {
26
+ assert(input >= 0n && input < TimestampCodec.max, `Invalid timestamp: ${input}`)
27
+
28
+ const byteArray = new Uint8Array(8)
29
+ for (let index = 0; index < 8; index += 1) {
30
+ byteArray[`${index}`] = Number((input >> BigInt((7 - index) * 8)) & BigInt(0xff))
31
+ }
32
+
33
+ return byteArray
34
+ }
35
+
36
+ _decode(input: Reader): bigint {
37
+ const bytes = input.consumeBytes(8)
38
+ return BigInt(`0x${binToHex(bytes)}`)
39
+ }
40
+ }
41
+
42
+ export const timestampCodec = new TimestampCodec()
@@ -16,36 +16,17 @@ You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
- import { Parser } from 'binary-parser'
20
- import { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
21
- import { Codec } from './codec'
19
+ import { u256Codec } from './compact-int-codec'
20
+ import { byte32Codec, ObjectCodec } from './codec'
22
21
  import { ArrayCodec } from './array-codec'
23
- import { concatBytes } from '../utils'
24
22
 
25
23
  export interface Token {
26
24
  tokenId: Uint8Array
27
- amount: DecodedCompactInt
25
+ amount: bigint
28
26
  }
29
27
 
30
- export class TokenCodec implements Codec<Token> {
31
- parser = Parser.start()
32
- .buffer('tokenId', {
33
- length: 32
34
- })
35
- .nest('amount', {
36
- type: compactUnsignedIntCodec.parser
37
- })
38
-
39
- encode(input: Token): Uint8Array {
40
- const tokenId = input.tokenId
41
- const amount = compactUnsignedIntCodec.encode(input.amount)
42
- return concatBytes([tokenId, amount])
43
- }
44
-
45
- decode(input: Uint8Array): Token {
46
- return this.parser.parse(input)
47
- }
48
- }
49
-
50
- export const tokenCodec = new TokenCodec()
28
+ export const tokenCodec = new ObjectCodec<Token>({
29
+ tokenId: byte32Codec,
30
+ amount: u256Codec
31
+ })
51
32
  export const tokensCodec = new ArrayCodec(tokenCodec)