@alephium/web3 0.42.0 → 0.44.0

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 (114) 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/{utils → address}/address.js +15 -19
  4. package/dist/src/address/index.d.ts +1 -0
  5. package/dist/src/address/index.js +34 -0
  6. package/dist/src/api/node-provider.js +10 -9
  7. package/dist/src/api/types.js +1 -1
  8. package/dist/src/codec/array-codec.d.ts +2 -3
  9. package/dist/src/codec/array-codec.js +2 -3
  10. package/dist/src/codec/asset-output-codec.d.ts +3 -4
  11. package/dist/src/codec/asset-output-codec.js +13 -15
  12. package/dist/src/codec/bigint-codec.d.ts +2 -3
  13. package/dist/src/codec/bigint-codec.js +2 -3
  14. package/dist/src/codec/bytestring-codec.d.ts +5 -6
  15. package/dist/src/codec/bytestring-codec.js +7 -7
  16. package/dist/src/codec/codec.d.ts +2 -3
  17. package/dist/src/codec/compact-int-codec.d.ts +11 -15
  18. package/dist/src/codec/compact-int-codec.js +23 -33
  19. package/dist/src/codec/contract-codec.d.ts +5 -6
  20. package/dist/src/codec/contract-codec.js +26 -26
  21. package/dist/src/codec/contract-output-codec.d.ts +2 -3
  22. package/dist/src/codec/contract-output-codec.js +9 -11
  23. package/dist/src/codec/contract-output-ref-codec.d.ts +3 -4
  24. package/dist/src/codec/contract-output-ref-codec.js +2 -2
  25. package/dist/src/codec/either-codec.d.ts +2 -3
  26. package/dist/src/codec/either-codec.js +1 -2
  27. package/dist/src/codec/hash.d.ts +2 -3
  28. package/dist/src/codec/hash.js +17 -0
  29. package/dist/src/codec/input-codec.d.ts +3 -4
  30. package/dist/src/codec/input-codec.js +6 -6
  31. package/dist/src/codec/instr-codec.d.ts +2 -3
  32. package/dist/src/codec/instr-codec.js +1 -2
  33. package/dist/src/codec/lockup-script-codec.d.ts +5 -6
  34. package/dist/src/codec/lockup-script-codec.js +1 -2
  35. package/dist/src/codec/long-codec.d.ts +2 -3
  36. package/dist/src/codec/long-codec.js +1 -2
  37. package/dist/src/codec/method-codec.d.ts +2 -3
  38. package/dist/src/codec/method-codec.js +13 -14
  39. package/dist/src/codec/option-codec.d.ts +3 -4
  40. package/dist/src/codec/option-codec.js +2 -3
  41. package/dist/src/codec/script-codec.d.ts +4 -5
  42. package/dist/src/codec/script-codec.js +2 -4
  43. package/dist/src/codec/signature-codec.d.ts +3 -4
  44. package/dist/src/codec/signature-codec.js +17 -0
  45. package/dist/src/codec/signed-int-codec.d.ts +2 -3
  46. package/dist/src/codec/signed-int-codec.js +1 -2
  47. package/dist/src/codec/token-codec.d.ts +3 -4
  48. package/dist/src/codec/token-codec.js +3 -3
  49. package/dist/src/codec/transaction-codec.d.ts +4 -5
  50. package/dist/src/codec/transaction-codec.js +12 -13
  51. package/dist/src/codec/unlock-script-codec.d.ts +5 -6
  52. package/dist/src/codec/unlock-script-codec.js +14 -15
  53. package/dist/src/codec/unsigned-tx-codec.d.ts +4 -5
  54. package/dist/src/codec/unsigned-tx-codec.js +4 -5
  55. package/dist/src/contract/contract.d.ts +10 -1
  56. package/dist/src/contract/contract.js +197 -21
  57. package/dist/src/contract/ralph.d.ts +2 -1
  58. package/dist/src/contract/ralph.js +25 -11
  59. package/dist/src/{utils → exchange}/exchange.js +5 -5
  60. package/dist/src/exchange/index.d.ts +1 -0
  61. package/dist/src/exchange/index.js +25 -0
  62. package/dist/src/index.d.ts +2 -0
  63. package/dist/src/index.js +2 -0
  64. package/dist/src/signer/signer.js +4 -5
  65. package/dist/src/signer/tx-builder.js +3 -3
  66. package/dist/src/utils/index.d.ts +0 -2
  67. package/dist/src/utils/index.js +0 -7
  68. package/dist/src/utils/sign.js +1 -1
  69. package/dist/src/utils/utils.d.ts +1 -0
  70. package/dist/src/utils/utils.js +30 -11
  71. package/dist/src/utils/webcrypto.js +3 -4
  72. package/package.json +1 -2
  73. package/src/{utils → address}/address.ts +15 -19
  74. package/src/address/index.ts +19 -0
  75. package/src/api/node-provider.ts +2 -9
  76. package/src/api/types.ts +2 -2
  77. package/src/codec/array-codec.ts +5 -6
  78. package/src/codec/asset-output-codec.ts +20 -22
  79. package/src/codec/bigint-codec.ts +4 -5
  80. package/src/codec/bytestring-codec.ts +11 -11
  81. package/src/codec/codec.ts +2 -3
  82. package/src/codec/compact-int-codec.ts +36 -50
  83. package/src/codec/contract-codec.ts +30 -29
  84. package/src/codec/contract-output-codec.ts +13 -15
  85. package/src/codec/contract-output-ref-codec.ts +5 -5
  86. package/src/codec/either-codec.ts +3 -4
  87. package/src/codec/hash.ts +2 -3
  88. package/src/codec/input-codec.ts +10 -10
  89. package/src/codec/instr-codec.ts +3 -4
  90. package/src/codec/lockup-script-codec.ts +8 -9
  91. package/src/codec/long-codec.ts +3 -4
  92. package/src/codec/method-codec.ts +16 -17
  93. package/src/codec/option-codec.ts +4 -5
  94. package/src/codec/script-codec.ts +7 -9
  95. package/src/codec/signature-codec.ts +3 -4
  96. package/src/codec/signed-int-codec.ts +3 -4
  97. package/src/codec/token-codec.ts +6 -6
  98. package/src/codec/transaction-codec.ts +17 -18
  99. package/src/codec/unlock-script-codec.ts +26 -27
  100. package/src/codec/unsigned-tx-codec.ts +10 -11
  101. package/src/contract/contract.ts +274 -9
  102. package/src/contract/ralph.ts +29 -12
  103. package/src/{utils → exchange}/exchange.ts +3 -11
  104. package/src/exchange/index.ts +19 -0
  105. package/src/index.ts +2 -0
  106. package/src/signer/signer.ts +2 -3
  107. package/src/signer/tx-builder.ts +2 -2
  108. package/src/utils/index.ts +0 -2
  109. package/src/utils/sign.ts +1 -1
  110. package/src/utils/utils.ts +29 -10
  111. package/src/utils/webcrypto.ts +3 -4
  112. package/webpack.config.js +1 -5
  113. /package/dist/src/{utils → address}/address.d.ts +0 -0
  114. /package/dist/src/{utils → exchange}/exchange.d.ts +0 -0
@@ -20,10 +20,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
20
20
  return (mod && mod.__esModule) ? mod : { "default": mod };
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.assertType = exports.difficultyToTarget = exports.targetToDifficulty = exports.isDevnet = exports.sleep = exports.hexToString = exports.stringToHex = exports.blockChainIndex = exports.binToHex = exports.hexToBinUnsafe = exports.toNonNegativeBigInt = exports.isHexString = exports.signatureDecode = exports.encodeHexSignature = exports.encodeSignature = exports.networkIds = void 0;
23
+ exports.assertType = exports.concatBytes = exports.difficultyToTarget = exports.targetToDifficulty = exports.isDevnet = exports.sleep = exports.hexToString = exports.stringToHex = exports.blockChainIndex = exports.binToHex = exports.hexToBinUnsafe = exports.toNonNegativeBigInt = exports.isHexString = exports.signatureDecode = exports.encodeHexSignature = exports.encodeSignature = exports.networkIds = void 0;
24
24
  const elliptic_1 = require("elliptic");
25
25
  const bn_js_1 = __importDefault(require("bn.js"));
26
- const buffer_1 = require("buffer/");
27
26
  const constants_1 = require("../constants");
28
27
  exports.networkIds = ['mainnet', 'testnet', 'devnet'];
29
28
  const ec = new elliptic_1.ec('secp256k1');
@@ -72,11 +71,17 @@ function toNonNegativeBigInt(input) {
72
71
  }
73
72
  exports.toNonNegativeBigInt = toNonNegativeBigInt;
74
73
  function hexToBinUnsafe(hex) {
75
- return buffer_1.Buffer.from(hex, 'hex');
74
+ const bytes = [];
75
+ for (let i = 0; i < hex.length; i += 2) {
76
+ bytes.push(parseInt(hex.slice(i, i + 2), 16));
77
+ }
78
+ return new Uint8Array(bytes);
76
79
  }
77
80
  exports.hexToBinUnsafe = hexToBinUnsafe;
78
81
  function binToHex(bin) {
79
- return buffer_1.Buffer.from(bin).toString('hex');
82
+ return Array.from(bin)
83
+ .map((byte) => byte.toString(16).padStart(2, '0'))
84
+ .join('');
80
85
  }
81
86
  exports.binToHex = binToHex;
82
87
  function blockChainIndex(blockHash) {
@@ -99,7 +104,8 @@ function hexToString(str) {
99
104
  if (!isHexString(str)) {
100
105
  throw new Error(`Invalid hex string: ${str}`);
101
106
  }
102
- return buffer_1.Buffer.from(str, 'hex').toString();
107
+ const bytes = hexToBinUnsafe(str);
108
+ return new TextDecoder().decode(bytes);
103
109
  }
104
110
  exports.hexToString = hexToString;
105
111
  function sleep(ms) {
@@ -125,15 +131,28 @@ function difficultyToTarget(diff) {
125
131
  const maxBigInt = 1n << 256n;
126
132
  const target = diff === 1n ? maxBigInt - 1n : maxBigInt / diff;
127
133
  const size = Math.floor((target.toString(2).length + 7) / 8);
128
- const mantissa = size <= 3
134
+ const mantissa = Number(size <= 3
129
135
  ? BigInt.asIntN(32, target) << BigInt(8 * (3 - size))
130
- : BigInt.asIntN(32, target >> BigInt(8 * (size - 3)));
131
- const mantissaBytes = buffer_1.Buffer.alloc(4);
132
- mantissaBytes.writeInt32BE(Number(mantissa), 0);
133
- const bytes = new Uint8Array([size, ...mantissaBytes.slice(1)]);
134
- return binToHex(bytes);
136
+ : BigInt.asIntN(32, target >> BigInt(8 * (size - 3))));
137
+ const mantissaBytes = new Uint8Array(4);
138
+ mantissaBytes[0] = size;
139
+ mantissaBytes[1] = (mantissa >> 16) & 0xff;
140
+ mantissaBytes[2] = (mantissa >> 8) & 0xff;
141
+ mantissaBytes[3] = mantissa & 0xff;
142
+ return binToHex(mantissaBytes);
135
143
  }
136
144
  exports.difficultyToTarget = difficultyToTarget;
145
+ function concatBytes(arrays) {
146
+ const totalLength = arrays.reduce((acc, arr) => acc + arr.length, 0);
147
+ const result = new Uint8Array(totalLength);
148
+ let offset = 0;
149
+ for (const array of arrays) {
150
+ result.set(array, offset);
151
+ offset += array.length;
152
+ }
153
+ return result;
154
+ }
155
+ exports.concatBytes = concatBytes;
137
156
  // eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
138
157
  function assertType() { }
139
158
  exports.assertType = assertType;
@@ -18,7 +18,6 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
20
  exports.WebCrypto = void 0;
21
- const buffer_1 = require("buffer/");
22
21
  const crypto_1 = require("crypto");
23
22
  const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
24
23
  class WebCrypto {
@@ -29,12 +28,12 @@ class WebCrypto {
29
28
  if (!ArrayBuffer.isView(array)) {
30
29
  throw new TypeError("Failed to execute 'getRandomValues' on 'Crypto': parameter 1 is not of type 'ArrayBufferView'");
31
30
  }
32
- const buffer = buffer_1.Buffer.from(array.buffer, array.byteOffset, array.byteLength);
31
+ const bytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
33
32
  if (isBrowser) {
34
- globalThis.crypto.getRandomValues(buffer);
33
+ globalThis.crypto.getRandomValues(bytes);
35
34
  }
36
35
  else {
37
- (0, crypto_1.randomFillSync)(buffer);
36
+ (0, crypto_1.randomFillSync)(bytes);
38
37
  }
39
38
  return array;
40
39
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.42.0",
3
+ "version": "0.44.0",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
6
  "main": "dist/src/index.js",
@@ -37,7 +37,6 @@
37
37
  "bignumber.js": "^9.1.1",
38
38
  "blakejs": "1.2.1",
39
39
  "bn.js": "5.2.1",
40
- "buffer": "^6.0.3",
41
40
  "cross-fetch": "^3.1.5",
42
41
  "crypto-browserify": "^3.12.0",
43
42
  "elliptic": "6.5.4",
@@ -20,12 +20,11 @@ import { ec as EC } from 'elliptic'
20
20
  import BN from 'bn.js'
21
21
  import { TOTAL_NUMBER_OF_GROUPS } from '../constants'
22
22
  import blake from 'blakejs'
23
- import bs58 from './bs58'
24
- import djb2 from './djb2'
25
- import { binToHex, hexToBinUnsafe } from './utils'
23
+ import bs58 from '../utils/bs58'
24
+ import djb2 from '../utils/djb2'
25
+ import { binToHex, concatBytes, hexToBinUnsafe } from '../utils'
26
26
  import { KeyType } from '../signer'
27
27
  import { MultiSig, lockupScriptCodec } from '../codec/lockup-script-codec'
28
- import { Buffer } from 'buffer/'
29
28
  import { compactSignedIntCodec } from '../codec'
30
29
 
31
30
  const ec = new EC('secp256k1')
@@ -54,7 +53,7 @@ function decodeAndValidateAddress(address: string): Uint8Array {
54
53
  if (addressType === AddressType.P2MPKH) {
55
54
  let multisig: MultiSig
56
55
  try {
57
- multisig = lockupScriptCodec.decode(Buffer.from(decoded)).script as MultiSig
56
+ multisig = lockupScriptCodec.decode(decoded).script as MultiSig
58
57
  } catch (_) {
59
58
  throw new Error(`Invalid multisig address: ${address}`)
60
59
  }
@@ -163,26 +162,23 @@ export function addressFromPublicKey(publicKey: string, _keyType?: KeyType): str
163
162
  const keyType = _keyType ?? 'default'
164
163
 
165
164
  if (keyType === 'default') {
166
- const addressType = Buffer.from([AddressType.P2PKH])
167
- const hash = Buffer.from(blake.blake2b(Buffer.from(publicKey, 'hex'), undefined, 32))
168
- const bytes = Buffer.concat([addressType, hash])
165
+ const hash = blake.blake2b(hexToBinUnsafe(publicKey), undefined, 32)
166
+ const bytes = new Uint8Array([AddressType.P2PKH, ...hash])
169
167
  return bs58.encode(bytes)
170
168
  } else {
171
- const lockupScript = Buffer.from(`0101000000000458144020${publicKey}8685`, 'hex')
169
+ const lockupScript = hexToBinUnsafe(`0101000000000458144020${publicKey}8685`)
172
170
  return addressFromScript(lockupScript)
173
171
  }
174
172
  }
175
173
 
176
174
  export function addressFromScript(script: Uint8Array): string {
177
175
  const scriptHash = blake.blake2b(script, undefined, 32)
178
- const addressType = Buffer.from([AddressType.P2SH])
179
- return bs58.encode(Buffer.concat([addressType, scriptHash]))
176
+ return bs58.encode(new Uint8Array([AddressType.P2SH, ...scriptHash]))
180
177
  }
181
178
 
182
179
  export function addressFromContractId(contractId: string): string {
183
- const addressType = Buffer.from([AddressType.P2C])
184
- const hash = Buffer.from(hexToBinUnsafe(contractId))
185
- const bytes = Buffer.concat([addressType, hash])
180
+ const hash = hexToBinUnsafe(contractId)
181
+ const bytes = new Uint8Array([AddressType.P2C, ...hash])
186
182
  return bs58.encode(bytes)
187
183
  }
188
184
 
@@ -193,7 +189,7 @@ export function addressFromTokenId(tokenId: string): string {
193
189
 
194
190
  export function contractIdFromTx(txId: string, outputIndex: number): string {
195
191
  const txIdBin = hexToBinUnsafe(txId)
196
- const data = Buffer.concat([txIdBin, Buffer.from([outputIndex])])
192
+ const data = new Uint8Array([...txIdBin, outputIndex])
197
193
  const hash = blake.blake2b(data, undefined, 32)
198
194
  return binToHex(hash)
199
195
  }
@@ -202,10 +198,10 @@ export function subContractId(parentContractId: string, pathInHex: string, group
202
198
  if (group < 0 || group >= TOTAL_NUMBER_OF_GROUPS) {
203
199
  throw new Error(`Invalid group ${group}`)
204
200
  }
205
- const data = Buffer.concat([hexToBinUnsafe(parentContractId), hexToBinUnsafe(pathInHex)])
206
- const bytes = Buffer.concat([
207
- blake.blake2b(blake.blake2b(data, undefined, 32), undefined, 32).slice(0, -1),
208
- Buffer.from([group])
201
+ const data = concatBytes([hexToBinUnsafe(parentContractId), hexToBinUnsafe(pathInHex)])
202
+ const bytes = new Uint8Array([
203
+ ...blake.blake2b(blake.blake2b(data, undefined, 32), undefined, 32).slice(0, -1),
204
+ group
209
205
  ])
210
206
  return binToHex(bytes)
211
207
  }
@@ -0,0 +1,19 @@
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 * from './address'
@@ -28,15 +28,8 @@ import {
28
28
  requestWithLog
29
29
  } from './types'
30
30
  import { Api as NodeApi, CallContractFailed, CallContractSucceeded } from './api-alephium'
31
- import {
32
- HexString,
33
- addressFromContractId,
34
- addressFromTokenId,
35
- groupOfAddress,
36
- hexToString,
37
- isHexString,
38
- toNonNegativeBigInt
39
- } from '../utils'
31
+ import { HexString, hexToString, isHexString, toNonNegativeBigInt } from '../utils'
32
+ import { addressFromContractId, addressFromTokenId, groupOfAddress } from '../address'
40
33
  import * as node from '../api/api-alephium'
41
34
 
42
35
  function initializeNodeApi(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): NodeApi<string> {
package/src/api/types.ts CHANGED
@@ -18,7 +18,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
 
19
19
  import { ZERO_ADDRESS } from '../constants'
20
20
  import { isDebugModeEnabled } from '../debug'
21
- import { assertType, bs58, Eq, isBase58, isHexString } from '../utils'
21
+ import { assertType, binToHex, bs58, Eq, isBase58, isHexString } from '../utils'
22
22
  import * as node from './api-alephium'
23
23
 
24
24
  export type Number256 = bigint | string
@@ -93,7 +93,7 @@ export function toApiByteVec(v: Val): string {
93
93
  try {
94
94
  const address = bs58.decode(v)
95
95
  if (address.length == 33 && address[0] == 3) {
96
- return Buffer.from(address.slice(1)).toString('hex')
96
+ return binToHex(address.slice(1))
97
97
  }
98
98
  } catch (_) {
99
99
  throw new Error(`Invalid hex-string: ${v}`)
@@ -15,9 +15,8 @@ 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 { Buffer } from 'buffer/'
19
18
  import { Parser } from 'binary-parser'
20
- import { compactSignedIntCodec, compactUnsignedIntCodec, DecodedCompactInt } from './compact-int-codec'
19
+ import { compactSignedIntCodec, DecodedCompactInt } from './compact-int-codec'
21
20
  import { Codec } from './codec'
22
21
 
23
22
  export interface DecodedArray<T> {
@@ -28,15 +27,15 @@ export interface DecodedArray<T> {
28
27
  export class ArrayCodec<T> implements Codec<T[]> {
29
28
  constructor(private childCodec: Codec<T>, public parser = ArrayCodec.arrayParser(childCodec.parser)) {}
30
29
 
31
- encode(input: T[]): Buffer {
30
+ encode(input: T[]): Uint8Array {
32
31
  const result = [...compactSignedIntCodec.encodeI256(BigInt(input.length))]
33
32
  for (const element of input) {
34
33
  result.push(...this.childCodec.encode(element))
35
34
  }
36
- return Buffer.from(result)
35
+ return new Uint8Array(result)
37
36
  }
38
37
 
39
- decode(input: Buffer): T[] {
38
+ decode(input: Uint8Array): T[] {
40
39
  const result = this.parser.parse(input)
41
40
  return result.value.map((v) => this.childCodec.decode(v.value))
42
41
  }
@@ -56,7 +55,7 @@ export class ArrayCodec<T> implements Codec<T[]> {
56
55
 
57
56
  fromArray(inputs: T[]): DecodedArray<T> {
58
57
  return {
59
- length: compactUnsignedIntCodec.fromU32(inputs.length),
58
+ length: compactSignedIntCodec.fromI32(inputs.length),
60
59
  value: inputs
61
60
  }
62
61
  }
@@ -15,17 +15,16 @@ 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 { Buffer } from 'buffer/'
19
18
  import { Parser } from 'binary-parser'
20
19
  import { ArrayCodec, DecodedArray } from './array-codec'
21
- import { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
20
+ import { DecodedCompactInt, compactSignedIntCodec, compactUnsignedIntCodec } from './compact-int-codec'
22
21
  import { signedIntCodec } from './signed-int-codec'
23
22
  import { longCodec } from './long-codec'
24
23
  import { ByteString, byteStringCodec } from './bytestring-codec'
25
- import { LockupScript, MultiSig, P2C, P2SH, lockupScriptCodec } from './lockup-script-codec'
24
+ import { LockupScript, MultiSig, P2SH, lockupScriptCodec } from './lockup-script-codec'
26
25
  import { FixedAssetOutput } from '../api/api-alephium'
27
26
  import { blakeHash, createHint } from './hash'
28
- import { bs58, binToHex } from '../utils'
27
+ import { bs58, binToHex, hexToBinUnsafe, concatBytes } from '../utils'
29
28
  import { Codec } from './codec'
30
29
  import { PublicKeyHash } from './lockup-script-codec'
31
30
  import { Token, tokensCodec } from './token-codec'
@@ -33,7 +32,7 @@ import { Token, tokensCodec } from './token-codec'
33
32
  export interface AssetOutput {
34
33
  amount: DecodedCompactInt
35
34
  lockupScript: LockupScript
36
- lockTime: Buffer
35
+ lockTime: Uint8Array
37
36
  tokens: DecodedArray<Token>
38
37
  additionalData: ByteString
39
38
  }
@@ -41,7 +40,7 @@ export interface AssetOutput {
41
40
  export class AssetOutputCodec implements Codec<AssetOutput> {
42
41
  parser = Parser.start()
43
42
  .nest('amount', {
44
- type: compactUnsignedIntCodec.parser
43
+ type: compactSignedIntCodec.parser
45
44
  })
46
45
  .nest('lockupScript', {
47
46
  type: lockupScriptCodec.parser
@@ -56,17 +55,16 @@ export class AssetOutputCodec implements Codec<AssetOutput> {
56
55
  type: byteStringCodec.parser
57
56
  })
58
57
 
59
- encode(input: AssetOutput): Buffer {
60
- const amount = Buffer.from(compactUnsignedIntCodec.encode(input.amount))
58
+ encode(input: AssetOutput): Uint8Array {
59
+ const amount = compactUnsignedIntCodec.encode(input.amount)
61
60
  const lockupScript = lockupScriptCodec.encode(input.lockupScript)
62
- const lockTime = Buffer.from(input.lockTime)
63
- const tokens = Buffer.from(tokensCodec.encode(input.tokens.value))
64
- const additionalData = Buffer.from(byteStringCodec.encode(input.additionalData))
61
+ const tokens = tokensCodec.encode(input.tokens.value)
62
+ const additionalData = byteStringCodec.encode(input.additionalData)
65
63
 
66
- return Buffer.concat([amount, lockupScript, lockTime, tokens, additionalData])
64
+ return concatBytes([amount, lockupScript, input.lockTime, tokens, additionalData])
67
65
  }
68
66
 
69
- decode(input: Buffer): AssetOutput {
67
+ decode(input: Uint8Array): AssetOutput {
70
68
  return this.parser.parse(input)
71
69
  }
72
70
 
@@ -79,13 +77,13 @@ export class AssetOutputCodec implements Codec<AssetOutput> {
79
77
  const lockTime = Number(longCodec.decode(output.lockTime))
80
78
  const tokens = output.tokens.value.map((token) => {
81
79
  return {
82
- id: token.tokenId.toString('hex'),
80
+ id: binToHex(token.tokenId),
83
81
  amount: compactUnsignedIntCodec.toU256(token.amount).toString()
84
82
  }
85
83
  })
86
- const message = output.additionalData.value.toString('hex')
84
+ const message = binToHex(output.additionalData.value)
87
85
  const scriptType = output.lockupScript.scriptType
88
- const key = binToHex(blakeHash(Buffer.concat([txIdBytes, signedIntCodec.encode(index)])))
86
+ const key = binToHex(blakeHash(concatBytes([txIdBytes, signedIntCodec.encode(index)])))
89
87
  const outputLockupScript = output.lockupScript.script
90
88
  const address = bs58.encode(lockupScriptCodec.encode(output.lockupScript))
91
89
 
@@ -117,21 +115,21 @@ export class AssetOutputCodec implements Codec<AssetOutput> {
117
115
  static fromFixedAssetOutput(fixedOutput: FixedAssetOutput): AssetOutput {
118
116
  const amount: DecodedCompactInt = compactUnsignedIntCodec.fromU256(BigInt(fixedOutput.attoAlphAmount))
119
117
 
120
- const lockTime: Buffer = longCodec.encode(BigInt(fixedOutput.lockTime))
121
- const lockupScript: LockupScript = lockupScriptCodec.decode(Buffer.from(bs58.decode(fixedOutput.address)))
118
+ const lockTime = longCodec.encode(BigInt(fixedOutput.lockTime))
119
+ const lockupScript: LockupScript = lockupScriptCodec.decode(bs58.decode(fixedOutput.address))
122
120
  const tokensValue = fixedOutput.tokens.map((token) => {
123
121
  return {
124
- tokenId: Buffer.from(token.id, 'hex'),
122
+ tokenId: hexToBinUnsafe(token.id),
125
123
  amount: compactUnsignedIntCodec.fromU256(BigInt(token.amount))
126
124
  }
127
125
  })
128
126
  const tokens: DecodedArray<Token> = {
129
- length: compactUnsignedIntCodec.fromU32(tokensValue.length),
127
+ length: compactSignedIntCodec.fromI32(tokensValue.length),
130
128
  value: tokensValue
131
129
  }
132
- const additionalDataValue = Buffer.from(fixedOutput.message, 'hex')
130
+ const additionalDataValue = hexToBinUnsafe(fixedOutput.message)
133
131
  const additionalData: ByteString = {
134
- length: compactUnsignedIntCodec.fromU32(additionalDataValue.length),
132
+ length: compactSignedIntCodec.fromI32(additionalDataValue.length),
135
133
  value: additionalDataValue
136
134
  }
137
135
 
@@ -15,12 +15,11 @@ 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 { Buffer } from 'buffer/'
19
18
  export class BigIntCodec {
20
- static encode(value: bigint): Buffer {
19
+ static encode(value: bigint): Uint8Array {
21
20
  // Special case for zero.
22
21
  if (value === 0n) {
23
- return Buffer.from([0])
22
+ return new Uint8Array([0])
24
23
  }
25
24
 
26
25
  const isNegative = value < 0n
@@ -62,10 +61,10 @@ export class BigIntCodec {
62
61
  }
63
62
 
64
63
  // The byte array needs to be reversed since we've constructed it in little-endian order.
65
- return Buffer.from(new Uint8Array(bytes.reverse()))
64
+ return new Uint8Array(bytes.reverse())
66
65
  }
67
66
 
68
- static decode(encoded: Buffer, signed: boolean): bigint {
67
+ static decode(encoded: Uint8Array, signed: boolean): bigint {
69
68
  // Special case for zero.
70
69
  if (encoded.length === 1 && encoded[0] === 0) {
71
70
  return 0n
@@ -15,40 +15,40 @@ 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 { Buffer } from 'buffer/'
19
18
  import { Parser } from 'binary-parser'
20
- import { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
19
+ import { DecodedCompactInt, compactSignedIntCodec } from './compact-int-codec'
21
20
  import { Codec } from './codec'
21
+ import { concatBytes } from '../utils'
22
22
 
23
23
  export interface ByteString {
24
24
  length: DecodedCompactInt
25
- value: Buffer
25
+ value: Uint8Array
26
26
  }
27
27
 
28
28
  export class ByteStringCodec implements Codec<ByteString> {
29
29
  parser = new Parser()
30
30
  .nest('length', {
31
- type: compactUnsignedIntCodec.parser
31
+ type: compactSignedIntCodec.parser
32
32
  })
33
33
  .buffer('value', {
34
34
  length: function (ctx) {
35
- return compactUnsignedIntCodec.toU32(this['length']! as any as DecodedCompactInt)
35
+ return compactSignedIntCodec.toI32(this['length']! as any as DecodedCompactInt)
36
36
  }
37
37
  })
38
38
 
39
- encode(input: ByteString): Buffer {
40
- return Buffer.from([...compactUnsignedIntCodec.encode(input.length), ...input.value])
39
+ encode(input: ByteString): Uint8Array {
40
+ return concatBytes([compactSignedIntCodec.encode(input.length), input.value])
41
41
  }
42
42
 
43
- decode(input: Buffer): ByteString {
43
+ decode(input: Uint8Array): ByteString {
44
44
  return this.parser.parse(input)
45
45
  }
46
46
 
47
- encodeBuffer(input: Buffer): Buffer {
48
- return Buffer.from([...compactUnsignedIntCodec.encodeU32(input.length), ...input])
47
+ encodeBytes(input: Uint8Array): Uint8Array {
48
+ return concatBytes([compactSignedIntCodec.encodeI32(input.length), input])
49
49
  }
50
50
 
51
- decodeBuffer(input: Buffer): Buffer {
51
+ decodeBytes(input: Uint8Array): Uint8Array {
52
52
  return this.decode(input).value
53
53
  }
54
54
  }
@@ -15,13 +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 { Buffer } from 'buffer/'
19
18
  import { Parser } from 'binary-parser'
20
19
 
21
20
  export interface Codec<T> {
22
21
  parser: Parser
23
- encode(input: T): Buffer
24
- decode(input: Buffer): T
22
+ encode(input: T): Uint8Array
23
+ decode(input: Uint8Array): T
25
24
  }
26
25
 
27
26
  export function assert(value: boolean, message: string) {