@alephium/web3 1.1.1-rc.0 → 1.1.2

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.
@@ -5,3 +5,4 @@ export interface Codec<T> {
5
5
  decode(input: Uint8Array): T;
6
6
  }
7
7
  export declare function assert(value: boolean, message: string): void;
8
+ export declare function fixedSizeBytes(name: string, length: number): Parser;
@@ -1,9 +1,40 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.assert = void 0;
3
+ exports.fixedSizeBytes = exports.assert = void 0;
4
+ /*
5
+ Copyright 2018 - 2022 The Alephium Authors
6
+ This file is part of the alephium project.
7
+
8
+ The library is free software: you can redistribute it and/or modify
9
+ it under the terms of the GNU Lesser General Public License as published by
10
+ the Free Software Foundation, either version 3 of the License, or
11
+ (at your option) any later version.
12
+
13
+ The library is distributed in the hope that it will be useful,
14
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ GNU Lesser General Public License for more details.
17
+
18
+ You should have received a copy of the GNU Lesser General Public License
19
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
20
+ */
21
+ const binary_parser_1 = require("binary-parser");
4
22
  function assert(value, message) {
5
23
  if (!value) {
6
24
  throw new Error(message);
7
25
  }
8
26
  }
9
27
  exports.assert = assert;
28
+ function fixedSizeBytes(name, length) {
29
+ return binary_parser_1.Parser.start().wrapped({
30
+ length,
31
+ type: binary_parser_1.Parser.start().buffer(name, { length }),
32
+ wrapper: function (result) {
33
+ if (result.length === length) {
34
+ return result;
35
+ }
36
+ throw new Error(`Too few bytes when parsing ${name}, expected ${length}, got ${result.length}`);
37
+ }
38
+ });
39
+ }
40
+ exports.fixedSizeBytes = fixedSizeBytes;
@@ -20,10 +20,11 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
20
20
  */
21
21
  const binary_parser_1 = require("binary-parser");
22
22
  const compact_int_codec_1 = require("./compact-int-codec");
23
+ const codec_1 = require("./codec");
23
24
  const array_codec_1 = require("./array-codec");
24
25
  class PublicKeyHashCodec {
25
26
  constructor() {
26
- this.parser = binary_parser_1.Parser.start().buffer('publicKeyHash', { length: 32 });
27
+ this.parser = (0, codec_1.fixedSizeBytes)('publicKeyHash', 32);
27
28
  }
28
29
  encode(input) {
29
30
  return input.publicKeyHash;
@@ -21,13 +21,14 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
21
21
  const binary_parser_1 = require("binary-parser");
22
22
  const array_codec_1 = require("./array-codec");
23
23
  const compact_int_codec_1 = require("./compact-int-codec");
24
+ const codec_1 = require("./codec");
24
25
  const script_codec_1 = require("./script-codec");
25
26
  const bytestring_codec_1 = require("./bytestring-codec");
26
27
  const lockup_script_codec_1 = require("./lockup-script-codec");
27
28
  const utils_1 = require("../utils");
28
29
  class P2PKHCodec {
29
30
  constructor() {
30
- this.parser = binary_parser_1.Parser.start().buffer('publicKey', { length: 33 });
31
+ this.parser = (0, codec_1.fixedSizeBytes)('publicKey', 33);
31
32
  }
32
33
  encode(input) {
33
34
  return input.publicKey;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "1.1.1-rc.0",
3
+ "version": "1.1.2",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
6
  "main": "dist/src/index.js",
@@ -28,3 +28,16 @@ export function assert(value: boolean, message: string) {
28
28
  throw new Error(message)
29
29
  }
30
30
  }
31
+
32
+ export function fixedSizeBytes(name: string, length: number): Parser {
33
+ return Parser.start().wrapped({
34
+ length,
35
+ type: Parser.start().buffer(name, { length }),
36
+ wrapper: function (result) {
37
+ if (result.length === length) {
38
+ return result
39
+ }
40
+ throw new Error(`Too few bytes when parsing ${name}, expected ${length}, got ${result.length}`)
41
+ }
42
+ })
43
+ }
@@ -19,7 +19,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
19
19
  import { Parser } from 'binary-parser'
20
20
  import { ArrayCodec, DecodedArray } from './array-codec'
21
21
  import { Codec } from './codec'
22
- import { compactSignedIntCodec, compactUnsignedIntCodec, DecodedCompactInt } from './compact-int-codec'
22
+ import { compactSignedIntCodec, DecodedCompactInt } from './compact-int-codec'
23
23
  import { Method, MethodCodec, methodCodec } from './method-codec'
24
24
  import { concatBytes } from '../utils'
25
25
 
@@ -17,7 +17,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
  import { Parser } from 'binary-parser'
19
19
  import { DecodedCompactInt, compactSignedIntCodec } from './compact-int-codec'
20
- import { Codec } from './codec'
20
+ import { Codec, fixedSizeBytes } from './codec'
21
21
  import { ArrayCodec, DecodedArray } from './array-codec'
22
22
 
23
23
  export interface PublicKeyHash {
@@ -25,7 +25,7 @@ export interface PublicKeyHash {
25
25
  }
26
26
 
27
27
  class PublicKeyHashCodec implements Codec<PublicKeyHash> {
28
- parser = Parser.start().buffer('publicKeyHash', { length: 32 })
28
+ parser = fixedSizeBytes('publicKeyHash', 32)
29
29
 
30
30
  encode(input: PublicKeyHash): Uint8Array {
31
31
  return input.publicKeyHash
@@ -18,7 +18,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  import { Parser } from 'binary-parser'
19
19
  import { ArrayCodec, DecodedArray } from './array-codec'
20
20
  import { compactUnsignedIntCodec, compactSignedIntCodec, DecodedCompactInt } from './compact-int-codec'
21
- import { Codec } from './codec'
21
+ import { Codec, fixedSizeBytes } from './codec'
22
22
  import { DecodedScript, scriptCodec } from './script-codec'
23
23
  import { ByteString, byteStringCodec } from './bytestring-codec'
24
24
  import { LockupScript, lockupScriptCodec } from './lockup-script-codec'
@@ -29,7 +29,7 @@ export interface P2PKH {
29
29
  }
30
30
 
31
31
  class P2PKHCodec implements Codec<P2PKH> {
32
- parser = Parser.start().buffer('publicKey', { length: 33 })
32
+ parser = fixedSizeBytes('publicKey', 33)
33
33
 
34
34
  encode(input: P2PKH): Uint8Array {
35
35
  return input.publicKey