@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
@@ -15,7 +15,6 @@ 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 { Codec, assert } from './codec'
21
20
 
@@ -24,7 +23,7 @@ export class LongCodec implements Codec<bigint> {
24
23
  length: 8
25
24
  })
26
25
 
27
- encode(input: bigint): Buffer {
26
+ encode(input: bigint): Uint8Array {
28
27
  const byteArray = new Uint8Array(8)
29
28
 
30
29
  assert(byteArray.length <= 8, 'Length should be less than or equal to 8')
@@ -34,10 +33,10 @@ export class LongCodec implements Codec<bigint> {
34
33
  input >>= BigInt(8)
35
34
  }
36
35
 
37
- return Buffer.from(byteArray)
36
+ return byteArray
38
37
  }
39
38
 
40
- decode(bytes: Buffer): bigint {
39
+ decode(bytes: Uint8Array): bigint {
41
40
  assert(bytes.length == 8, 'Length should be 8')
42
41
  let int64 = BigInt(0)
43
42
  let pow = BigInt(1)
@@ -15,10 +15,9 @@ 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 } from './compact-int-codec'
22
21
  import { Codec } from './codec'
23
22
  import { instrsCodec, Instr } from './instr-codec'
24
23
 
@@ -83,28 +82,28 @@ export class MethodCodec implements Codec<DecodedMethod> {
83
82
  .uint8('isPublic')
84
83
  .uint8('assetModifier')
85
84
  .nest('argsLength', {
86
- type: compactUnsignedIntCodec.parser
85
+ type: compactSignedIntCodec.parser
87
86
  })
88
87
  .nest('localsLength', {
89
- type: compactUnsignedIntCodec.parser
88
+ type: compactSignedIntCodec.parser
90
89
  })
91
90
  .nest('returnLength', {
92
- type: compactUnsignedIntCodec.parser
91
+ type: compactSignedIntCodec.parser
93
92
  })
94
93
  .nest('instrs', {
95
94
  type: instrsCodec.parser
96
95
  })
97
96
 
98
- encode(input: DecodedMethod): Buffer {
97
+ encode(input: DecodedMethod): Uint8Array {
99
98
  const result = [input.isPublic, input.assetModifier]
100
- result.push(...compactUnsignedIntCodec.encode(input.argsLength))
101
- result.push(...compactUnsignedIntCodec.encode(input.localsLength))
102
- result.push(...compactUnsignedIntCodec.encode(input.returnLength))
99
+ result.push(...compactSignedIntCodec.encode(input.argsLength))
100
+ result.push(...compactSignedIntCodec.encode(input.localsLength))
101
+ result.push(...compactSignedIntCodec.encode(input.returnLength))
103
102
  result.push(...instrsCodec.encode(input.instrs.value))
104
- return Buffer.from(result)
103
+ return new Uint8Array(result)
105
104
  }
106
105
 
107
- decode(input: Buffer): DecodedMethod {
106
+ decode(input: Uint8Array): DecodedMethod {
108
107
  return this.parser.parse(input)
109
108
  }
110
109
 
@@ -112,9 +111,9 @@ export class MethodCodec implements Codec<DecodedMethod> {
112
111
  return {
113
112
  isPublic: decodedMethod.isPublic === 1,
114
113
  ...decodeAssetModifier(decodedMethod.assetModifier),
115
- argsLength: compactUnsignedIntCodec.toU32(decodedMethod.argsLength),
116
- localsLength: compactUnsignedIntCodec.toU32(decodedMethod.localsLength),
117
- returnLength: compactUnsignedIntCodec.toU32(decodedMethod.returnLength),
114
+ argsLength: compactSignedIntCodec.toI32(decodedMethod.argsLength),
115
+ localsLength: compactSignedIntCodec.toI32(decodedMethod.localsLength),
116
+ returnLength: compactSignedIntCodec.toI32(decodedMethod.returnLength),
118
117
  instrs: decodedMethod.instrs.value
119
118
  }
120
119
  }
@@ -123,9 +122,9 @@ export class MethodCodec implements Codec<DecodedMethod> {
123
122
  return {
124
123
  isPublic: method.isPublic ? 1 : 0,
125
124
  assetModifier: encodeAssetModifier(method),
126
- argsLength: compactUnsignedIntCodec.fromU32(method.argsLength),
127
- localsLength: compactUnsignedIntCodec.fromU32(method.localsLength),
128
- returnLength: compactUnsignedIntCodec.fromU32(method.returnLength),
125
+ argsLength: compactSignedIntCodec.fromI32(method.argsLength),
126
+ localsLength: compactSignedIntCodec.fromI32(method.localsLength),
127
+ returnLength: compactSignedIntCodec.fromI32(method.returnLength),
129
128
  instrs: instrsCodec.fromArray(method.instrs)
130
129
  }
131
130
  }
@@ -15,7 +15,6 @@ 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 { Codec } from './codec'
21
20
 
@@ -35,15 +34,15 @@ export class OptionCodec<T> implements Codec<Option<T>> {
35
34
  })
36
35
  ) {}
37
36
 
38
- encode(input: Option<T>): Buffer {
37
+ encode(input: Option<T>): Uint8Array {
39
38
  const result = [input.option]
40
39
  if (input.option === 1) {
41
40
  result.push(...this.childCodec.encode(input.value!))
42
41
  }
43
- return Buffer.from(result)
42
+ return new Uint8Array(result)
44
43
  }
45
44
 
46
- decode(input: Buffer): Option<T> {
45
+ decode(input: Uint8Array): Option<T> {
47
46
  const result = this.parser.parse(input)
48
47
  return {
49
48
  ...result,
@@ -51,7 +50,7 @@ export class OptionCodec<T> implements Codec<Option<T>> {
51
50
  }
52
51
  }
53
52
 
54
- fromBuffer(input?: Buffer): Option<T> {
53
+ fromBytes(input?: Uint8Array): Option<T> {
55
54
  return {
56
55
  option: input ? 1 : 0,
57
56
  value: input ? this.childCodec.decode(input) : undefined
@@ -16,13 +16,12 @@ 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 { Buffer } from 'buffer/'
20
19
  import { Parser } from 'binary-parser'
21
20
  import { DecodedArray } from './array-codec'
22
21
  import { Codec } from './codec'
23
22
  import { DecodedMethod, methodsCodec, Method, MethodCodec } from './method-codec'
24
23
  import { OptionCodec } from './option-codec'
25
- import { compactUnsignedIntCodec } from './compact-int-codec'
24
+ import { compactSignedIntCodec } from './compact-int-codec'
26
25
 
27
26
  export interface DecodedScript {
28
27
  methods: DecodedArray<DecodedMethod>
@@ -37,23 +36,22 @@ export class ScriptCodec implements Codec<DecodedScript> {
37
36
  type: methodsCodec.parser
38
37
  })
39
38
 
40
- encode(input: DecodedScript): Buffer {
41
- const script = methodsCodec.encode(input.methods.value)
42
- return Buffer.from(script)
39
+ encode(input: DecodedScript): Uint8Array {
40
+ return methodsCodec.encode(input.methods.value)
43
41
  }
44
42
 
45
- decode(input: Buffer): DecodedScript {
43
+ decode(input: Uint8Array): DecodedScript {
46
44
  return this.parser.parse(input)
47
45
  }
48
46
 
49
- decodeScript(input: Buffer): Script {
47
+ decodeScript(input: Uint8Array): Script {
50
48
  const decodedTxScript = this.decode(input)
51
49
  const methods = decodedTxScript.methods.value.map((decodedMethod) => MethodCodec.toMethod(decodedMethod))
52
50
  return { methods }
53
51
  }
54
52
 
55
- encodeScript(inputTxScript: Script): Buffer {
56
- const methodLength = compactUnsignedIntCodec.fromU32(inputTxScript.methods.length)
53
+ encodeScript(inputTxScript: Script): Uint8Array {
54
+ const methodLength = compactSignedIntCodec.fromI32(inputTxScript.methods.length)
57
55
  const decodedMethods = inputTxScript.methods.map((method) => MethodCodec.fromMethod(method))
58
56
  return this.encode({ methods: { value: decodedMethods, length: methodLength } })
59
57
  }
@@ -15,23 +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 { Buffer } from 'buffer/'
19
18
  import { Parser } from 'binary-parser'
20
19
  import { ArrayCodec } from './array-codec'
21
20
  import { Codec } from './codec'
22
21
 
23
22
  export interface Signature {
24
- value: Buffer
23
+ value: Uint8Array
25
24
  }
26
25
 
27
26
  export class SignatureCodec implements Codec<Signature> {
28
27
  parser = Parser.start().buffer('value', { length: 64 })
29
28
 
30
- encode(input: Signature): Buffer {
29
+ encode(input: Signature): Uint8Array {
31
30
  return input.value
32
31
  }
33
32
 
34
- decode(input: Buffer): Signature {
33
+ decode(input: Uint8Array): Signature {
35
34
  return this.parser.parse(input)
36
35
  }
37
36
  }
@@ -18,17 +18,16 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  import { Parser } from 'binary-parser'
19
19
  import { Codec, assert } from './codec'
20
20
 
21
- import { Buffer } from 'buffer/'
22
21
  export class SignedIntCodec implements Codec<number> {
23
22
  parser = Parser.start().buffer('value', {
24
23
  length: 4
25
24
  })
26
25
 
27
- encode(value: number): Buffer {
28
- return Buffer.from([(value >> 24) & 0xff, (value >> 16) & 0xff, (value >> 8) & 0xff, value & 0xff])
26
+ encode(value: number): Uint8Array {
27
+ return new Uint8Array([(value >> 24) & 0xff, (value >> 16) & 0xff, (value >> 8) & 0xff, value & 0xff])
29
28
  }
30
29
 
31
- decode(bytes: Buffer): number {
30
+ decode(bytes: Uint8Array): number {
32
31
  assert(bytes.length === 4, 'Length should be 4')
33
32
  return ((bytes[0] & 0xff) << 24) | ((bytes[1] & 0xff) << 16) | ((bytes[2] & 0xff) << 8) | (bytes[3] & 0xff)
34
33
  }
@@ -16,14 +16,14 @@ 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 { Buffer } from 'buffer/'
20
19
  import { Parser } from 'binary-parser'
21
20
  import { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
22
21
  import { Codec } from './codec'
23
22
  import { ArrayCodec } from './array-codec'
23
+ import { concatBytes } from '../utils'
24
24
 
25
25
  export interface Token {
26
- tokenId: Buffer
26
+ tokenId: Uint8Array
27
27
  amount: DecodedCompactInt
28
28
  }
29
29
 
@@ -36,13 +36,13 @@ export class TokenCodec implements Codec<Token> {
36
36
  type: compactUnsignedIntCodec.parser
37
37
  })
38
38
 
39
- encode(input: Token): Buffer {
39
+ encode(input: Token): Uint8Array {
40
40
  const tokenId = input.tokenId
41
- const amount = Buffer.from(compactUnsignedIntCodec.encode(input.amount))
42
- return Buffer.concat([tokenId, amount])
41
+ const amount = compactUnsignedIntCodec.encode(input.amount)
42
+ return concatBytes([tokenId, amount])
43
43
  }
44
44
 
45
- decode(input: Buffer): Token {
45
+ decode(input: Uint8Array): Token {
46
46
  return this.parser.parse(input)
47
47
  }
48
48
  }
@@ -16,7 +16,6 @@ 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 { Buffer } from 'buffer/'
20
19
  import { Parser } from 'binary-parser'
21
20
  import { DecodedArray } from './array-codec'
22
21
 
@@ -27,7 +26,7 @@ import { Either } from './either-codec'
27
26
  import { AssetOutput, AssetOutputCodec } from './asset-output-codec'
28
27
  import { ContractOutput, ContractOutputCodec } from './contract-output-codec'
29
28
  import { FixedAssetOutput, Transaction as ApiTransaction } from '../api/api-alephium'
30
- import { hexToBinUnsafe } from '../utils'
29
+ import { binToHex, hexToBinUnsafe, concatBytes } from '../utils'
31
30
  import { ContractOutput as ApiContractOutput } from '../api/api-alephium'
32
31
  import { Codec } from './codec'
33
32
  import { Output, outputCodec, outputsCodec } from './output-codec'
@@ -60,27 +59,27 @@ export class TransactionCodec implements Codec<Transaction> {
60
59
  type: signaturesCodec.parser
61
60
  })
62
61
 
63
- encode(decodedTx: Transaction): Buffer {
64
- return Buffer.concat([
62
+ encode(decodedTx: Transaction): Uint8Array {
63
+ return concatBytes([
65
64
  unsignedTxCodec.encode(decodedTx.unsigned),
66
- Buffer.from([decodedTx.scriptExecutionOk]),
67
- Buffer.from([...contractOutputRefsCodec.encode(decodedTx.contractInputs.value)]),
68
- Buffer.from([...outputsCodec.encode(decodedTx.generatedOutputs.value)]),
69
- Buffer.from([...signaturesCodec.encode(decodedTx.inputSignatures.value)]),
70
- Buffer.from([...signaturesCodec.encode(decodedTx.scriptSignatures.value)])
65
+ new Uint8Array([decodedTx.scriptExecutionOk]),
66
+ contractOutputRefsCodec.encode(decodedTx.contractInputs.value),
67
+ outputsCodec.encode(decodedTx.generatedOutputs.value),
68
+ signaturesCodec.encode(decodedTx.inputSignatures.value),
69
+ signaturesCodec.encode(decodedTx.scriptSignatures.value)
71
70
  ])
72
71
  }
73
72
 
74
- decode(input: Buffer): Transaction {
73
+ decode(input: Uint8Array): Transaction {
75
74
  return this.parser.parse(input)
76
75
  }
77
76
 
78
- encodeApiTransaction(input: ApiTransaction): Buffer {
77
+ encodeApiTransaction(input: ApiTransaction): Uint8Array {
79
78
  const decodedTx = TransactionCodec.fromApiTransaction(input)
80
79
  return this.encode(decodedTx)
81
80
  }
82
81
 
83
- decodeApiTransaction(input: Buffer): ApiTransaction {
82
+ decodeApiTransaction(input: Uint8Array): ApiTransaction {
84
83
  const decodedTx = this.parser.parse(input)
85
84
  return TransactionCodec.toApiTransaction(decodedTx)
86
85
  }
@@ -91,7 +90,7 @@ export class TransactionCodec implements Codec<Transaction> {
91
90
  const scriptExecutionOk = !!transaction.scriptExecutionOk
92
91
  const contractInputs = transaction.contractInputs.value.map((contractInput) => {
93
92
  const hint = contractInput.hint
94
- const key = contractInput.key.toString('hex')
93
+ const key = binToHex(contractInput.key)
95
94
  return { hint, key }
96
95
  })
97
96
  const txIdBytes = hexToBinUnsafe(txId)
@@ -104,8 +103,8 @@ export class TransactionCodec implements Codec<Transaction> {
104
103
  }
105
104
  })
106
105
 
107
- const inputSignatures = transaction.inputSignatures.value.map((signature) => signature.value.toString('hex'))
108
- const scriptSignatures = transaction.scriptSignatures.value.map((signature) => signature.value.toString('hex'))
106
+ const inputSignatures = transaction.inputSignatures.value.map((signature) => binToHex(signature.value))
107
+ const scriptSignatures = transaction.scriptSignatures.value.map((signature) => binToHex(signature.value))
109
108
 
110
109
  return { unsigned, scriptExecutionOk, contractInputs, generatedOutputs, inputSignatures, scriptSignatures }
111
110
  }
@@ -114,7 +113,7 @@ export class TransactionCodec implements Codec<Transaction> {
114
113
  const unsigned = UnsignedTxCodec.fromApiUnsignedTx(tx.unsigned)
115
114
  const scriptExecutionOk = tx.scriptExecutionOk ? 1 : 0
116
115
  const contractInputs: ContractOutputRef[] = tx.contractInputs.map((contractInput) => {
117
- return { hint: contractInput.hint, key: Buffer.from(contractInput.key, 'hex') }
116
+ return { hint: contractInput.hint, key: hexToBinUnsafe(contractInput.key) }
118
117
  })
119
118
  const generatedOutputs: Either<AssetOutput, ContractOutput>[] = tx.generatedOutputs.map((output) => {
120
119
  if (output.type === 'AssetOutput') {
@@ -127,10 +126,10 @@ export class TransactionCodec implements Codec<Transaction> {
127
126
  })
128
127
 
129
128
  const inputSignatures: Signature[] = tx.inputSignatures.map((signature) => {
130
- return { value: Buffer.from(signature, 'hex') }
129
+ return { value: hexToBinUnsafe(signature) }
131
130
  })
132
131
  const scriptSignatures: Signature[] = tx.scriptSignatures.map((signature) => {
133
- return { value: Buffer.from(signature, 'hex') }
132
+ return { value: hexToBinUnsafe(signature) }
134
133
  })
135
134
 
136
135
  return {
@@ -15,27 +15,27 @@ 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
20
  import { compactUnsignedIntCodec, compactSignedIntCodec, DecodedCompactInt } from './compact-int-codec'
22
21
  import { Codec } from './codec'
23
- import { DecodedScript, Script, scriptCodec } from './script-codec'
22
+ import { DecodedScript, scriptCodec } from './script-codec'
24
23
  import { ByteString, byteStringCodec } from './bytestring-codec'
25
24
  import { LockupScript, lockupScriptCodec } from './lockup-script-codec'
25
+ import { concatBytes } from '../utils'
26
26
 
27
27
  export interface P2PKH {
28
- publicKey: Buffer
28
+ publicKey: Uint8Array
29
29
  }
30
30
 
31
31
  class P2PKHCodec implements Codec<P2PKH> {
32
32
  parser = Parser.start().buffer('publicKey', { length: 33 })
33
33
 
34
- encode(input: P2PKH): Buffer {
34
+ encode(input: P2PKH): Uint8Array {
35
35
  return input.publicKey
36
36
  }
37
37
 
38
- decode(input: Buffer): P2PKH {
38
+ decode(input: Uint8Array): P2PKH {
39
39
  return this.parser.parse(input)
40
40
  }
41
41
  }
@@ -57,16 +57,16 @@ class P2MPKHCodec implements Codec<P2MPKH> {
57
57
  )
58
58
  })
59
59
 
60
- encode(input: P2MPKH): Buffer {
61
- return Buffer.concat([
62
- Buffer.from(compactUnsignedIntCodec.encode(input.publicKeys.length)),
60
+ encode(input: P2MPKH): Uint8Array {
61
+ return concatBytes([
62
+ compactUnsignedIntCodec.encode(input.publicKeys.length),
63
63
  ...input.publicKeys.value.map((v) => {
64
- return Buffer.concat([v.publicKey.publicKey, Buffer.from(compactUnsignedIntCodec.encode(v.index))])
64
+ return concatBytes([v.publicKey.publicKey, compactUnsignedIntCodec.encode(v.index)])
65
65
  })
66
66
  ])
67
67
  }
68
68
 
69
- decode(input: Buffer): any {
69
+ decode(input: Uint8Array): any {
70
70
  return this.parser.parse(input)
71
71
  }
72
72
  }
@@ -90,30 +90,30 @@ class ValCodec implements Codec<Val> {
90
90
  }
91
91
  })
92
92
 
93
- encode(input: Val): Buffer {
93
+ encode(input: Val): Uint8Array {
94
94
  const valType = input.type
95
95
 
96
96
  if (valType === 0x00) {
97
97
  // Boolean
98
- return Buffer.from([valType, input.val as number])
98
+ return new Uint8Array([valType, input.val as number])
99
99
  } else if (valType === 0x01) {
100
100
  // I256
101
- return Buffer.from([valType, ...compactUnsignedIntCodec.encode(input.val as DecodedCompactInt)])
101
+ return new Uint8Array([valType, ...compactUnsignedIntCodec.encode(input.val as DecodedCompactInt)])
102
102
  } else if (valType === 0x02) {
103
103
  // U256
104
- return Buffer.from([valType, ...compactUnsignedIntCodec.encode(input.val as DecodedCompactInt)])
104
+ return new Uint8Array([valType, ...compactUnsignedIntCodec.encode(input.val as DecodedCompactInt)])
105
105
  } else if (valType === 0x03) {
106
106
  // ByteVec
107
- return Buffer.from([valType, ...byteStringCodec.encode(input.val as ByteString)])
107
+ return new Uint8Array([valType, ...byteStringCodec.encode(input.val as ByteString)])
108
108
  } else if (valType === 0x04) {
109
109
  // Address
110
- return Buffer.from([valType, ...lockupScriptCodec.encode(input.val as LockupScript)])
110
+ return new Uint8Array([valType, ...lockupScriptCodec.encode(input.val as LockupScript)])
111
111
  } else {
112
112
  throw new Error(`ValCodec: unsupported val type: ${valType}`)
113
113
  }
114
114
  }
115
115
 
116
- decode(input: Buffer): Val {
116
+ decode(input: Uint8Array): Val {
117
117
  return this.parser.parse(input)
118
118
  }
119
119
  }
@@ -135,11 +135,11 @@ export class P2SHCodec implements Codec<P2SH> {
135
135
  type: valsCodec.parser
136
136
  })
137
137
 
138
- encode(input: P2SH): Buffer {
139
- return Buffer.concat([scriptCodec.encode(input.script), valsCodec.encode(input.params.value)])
138
+ encode(input: P2SH): Uint8Array {
139
+ return concatBytes([scriptCodec.encode(input.script), valsCodec.encode(input.params.value)])
140
140
  }
141
141
 
142
- decode(input: Buffer): P2SH {
142
+ decode(input: Uint8Array): P2SH {
143
143
  return this.parser.parse(input)
144
144
  }
145
145
  }
@@ -164,29 +164,28 @@ export class UnlockScriptCodec implements Codec<UnlockScript> {
164
164
  }
165
165
  })
166
166
 
167
- encode(input: UnlockScript): Buffer {
167
+ encode(input: UnlockScript): Uint8Array {
168
168
  const scriptType = input.scriptType
169
169
  const inputUnLockScript = input.script
170
- const inputUnLockScriptType = Buffer.from([scriptType])
171
170
 
172
171
  if (scriptType === 0) {
173
172
  // P2PKH
174
- return Buffer.concat([inputUnLockScriptType, p2pkhCodec.encode(inputUnLockScript as P2PKH)])
173
+ return new Uint8Array([scriptType, ...p2pkhCodec.encode(inputUnLockScript as P2PKH)])
175
174
  } else if (scriptType === 1) {
176
175
  // P2MPKH
177
- return Buffer.concat([inputUnLockScriptType, p2mpkhCodec.encode(inputUnLockScript as P2MPKH)])
176
+ return new Uint8Array([scriptType, ...p2mpkhCodec.encode(inputUnLockScript as P2MPKH)])
178
177
  } else if (scriptType === 2) {
179
178
  // P2SH
180
- return Buffer.concat([inputUnLockScriptType, p2shCodec.encode(input.script as P2SH)])
179
+ return new Uint8Array([scriptType, ...p2shCodec.encode(inputUnLockScript as P2SH)])
181
180
  } else if (scriptType === 3) {
182
181
  // SameAsPrevious
183
- return inputUnLockScriptType
182
+ return new Uint8Array([scriptType])
184
183
  } else {
185
184
  throw new Error(`TODO: encode unlock script: ${scriptType}`)
186
185
  }
187
186
  }
188
187
 
189
- decode(input: Buffer): UnlockScript {
188
+ decode(input: Uint8Array): UnlockScript {
190
189
  return this.parser.parse(input)
191
190
  }
192
191
  }
@@ -15,10 +15,9 @@ 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 { UnsignedTx as ApiUnsignedTx } from '../api/api-alephium'
21
- import { binToHex, hexToBinUnsafe } from '../utils'
20
+ import { binToHex, concatBytes, hexToBinUnsafe } from '../utils'
22
21
  import { DecodedScript, scriptCodec, statefulScriptCodecOpt } from './script-codec'
23
22
  import { Option } from './option-codec'
24
23
  import { DecodedCompactInt, compactSignedIntCodec, compactUnsignedIntCodec } from './compact-int-codec'
@@ -58,9 +57,9 @@ export class UnsignedTxCodec implements Codec<UnsignedTx> {
58
57
  type: assetOutputsCodec.parser
59
58
  })
60
59
 
61
- encode(decodedUnsignedTx: UnsignedTx): Buffer {
62
- return Buffer.concat([
63
- Buffer.from([decodedUnsignedTx.version, decodedUnsignedTx.networkId]),
60
+ encode(decodedUnsignedTx: UnsignedTx): Uint8Array {
61
+ return concatBytes([
62
+ new Uint8Array([decodedUnsignedTx.version, decodedUnsignedTx.networkId]),
64
63
  statefulScriptCodecOpt.encode(decodedUnsignedTx.statefulScript),
65
64
  compactSignedIntCodec.encode(decodedUnsignedTx.gasAmount),
66
65
  compactUnsignedIntCodec.encode(decodedUnsignedTx.gasPrice),
@@ -69,16 +68,16 @@ export class UnsignedTxCodec implements Codec<UnsignedTx> {
69
68
  ])
70
69
  }
71
70
 
72
- decode(input: Buffer): UnsignedTx {
71
+ decode(input: Uint8Array): UnsignedTx {
73
72
  return this.parser.parse(input)
74
73
  }
75
74
 
76
- encodeApiUnsignedTx(input: ApiUnsignedTx): Buffer {
75
+ encodeApiUnsignedTx(input: ApiUnsignedTx): Uint8Array {
77
76
  const decoded = UnsignedTxCodec.fromApiUnsignedTx(input)
78
77
  return this.encode(decoded)
79
78
  }
80
79
 
81
- decodeApiUnsignedTx(input: Buffer): ApiUnsignedTx {
80
+ decodeApiUnsignedTx(input: Uint8Array): ApiUnsignedTx {
82
81
  const decoded = this.parser.parse(input)
83
82
  return UnsignedTxCodec.toApiUnsignedTx(decoded)
84
83
  }
@@ -98,7 +97,7 @@ export class UnsignedTxCodec implements Codec<UnsignedTx> {
98
97
  const fixedOutputs = AssetOutputCodec.toFixedAssetOutputs(txIdBytes, unsigned.fixedOutputs.value)
99
98
  let scriptOpt: string | undefined = undefined
100
99
  if (unsigned.statefulScript.option === 1) {
101
- scriptOpt = scriptCodec.encode(unsigned.statefulScript.value!).toString('hex')
100
+ scriptOpt = binToHex(scriptCodec.encode(unsigned.statefulScript.value!))
102
101
  }
103
102
 
104
103
  return { txId, version, networkId, gasAmount, scriptOpt, gasPrice, inputs, fixedOutputs }
@@ -113,8 +112,8 @@ export class UnsignedTxCodec implements Codec<UnsignedTx> {
113
112
  const inputs = inputsCodec.fromArray(inputsValue)
114
113
  const fixedOutputsValue = AssetOutputCodec.fromFixedAssetOutputs(unsignedTx.fixedOutputs)
115
114
  const fixedOutputs = assetOutputsCodec.fromArray(fixedOutputsValue)
116
- const statefulScript = statefulScriptCodecOpt.fromBuffer(
117
- unsignedTx.scriptOpt ? Buffer.from(unsignedTx.scriptOpt, 'hex') : undefined
115
+ const statefulScript = statefulScriptCodecOpt.fromBytes(
116
+ unsignedTx.scriptOpt ? hexToBinUnsafe(unsignedTx.scriptOpt) : undefined
118
117
  )
119
118
 
120
119
  return { version, networkId, gasAmount, gasPrice, inputs, fixedOutputs, statefulScript }