@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
@@ -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 { createHash } from 'crypto'
21
20
  import { ExplorerProvider, fromApiNumber256, fromApiTokens, NodeProvider, toApiNumber256, toApiTokens } from '../api'
22
21
  import { node } from '../api'
@@ -44,7 +43,7 @@ import {
44
43
  MessageHasher
45
44
  } from './types'
46
45
  import { TransactionBuilder } from './tx-builder'
47
- import { addressFromPublicKey, groupOfAddress } from '../utils'
46
+ import { addressFromPublicKey, groupOfAddress } from '../address'
48
47
 
49
48
  export abstract class SignerProvider {
50
49
  abstract get nodeProvider(): NodeProvider | undefined
@@ -254,7 +253,7 @@ export function hashMessage(message: string, hasher: MessageHasher): string {
254
253
  return utils.binToHex(blake.blake2b(extendMessage(message), undefined, 32))
255
254
  case 'sha256':
256
255
  const sha256 = createHash('sha256')
257
- sha256.update(Buffer.from(message))
256
+ sha256.update(new TextEncoder().encode(message))
258
257
  return utils.binToHex(sha256.digest())
259
258
  case 'blake2b':
260
259
  return utils.binToHex(blake.blake2b(message, undefined, 32))
@@ -16,9 +16,9 @@ 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 { binToHex, contractIdFromAddress } from '../utils'
19
+ import { binToHex } from '../utils'
20
20
  import { fromApiNumber256, node, NodeProvider, toApiNumber256Optional, toApiTokens } from '../api'
21
- import { addressFromPublicKey } from '../utils'
21
+ import { addressFromPublicKey, contractIdFromAddress } from '../address'
22
22
  import { toApiDestinations } from './signer'
23
23
  import {
24
24
  KeyType,
@@ -17,11 +17,9 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
19
  export * from './webcrypto'
20
- export * from './address'
21
20
  export * from './bs58'
22
21
  export * from './djb2'
23
22
  export * from './utils'
24
23
  export * from './subscription'
25
24
  export * from './sign'
26
25
  export * from './number'
27
- export { validateExchangeAddress, isALPHTransferTx, getSenderAddress, getALPHDepositInfo } from './exchange'
package/src/utils/sign.ts CHANGED
@@ -31,7 +31,7 @@ necc.utils.sha256Sync = (...messages: Uint8Array[]): Uint8Array => {
31
31
  }
32
32
 
33
33
  necc.utils.hmacSha256Sync = (key: Uint8Array, ...messages: Uint8Array[]): Uint8Array => {
34
- const hash = createHmac('sha256', Buffer.from(key))
34
+ const hash = createHmac('sha256', key)
35
35
  messages.forEach((m) => hash.update(m))
36
36
  return Uint8Array.from(hash.digest())
37
37
  }
@@ -18,8 +18,6 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
 
19
19
  import { ec as EC, SignatureInput } from 'elliptic'
20
20
  import BN from 'bn.js'
21
- import { Buffer } from 'buffer/'
22
-
23
21
  import { TOTAL_NUMBER_OF_GROUPS, TOTAL_NUMBER_OF_CHAINS } from '../constants'
24
22
 
25
23
  export const networkIds = ['mainnet', 'testnet', 'devnet'] as const
@@ -73,11 +71,17 @@ export function toNonNegativeBigInt(input: string): bigint | undefined {
73
71
  }
74
72
 
75
73
  export function hexToBinUnsafe(hex: string): Uint8Array {
76
- return Buffer.from(hex, 'hex')
74
+ const bytes: number[] = []
75
+ for (let i = 0; i < hex.length; i += 2) {
76
+ bytes.push(parseInt(hex.slice(i, i + 2), 16))
77
+ }
78
+ return new Uint8Array(bytes)
77
79
  }
78
80
 
79
81
  export function binToHex(bin: Uint8Array): string {
80
- return Buffer.from(bin).toString('hex')
82
+ return Array.from(bin)
83
+ .map((byte) => byte.toString(16).padStart(2, '0'))
84
+ .join('')
81
85
  }
82
86
 
83
87
  export function blockChainIndex(blockHash: HexString): { fromGroup: number; toGroup: number } {
@@ -101,7 +105,8 @@ export function hexToString(str: string): string {
101
105
  if (!isHexString(str)) {
102
106
  throw new Error(`Invalid hex string: ${str}`)
103
107
  }
104
- return Buffer.from(str, 'hex').toString()
108
+ const bytes = hexToBinUnsafe(str)
109
+ return new TextDecoder().decode(bytes)
105
110
  }
106
111
 
107
112
  export function sleep(ms: number): Promise<void> {
@@ -127,14 +132,28 @@ export function difficultyToTarget(diff: bigint): HexString {
127
132
  const maxBigInt = 1n << 256n
128
133
  const target = diff === 1n ? maxBigInt - 1n : maxBigInt / diff
129
134
  const size = Math.floor((target.toString(2).length + 7) / 8)
130
- const mantissa =
135
+ const mantissa = Number(
131
136
  size <= 3
132
137
  ? BigInt.asIntN(32, target) << BigInt(8 * (3 - size))
133
138
  : BigInt.asIntN(32, target >> BigInt(8 * (size - 3)))
134
- const mantissaBytes = Buffer.alloc(4)
135
- mantissaBytes.writeInt32BE(Number(mantissa), 0)
136
- const bytes = new Uint8Array([size, ...mantissaBytes.slice(1)])
137
- return binToHex(bytes)
139
+ )
140
+ const mantissaBytes = new Uint8Array(4)
141
+ mantissaBytes[0] = size
142
+ mantissaBytes[1] = (mantissa >> 16) & 0xff
143
+ mantissaBytes[2] = (mantissa >> 8) & 0xff
144
+ mantissaBytes[3] = mantissa & 0xff
145
+ return binToHex(mantissaBytes)
146
+ }
147
+
148
+ export function concatBytes(arrays: Uint8Array[]): Uint8Array {
149
+ const totalLength = arrays.reduce((acc, arr) => acc + arr.length, 0)
150
+ const result = new Uint8Array(totalLength)
151
+ let offset = 0
152
+ for (const array of arrays) {
153
+ result.set(array, offset)
154
+ offset += array.length
155
+ }
156
+ return result
138
157
  }
139
158
 
140
159
  type _Eq<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false
@@ -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 { webcrypto, randomFillSync } from 'crypto'
21
20
 
22
21
  const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined'
@@ -30,12 +29,12 @@ export class WebCrypto {
30
29
  "Failed to execute 'getRandomValues' on 'Crypto': parameter 1 is not of type 'ArrayBufferView'"
31
30
  )
32
31
  }
33
- const buffer = Buffer.from(array.buffer, array.byteOffset, array.byteLength)
32
+ const bytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength)
34
33
 
35
34
  if (isBrowser) {
36
- globalThis.crypto.getRandomValues(buffer)
35
+ globalThis.crypto.getRandomValues(bytes)
37
36
  } else {
38
- randomFillSync(buffer)
37
+ randomFillSync(bytes)
39
38
  }
40
39
  return array
41
40
  }
package/webpack.config.js CHANGED
@@ -25,9 +25,6 @@ module.exports = {
25
25
  },
26
26
  plugins: [
27
27
  new webpack.SourceMapDevToolPlugin({ filename: '[file].map' }),
28
- new webpack.ProvidePlugin({
29
- Buffer: ['buffer', 'Buffer']
30
- }),
31
28
  new webpack.IgnorePlugin({
32
29
  checkResource(resource) {
33
30
  // The "bip39/src/wordlists" node module consists of json files for multiple languages. We only need English.
@@ -40,8 +37,7 @@ module.exports = {
40
37
  fallback: {
41
38
  fs: false,
42
39
  stream: require.resolve('stream-browserify'),
43
- crypto: require.resolve('crypto-browserify'),
44
- buffer: require.resolve('buffer/')
40
+ crypto: require.resolve('crypto-browserify')
45
41
  }
46
42
  },
47
43
  output: {
File without changes
File without changes