@alephium/web3 0.39.3 → 0.41.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 (64) 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/api/api-alephium.d.ts +1 -1
  4. package/dist/src/api/api-alephium.js +1 -1
  5. package/dist/src/api/node-provider.d.ts +2 -0
  6. package/dist/src/api/node-provider.js +12 -6
  7. package/dist/src/api/utils.d.ts +1 -1
  8. package/dist/src/block/block.d.ts +28 -0
  9. package/dist/src/block/block.js +131 -0
  10. package/dist/src/block/index.d.ts +1 -0
  11. package/dist/src/block/index.js +22 -0
  12. package/dist/src/codec/contract-output-codec.js +4 -4
  13. package/dist/src/codec/instr-codec.d.ts +3 -0
  14. package/dist/src/codec/instr-codec.js +19 -3
  15. package/dist/src/codec/lockup-script-codec.js +2 -2
  16. package/dist/src/codec/method-codec.d.ts +3 -1
  17. package/dist/src/codec/method-codec.js +27 -2
  18. package/dist/src/codec/script-codec.d.ts +11 -6
  19. package/dist/src/codec/script-codec.js +13 -2
  20. package/dist/src/codec/transaction-codec.js +2 -2
  21. package/dist/src/codec/unlock-script-codec.d.ts +2 -2
  22. package/dist/src/codec/unsigned-tx-codec.d.ts +2 -2
  23. package/dist/src/contract/contract.d.ts +25 -7
  24. package/dist/src/contract/contract.js +136 -51
  25. package/dist/src/contract/events.d.ts +1 -2
  26. package/dist/src/contract/events.js +28 -14
  27. package/dist/src/index.d.ts +1 -0
  28. package/dist/src/index.js +1 -0
  29. package/dist/src/signer/tx-builder.js +4 -4
  30. package/dist/src/transaction/status.js +28 -4
  31. package/dist/src/utils/address.js +29 -16
  32. package/dist/src/utils/exchange.js +25 -15
  33. package/dist/src/utils/number.d.ts +1 -1
  34. package/dist/src/utils/sign.js +6 -6
  35. package/dist/src/utils/subscription.d.ts +4 -4
  36. package/dist/src/utils/subscription.js +1 -1
  37. package/package.json +5 -5
  38. package/src/api/api-alephium.ts +1 -1
  39. package/src/api/node-provider.ts +8 -1
  40. package/src/api/utils.ts +1 -1
  41. package/src/block/block.ts +139 -0
  42. package/src/block/index.ts +19 -0
  43. package/src/codec/contract-output-codec.ts +1 -1
  44. package/src/codec/instr-codec.ts +14 -1
  45. package/src/codec/lockup-script-codec.ts +3 -3
  46. package/src/codec/method-codec.ts +41 -3
  47. package/src/codec/script-codec.ts +23 -5
  48. package/src/codec/transaction-codec.ts +1 -1
  49. package/src/codec/unlock-script-codec.ts +2 -2
  50. package/src/codec/unsigned-tx-codec.ts +2 -2
  51. package/src/contract/contract.ts +178 -67
  52. package/src/contract/events.ts +6 -18
  53. package/src/index.ts +1 -0
  54. package/src/signer/tx-builder.ts +2 -2
  55. package/src/transaction/status.ts +4 -4
  56. package/src/utils/address.ts +15 -2
  57. package/src/utils/exchange.ts +32 -10
  58. package/src/utils/number.ts +1 -1
  59. package/src/utils/sign.ts +1 -1
  60. package/src/utils/subscription.ts +4 -4
  61. package/std/fungible_token_interface.ral +1 -0
  62. package/std/nft_collection_interface.ral +1 -0
  63. package/std/nft_collection_with_royalty_interface.ral +1 -0
  64. package/std/nft_interface.ral +1 -0
@@ -16,9 +16,20 @@ 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 { AddressType, addressFromPublicKey, addressFromScript, binToHex, bs58, hexToBinUnsafe } from '..'
19
+ import {
20
+ AddressType,
21
+ addressFromPublicKey,
22
+ addressFromScript,
23
+ binToHex,
24
+ bs58,
25
+ hexToBinUnsafe,
26
+ isHexString
27
+ } from '../utils'
20
28
  import { Transaction } from '../api/api-alephium'
21
29
  import { Address } from '../signer'
30
+ import { P2SH, unlockScriptCodec } from '../codec/unlock-script-codec'
31
+ import { Buffer } from 'buffer/'
32
+ import { scriptCodec } from '../codec/script-codec'
22
33
 
23
34
  export function validateExchangeAddress(address: string) {
24
35
  let decoded: Uint8Array
@@ -80,24 +91,35 @@ enum UnlockScriptType {
80
91
  }
81
92
 
82
93
  export function getAddressFromUnlockScript(unlockScript: string): Address {
94
+ if (!isHexString(unlockScript)) {
95
+ throw new Error(`Invalid unlock script ${unlockScript}, expected a hex string`)
96
+ }
83
97
  const decoded = hexToBinUnsafe(unlockScript)
84
98
  if (decoded.length === 0) throw new Error('UnlockScript is empty')
85
99
  const unlockScriptType = decoded[0]
86
100
  const unlockScriptBody = decoded.slice(1)
87
101
 
88
102
  if (unlockScriptType === UnlockScriptType.P2PKH) {
103
+ if (unlockScriptBody.length !== 33) {
104
+ throw new Error(`Invalid p2pkh unlock script: ${unlockScript}`)
105
+ }
89
106
  return addressFromPublicKey(binToHex(unlockScriptBody))
90
- } else if (unlockScriptType === UnlockScriptType.P2MPKH) {
107
+ }
108
+
109
+ if (unlockScriptType === UnlockScriptType.P2MPKH) {
91
110
  throw new Error('Naive multi-sig address is not supported for exchanges as it will be replaced by P2SH')
92
- } else if (unlockScriptType === UnlockScriptType.P2SH) {
93
- // FIXEME: for now we assume that the params is empty, so we need to
94
- // remove the last byte from the `unlockScriptBody`, we can decode
95
- // the unlock script once the codec PR is merged
96
- const script = unlockScriptBody.slice(0, -1)
97
- return addressFromScript(script)
98
- } else {
99
- throw new Error('Invalid unlock script type')
100
111
  }
112
+
113
+ if (unlockScriptType === UnlockScriptType.P2SH) {
114
+ let p2sh: P2SH
115
+ try {
116
+ p2sh = unlockScriptCodec.decode(Buffer.from(decoded)).script as P2SH
117
+ } catch (_) {
118
+ throw new Error(`Invalid p2sh unlock script: ${unlockScript}`)
119
+ }
120
+ return addressFromScript(scriptCodec.encode(p2sh.script))
121
+ }
122
+ throw new Error('Invalid unlock script type')
101
123
  }
102
124
 
103
125
  function checkALPHOutput(tx: Transaction): boolean {
@@ -21,7 +21,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
21
21
  // 2. https://github.com/ethers-io/ethers.js/blob/724881f34d428406488a1c9f9dbebe54b6edecda/src.ts/utils/fixednumber.ts
22
22
 
23
23
  import BigNumber from 'bignumber.js'
24
- import { Number256 } from '..'
24
+ import { Number256 } from '../api/types'
25
25
 
26
26
  export const isNumeric = (numToCheck: any): boolean => !isNaN(parseFloat(numToCheck)) && isFinite(numToCheck)
27
27
 
package/src/utils/sign.ts CHANGED
@@ -17,7 +17,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
19
  import { ec as EC } from 'elliptic'
20
- import { binToHex, encodeSignature, hexToBinUnsafe, signatureDecode } from '..'
20
+ import { binToHex, encodeSignature, hexToBinUnsafe, signatureDecode } from '../utils'
21
21
  import { KeyType } from '../signer'
22
22
  import * as necc from '@noble/secp256k1'
23
23
  import { createHash, createHmac } from 'crypto'
@@ -18,8 +18,8 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
 
19
19
  import EventEmitter from 'eventemitter3'
20
20
 
21
- type MessageCallback<Message> = (message: Message) => Promise<void>
22
- type ErrorCallback<Message> = (error: any, subscription: Subscription<Message>) => Promise<void>
21
+ type MessageCallback<Message> = (message: Message) => Promise<void> | void
22
+ type ErrorCallback<Message> = (error: any, subscription: Subscription<Message>) => Promise<void> | void
23
23
 
24
24
  export interface SubscribeOptions<Message> {
25
25
  pollingInterval: number
@@ -45,7 +45,7 @@ export abstract class Subscription<Message> {
45
45
  this.eventEmitter = new EventEmitter()
46
46
  }
47
47
 
48
- startPolling(): void {
48
+ subscribe(): void {
49
49
  this.eventEmitter.on('tick', async () => {
50
50
  await this.polling()
51
51
 
@@ -68,5 +68,5 @@ export abstract class Subscription<Message> {
68
68
  return this.cancelled
69
69
  }
70
70
 
71
- abstract polling(): Promise<void>
71
+ protected abstract polling(): Promise<void>
72
72
  }
@@ -1,4 +1,5 @@
1
1
  @std(id = #0001)
2
+ @using(methodSelector = false)
2
3
  Interface IFungibleToken {
3
4
  pub fn getSymbol() -> ByteVec
4
5
 
@@ -1,6 +1,7 @@
1
1
  import "std/nft_interface"
2
2
 
3
3
  @std(id = #0002)
4
+ @using(methodSelector = false)
4
5
  Interface INFTCollection {
5
6
  // Collection Uri points to a json file containing metadata for the NFT collection.
6
7
  //
@@ -1,6 +1,7 @@
1
1
  import "std/nft_collection_interface"
2
2
 
3
3
  @std(id = #000201)
4
+ @using(methodSelector = false)
4
5
  Interface INFTCollectionWithRoyalty extends INFTCollection {
5
6
  pub fn royaltyAmount(tokenId: ByteVec, salePrice: U256) -> (U256)
6
7
 
@@ -1,4 +1,5 @@
1
1
  @std(id = #0003)
2
+ @using(methodSelector = false)
2
3
  Interface INFT {
3
4
  // Token Uri points to a json file containing metadata for the NFT.
4
5
  //