@alephium/web3 0.42.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 (113) 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.js +20 -20
  56. package/dist/src/contract/ralph.d.ts +1 -1
  57. package/dist/src/contract/ralph.js +9 -10
  58. package/dist/src/{utils → exchange}/exchange.js +5 -5
  59. package/dist/src/exchange/index.d.ts +1 -0
  60. package/dist/src/exchange/index.js +25 -0
  61. package/dist/src/index.d.ts +2 -0
  62. package/dist/src/index.js +2 -0
  63. package/dist/src/signer/signer.js +4 -5
  64. package/dist/src/signer/tx-builder.js +3 -3
  65. package/dist/src/utils/index.d.ts +0 -2
  66. package/dist/src/utils/index.js +0 -7
  67. package/dist/src/utils/sign.js +1 -1
  68. package/dist/src/utils/utils.d.ts +1 -0
  69. package/dist/src/utils/utils.js +30 -11
  70. package/dist/src/utils/webcrypto.js +3 -4
  71. package/package.json +1 -2
  72. package/src/{utils → address}/address.ts +15 -19
  73. package/src/address/index.ts +19 -0
  74. package/src/api/node-provider.ts +2 -9
  75. package/src/api/types.ts +2 -2
  76. package/src/codec/array-codec.ts +3 -4
  77. package/src/codec/asset-output-codec.ts +16 -18
  78. package/src/codec/bigint-codec.ts +4 -5
  79. package/src/codec/bytestring-codec.ts +8 -8
  80. package/src/codec/codec.ts +2 -3
  81. package/src/codec/compact-int-codec.ts +36 -37
  82. package/src/codec/contract-codec.ts +28 -27
  83. package/src/codec/contract-output-codec.ts +11 -13
  84. package/src/codec/contract-output-ref-codec.ts +5 -5
  85. package/src/codec/either-codec.ts +3 -4
  86. package/src/codec/hash.ts +2 -3
  87. package/src/codec/input-codec.ts +10 -10
  88. package/src/codec/instr-codec.ts +3 -4
  89. package/src/codec/lockup-script-codec.ts +8 -9
  90. package/src/codec/long-codec.ts +3 -4
  91. package/src/codec/method-codec.ts +3 -4
  92. package/src/codec/option-codec.ts +4 -5
  93. package/src/codec/script-codec.ts +5 -7
  94. package/src/codec/signature-codec.ts +3 -4
  95. package/src/codec/signed-int-codec.ts +3 -4
  96. package/src/codec/token-codec.ts +6 -6
  97. package/src/codec/transaction-codec.ts +17 -18
  98. package/src/codec/unlock-script-codec.ts +26 -27
  99. package/src/codec/unsigned-tx-codec.ts +10 -11
  100. package/src/contract/contract.ts +3 -7
  101. package/src/contract/ralph.ts +10 -11
  102. package/src/{utils → exchange}/exchange.ts +3 -11
  103. package/src/exchange/index.ts +19 -0
  104. package/src/index.ts +2 -0
  105. package/src/signer/signer.ts +2 -3
  106. package/src/signer/tx-builder.ts +2 -2
  107. package/src/utils/index.ts +0 -2
  108. package/src/utils/sign.ts +1 -1
  109. package/src/utils/utils.ts +29 -10
  110. package/src/utils/webcrypto.ts +3 -4
  111. package/webpack.config.js +1 -5
  112. /package/dist/src/{utils → address}/address.d.ts +0 -0
  113. /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 { 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
  }
@@ -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 }
@@ -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 { promises as fsPromises } from 'fs'
21
20
  import {
22
21
  fromApiNumber256,
@@ -47,19 +46,16 @@ import * as ralph from './ralph'
47
46
  import {
48
47
  bs58,
49
48
  binToHex,
50
- contractIdFromAddress,
51
49
  Subscription,
52
50
  assertType,
53
51
  Eq,
54
52
  Optional,
55
- groupOfAddress,
56
53
  WebCrypto,
57
54
  hexToBinUnsafe,
58
55
  isDevnet,
59
- addressFromContractId,
60
- subContractId,
61
56
  HexString
62
57
  } from '../utils'
58
+ import { contractIdFromAddress, groupOfAddress, addressFromContractId, subContractId } from '../address'
63
59
  import { getCurrentNodeProvider } from '../global'
64
60
  import { EventSubscribeOptions, EventSubscription, subscribeToEvents } from './events'
65
61
  import { ONE_ALPH, TOTAL_NUMBER_OF_GROUPS } from '../constants'
@@ -220,7 +216,7 @@ export class Contract extends Artifact {
220
216
  this.bytecodeDebug = ralph.buildDebugBytecode(this.bytecode, this.bytecodeDebugPatch)
221
217
  this.codeHashDebug = codeHashDebug
222
218
 
223
- this.decodedMethods = contract.contractCodec.decodeContract(Buffer.from(bytecode, 'hex')).methods
219
+ this.decodedMethods = contract.contractCodec.decodeContract(hexToBinUnsafe(bytecode)).methods
224
220
  }
225
221
 
226
222
  publicFunctions(): FunctionSig[] {
@@ -1208,7 +1204,7 @@ function genCodeForType(type: string, structs: Struct[]): { bytecode: string; co
1208
1204
  fieldLength: immFields + mutFields + 1, // parentContractId
1209
1205
  methods: [loadImmFieldByIndex, loadMutFieldByIndex, storeMutFieldByIndex, destroy]
1210
1206
  }
1211
- const bytecode = contract.contractCodec.encode(contract.toHalfDecoded(c))
1207
+ const bytecode = contract.contractCodec.encodeContract(c)
1212
1208
  const codeHash = blake.blake2b(bytecode, undefined, 32)
1213
1209
  return { bytecode: binToHex(bytecode), codeHash: binToHex(codeHash) }
1214
1210
  }
@@ -16,9 +16,8 @@ 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 { Val, decodeArrayType, toApiAddress, toApiBoolean, toApiByteVec, toApiNumber256 } from '../api'
21
- import { HexString, binToHex, bs58, hexToBinUnsafe, isHexString } from '../utils'
20
+ import { HexString, binToHex, bs58, concatBytes, hexToBinUnsafe, isHexString } from '../utils'
22
21
  import { Fields, FieldsSig, Struct } from './contract'
23
22
  import { compactSignedIntCodec, compactUnsignedIntCodec } from '../codec'
24
23
 
@@ -151,14 +150,14 @@ export function encodeU256(u256: bigint): Uint8Array {
151
150
  }
152
151
  }
153
152
 
154
- export function encodeByteVec(bytes: string): Uint8Array {
155
- if (!isHexString(bytes)) {
156
- throw Error(`Given value ${bytes} is not a valid hex string`)
153
+ export function encodeByteVec(hex: string): Uint8Array {
154
+ if (!isHexString(hex)) {
155
+ throw Error(`Given value ${hex} is not a valid hex string`)
157
156
  }
158
157
 
159
- const buffer0 = Buffer.from(bytes, 'hex')
160
- const buffer1 = Buffer.from(encodeI256(BigInt(buffer0.length)))
161
- return Buffer.concat([buffer1, buffer0])
158
+ const bytes0 = hexToBinUnsafe(hex)
159
+ const bytes1 = encodeI256(BigInt(bytes0.length))
160
+ return concatBytes([bytes1, bytes0])
162
161
  }
163
162
 
164
163
  export function encodeAddress(address: string): Uint8Array {
@@ -254,7 +253,7 @@ function encodeScriptFieldU256(value: bigint): Uint8Array {
254
253
  }
255
254
 
256
255
  export function encodeScriptFieldAsString(tpe: string, value: Val): string {
257
- return Buffer.from(encodeScriptField(tpe, value)).toString('hex')
256
+ return binToHex(encodeScriptField(tpe, value))
258
257
  }
259
258
 
260
259
  export function encodeScriptField(tpe: string, value: Val): Uint8Array {
@@ -378,9 +377,9 @@ export function decodePrimitive(value: Uint8Array, type: string): Val {
378
377
  case 'Bool':
379
378
  return decodeBool(value)
380
379
  case 'I256':
381
- return compactSignedIntCodec.decodeI256(Buffer.from(value))
380
+ return compactSignedIntCodec.decodeI256(value)
382
381
  case 'U256':
383
- return compactUnsignedIntCodec.decodeU256(Buffer.from(value))
382
+ return compactUnsignedIntCodec.decodeU256(value)
384
383
  case 'ByteVec':
385
384
  return binToHex(value)
386
385
  case 'Address':
@@ -16,19 +16,11 @@ 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 {
20
- AddressType,
21
- addressFromPublicKey,
22
- addressFromScript,
23
- binToHex,
24
- bs58,
25
- hexToBinUnsafe,
26
- isHexString
27
- } from '../utils'
19
+ import { AddressType, addressFromPublicKey, addressFromScript } from '../address'
20
+ import { binToHex, bs58, hexToBinUnsafe, isHexString } from '../utils'
28
21
  import { Transaction } from '../api/api-alephium'
29
22
  import { Address } from '../signer'
30
23
  import { P2SH, unlockScriptCodec } from '../codec/unlock-script-codec'
31
- import { Buffer } from 'buffer/'
32
24
  import { scriptCodec } from '../codec/script-codec'
33
25
 
34
26
  export function validateExchangeAddress(address: string) {
@@ -113,7 +105,7 @@ export function getAddressFromUnlockScript(unlockScript: string): Address {
113
105
  if (unlockScriptType === UnlockScriptType.P2SH) {
114
106
  let p2sh: P2SH
115
107
  try {
116
- p2sh = unlockScriptCodec.decode(Buffer.from(decoded)).script as P2SH
108
+ p2sh = unlockScriptCodec.decode(decoded).script as P2SH
117
109
  } catch (_) {
118
110
  throw new Error(`Invalid p2sh unlock script: ${unlockScript}`)
119
111
  }
@@ -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 { validateExchangeAddress, isALPHTransferTx, getSenderAddress, getALPHDepositInfo } from './exchange'
package/src/index.ts CHANGED
@@ -33,3 +33,5 @@ export * as codec from './codec'
33
33
  export * as utils from './utils'
34
34
  export * from './debug'
35
35
  export * from './block'
36
+ export * from './address'
37
+ export * from './exchange'