@alephium/web3 0.41.0 → 0.43.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 (123) 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 +1 -2
  10. package/dist/src/codec/asset-output-codec.d.ts +3 -4
  11. package/dist/src/codec/asset-output-codec.js +10 -12
  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 +5 -5
  16. package/dist/src/codec/codec.d.ts +2 -3
  17. package/dist/src/codec/compact-int-codec.d.ts +12 -13
  18. package/dist/src/codec/compact-int-codec.js +22 -22
  19. package/dist/src/codec/contract-codec.d.ts +5 -6
  20. package/dist/src/codec/contract-codec.js +24 -24
  21. package/dist/src/codec/contract-output-codec.d.ts +2 -3
  22. package/dist/src/codec/contract-output-codec.js +8 -10
  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 +1 -2
  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 +1 -3
  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 +8 -95
  56. package/dist/src/contract/contract.js +31 -564
  57. package/dist/src/contract/ralph.d.ts +1 -1
  58. package/dist/src/contract/ralph.js +9 -10
  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/transaction/index.d.ts +1 -0
  67. package/dist/src/transaction/index.js +1 -0
  68. package/dist/src/transaction/utils.d.ts +2 -0
  69. package/dist/src/transaction/utils.js +34 -0
  70. package/dist/src/utils/index.d.ts +0 -2
  71. package/dist/src/utils/index.js +0 -7
  72. package/dist/src/utils/sign.js +1 -1
  73. package/dist/src/utils/utils.d.ts +1 -0
  74. package/dist/src/utils/utils.js +30 -11
  75. package/dist/src/utils/webcrypto.js +3 -4
  76. package/package.json +1 -2
  77. package/src/{utils → address}/address.ts +15 -19
  78. package/src/address/index.ts +19 -0
  79. package/src/api/node-provider.ts +2 -9
  80. package/src/api/types.ts +2 -2
  81. package/src/codec/array-codec.ts +3 -4
  82. package/src/codec/asset-output-codec.ts +16 -18
  83. package/src/codec/bigint-codec.ts +4 -5
  84. package/src/codec/bytestring-codec.ts +8 -8
  85. package/src/codec/codec.ts +2 -3
  86. package/src/codec/compact-int-codec.ts +36 -37
  87. package/src/codec/contract-codec.ts +28 -27
  88. package/src/codec/contract-output-codec.ts +11 -13
  89. package/src/codec/contract-output-ref-codec.ts +5 -5
  90. package/src/codec/either-codec.ts +3 -4
  91. package/src/codec/hash.ts +2 -3
  92. package/src/codec/input-codec.ts +10 -10
  93. package/src/codec/instr-codec.ts +3 -4
  94. package/src/codec/lockup-script-codec.ts +8 -9
  95. package/src/codec/long-codec.ts +3 -4
  96. package/src/codec/method-codec.ts +3 -4
  97. package/src/codec/option-codec.ts +4 -5
  98. package/src/codec/script-codec.ts +5 -7
  99. package/src/codec/signature-codec.ts +3 -4
  100. package/src/codec/signed-int-codec.ts +3 -4
  101. package/src/codec/token-codec.ts +6 -6
  102. package/src/codec/transaction-codec.ts +17 -18
  103. package/src/codec/unlock-script-codec.ts +26 -27
  104. package/src/codec/unsigned-tx-codec.ts +10 -11
  105. package/src/contract/contract.ts +18 -790
  106. package/src/contract/ralph.ts +10 -11
  107. package/src/{utils → exchange}/exchange.ts +3 -11
  108. package/src/exchange/index.ts +19 -0
  109. package/src/index.ts +2 -0
  110. package/src/signer/signer.ts +2 -3
  111. package/src/signer/tx-builder.ts +2 -2
  112. package/src/transaction/index.ts +1 -0
  113. package/src/transaction/utils.ts +38 -0
  114. package/src/utils/index.ts +0 -2
  115. package/src/utils/sign.ts +1 -1
  116. package/src/utils/utils.ts +29 -10
  117. package/src/utils/webcrypto.ts +3 -4
  118. package/webpack.config.js +1 -6
  119. package/dist/src/utils/error.d.ts +0 -15
  120. package/dist/src/utils/error.js +0 -66
  121. package/src/utils/error.ts +0 -77
  122. /package/dist/src/{utils → address}/address.d.ts +0 -0
  123. /package/dist/src/{utils → exchange}/exchange.d.ts +0 -0
@@ -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 { AssetInput } from '../api/api-alephium'
21
- import { binToHex } from '../utils'
20
+ import { binToHex, concatBytes, hexToBinUnsafe } from '../utils'
22
21
  import { UnlockScript, unlockScriptCodec } from './unlock-script-codec'
23
22
  import { Codec } from './codec'
24
23
  import { signedIntCodec } from './signed-int-codec'
@@ -27,7 +26,7 @@ import { ArrayCodec } from './array-codec'
27
26
  export interface Input {
28
27
  outputRef: {
29
28
  hint: number
30
- key: Buffer
29
+ key: Uint8Array
31
30
  }
32
31
  unlockScript: UnlockScript
33
32
  }
@@ -41,14 +40,15 @@ export class InputCodec implements Codec<Input> {
41
40
  type: unlockScriptCodec.parser
42
41
  })
43
42
 
44
- encode(input: Input): Buffer {
45
- return Buffer.concat([
46
- Buffer.from([...signedIntCodec.encode(input.outputRef.hint), ...input.outputRef.key]),
43
+ encode(input: Input): Uint8Array {
44
+ return concatBytes([
45
+ signedIntCodec.encode(input.outputRef.hint),
46
+ input.outputRef.key,
47
47
  unlockScriptCodec.encode(input.unlockScript)
48
48
  ])
49
49
  }
50
50
 
51
- decode(input: Buffer): Input {
51
+ decode(input: Uint8Array): Input {
52
52
  return this.parser.parse(input)
53
53
  }
54
54
 
@@ -59,7 +59,7 @@ export class InputCodec implements Codec<Input> {
59
59
  const unlockScript = unlockScriptCodec.encode(input.unlockScript)
60
60
  return {
61
61
  outputRef: { hint, key },
62
- unlockScript: unlockScript.toString('hex')
62
+ unlockScript: binToHex(unlockScript)
63
63
  }
64
64
  })
65
65
  }
@@ -67,8 +67,8 @@ export class InputCodec implements Codec<Input> {
67
67
  static fromAssetInputs(inputs: AssetInput[]): Input[] {
68
68
  return inputs.map((input) => {
69
69
  const hint = input.outputRef.hint
70
- const key = Buffer.from(input.outputRef.key, 'hex')
71
- const unlockScript = unlockScriptCodec.decode(Buffer.from(input.unlockScript, 'hex'))
70
+ const key = hexToBinUnsafe(input.outputRef.key)
71
+ const unlockScript = unlockScriptCodec.decode(hexToBinUnsafe(input.unlockScript))
72
72
  return {
73
73
  outputRef: { hint, key },
74
74
  unlockScript
@@ -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 { ArrayCodec, DecodedArray } from './array-codec'
21
20
  import { compactUnsignedIntCodec, compactSignedIntCodec, DecodedCompactInt } from './compact-int-codec'
@@ -458,7 +457,7 @@ export class InstrCodec implements Codec<Instr> {
458
457
  }
459
458
  })
460
459
 
461
- encode(instr: Instr): Buffer {
460
+ encode(instr: Instr): Uint8Array {
462
461
  const instrValue = instr.value
463
462
  const result = [instr.code]
464
463
  const instrsWithIndex = [0x00, 0x01, 0x16, 0x17, 0xa0, 0xa1, 0xce]
@@ -480,10 +479,10 @@ export class InstrCodec implements Codec<Instr> {
480
479
  result.push(...signedIntCodec.encode((instrValue as InstrValueWithIndex).index))
481
480
  }
482
481
 
483
- return Buffer.from(result)
482
+ return new Uint8Array(result)
484
483
  }
485
484
 
486
- decode(input: Buffer): Instr {
485
+ decode(input: Uint8Array): Instr {
487
486
  return this.parser.parse(input)
488
487
  }
489
488
  }
@@ -15,24 +15,23 @@ 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 { DecodedCompactInt, compactSignedIntCodec } from './compact-int-codec'
21
20
  import { Codec } from './codec'
22
21
  import { ArrayCodec, DecodedArray } from './array-codec'
23
22
 
24
23
  export interface PublicKeyHash {
25
- publicKeyHash: Buffer
24
+ publicKeyHash: Uint8Array
26
25
  }
27
26
 
28
27
  class PublicKeyHashCodec implements Codec<PublicKeyHash> {
29
28
  parser = Parser.start().buffer('publicKeyHash', { length: 32 })
30
29
 
31
- encode(input: PublicKeyHash): Buffer {
30
+ encode(input: PublicKeyHash): Uint8Array {
32
31
  return input.publicKeyHash
33
32
  }
34
33
 
35
- decode(input: Buffer): PublicKeyHash {
34
+ decode(input: Uint8Array): PublicKeyHash {
36
35
  return this.parser.parse(input)
37
36
  }
38
37
  }
@@ -48,11 +47,11 @@ export interface MultiSig {
48
47
  }
49
48
 
50
49
  export interface P2SH {
51
- scriptHash: Buffer
50
+ scriptHash: Uint8Array
52
51
  }
53
52
 
54
53
  export interface P2C {
55
- contractId: Buffer
54
+ contractId: Uint8Array
56
55
  }
57
56
 
58
57
  export interface LockupScript {
@@ -73,7 +72,7 @@ export class LockupScriptCodec implements Codec<LockupScript> {
73
72
  }
74
73
  })
75
74
 
76
- encode(input: LockupScript): Buffer {
75
+ encode(input: LockupScript): Uint8Array {
77
76
  const result: number[] = [input.scriptType]
78
77
  if (input.scriptType === 0) {
79
78
  result.push(...(input.script as PublicKeyHash).publicKeyHash)
@@ -88,10 +87,10 @@ export class LockupScriptCodec implements Codec<LockupScript> {
88
87
  throw new Error(`Unsupported script type: ${input.scriptType}`)
89
88
  }
90
89
 
91
- return Buffer.from(result)
90
+ return new Uint8Array(result)
92
91
  }
93
92
 
94
- decode(input: Buffer): LockupScript {
93
+ decode(input: Uint8Array): LockupScript {
95
94
  return this.parser.parse(input)
96
95
  }
97
96
  }
@@ -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,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 { ArrayCodec, DecodedArray } from './array-codec'
21
20
  import { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
@@ -95,16 +94,16 @@ export class MethodCodec implements Codec<DecodedMethod> {
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
99
  result.push(...compactUnsignedIntCodec.encode(input.argsLength))
101
100
  result.push(...compactUnsignedIntCodec.encode(input.localsLength))
102
101
  result.push(...compactUnsignedIntCodec.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
 
@@ -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,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
  import { Codec } from './codec'
@@ -37,22 +36,21 @@ 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 {
53
+ encodeScript(inputTxScript: Script): Uint8Array {
56
54
  const methodLength = compactUnsignedIntCodec.fromU32(inputTxScript.methods.length)
57
55
  const decodedMethods = inputTxScript.methods.map((method) => MethodCodec.fromMethod(method))
58
56
  return this.encode({ methods: { value: decodedMethods, length: methodLength } })
@@ -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
  }