@alephium/web3 1.3.0 → 1.5.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 (107) 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/address/address.js +11 -5
  4. package/dist/src/api/api-alephium.d.ts +2 -4
  5. package/dist/src/api/api-alephium.js +1 -1
  6. package/dist/src/codec/array-codec.d.ts +4 -12
  7. package/dist/src/codec/array-codec.js +15 -28
  8. package/dist/src/codec/asset-output-codec.d.ts +6 -11
  9. package/dist/src/codec/asset-output-codec.js +32 -71
  10. package/dist/src/codec/bigint-codec.d.ts +2 -1
  11. package/dist/src/codec/bigint-codec.js +14 -2
  12. package/dist/src/codec/bytestring-codec.d.ts +6 -11
  13. package/dist/src/codec/bytestring-codec.js +9 -23
  14. package/dist/src/codec/codec.d.ts +54 -5
  15. package/dist/src/codec/codec.js +112 -14
  16. package/dist/src/codec/compact-int-codec.d.ts +65 -44
  17. package/dist/src/codec/compact-int-codec.js +222 -204
  18. package/dist/src/codec/contract-codec.d.ts +5 -8
  19. package/dist/src/codec/contract-codec.js +15 -29
  20. package/dist/src/codec/contract-output-codec.d.ts +4 -10
  21. package/dist/src/codec/contract-output-codec.js +20 -40
  22. package/dist/src/codec/contract-output-ref-codec.d.ts +2 -8
  23. package/dist/src/codec/contract-output-ref-codec.js +7 -17
  24. package/dist/src/codec/either-codec.d.ts +8 -15
  25. package/dist/src/codec/either-codec.js +5 -46
  26. package/dist/src/codec/index.d.ts +4 -3
  27. package/dist/src/codec/index.js +7 -4
  28. package/dist/src/codec/input-codec.d.ts +4 -10
  29. package/dist/src/codec/input-codec.js +11 -46
  30. package/dist/src/codec/instr-codec.d.ts +633 -40
  31. package/dist/src/codec/instr-codec.js +1040 -434
  32. package/dist/src/codec/int-as-4bytes-codec.d.ts +7 -0
  33. package/dist/src/codec/{signed-int-codec.js → int-as-4bytes-codec.js} +6 -12
  34. package/dist/src/codec/lockup-script-codec.d.ts +23 -26
  35. package/dist/src/codec/lockup-script-codec.js +12 -58
  36. package/dist/src/codec/method-codec.d.ts +6 -18
  37. package/dist/src/codec/method-codec.js +20 -48
  38. package/dist/src/codec/option-codec.d.ts +8 -13
  39. package/dist/src/codec/option-codec.js +14 -32
  40. package/dist/src/codec/output-codec.d.ts +2 -2
  41. package/dist/src/codec/output-codec.js +1 -1
  42. package/dist/src/codec/reader.d.ts +8 -0
  43. package/dist/src/codec/reader.js +48 -0
  44. package/dist/src/codec/script-codec.d.ts +6 -14
  45. package/dist/src/codec/script-codec.js +6 -22
  46. package/dist/src/codec/signature-codec.d.ts +4 -12
  47. package/dist/src/codec/signature-codec.js +3 -15
  48. package/dist/src/codec/timestamp-codec.d.ts +8 -0
  49. package/dist/src/codec/timestamp-codec.js +39 -0
  50. package/dist/src/codec/token-codec.d.ts +3 -10
  51. package/dist/src/codec/token-codec.js +6 -24
  52. package/dist/src/codec/transaction-codec.d.ts +6 -11
  53. package/dist/src/codec/transaction-codec.js +24 -49
  54. package/dist/src/codec/unlock-script-codec.d.ts +25 -36
  55. package/dist/src/codec/unlock-script-codec.js +26 -147
  56. package/dist/src/codec/unsigned-tx-codec.d.ts +8 -14
  57. package/dist/src/codec/unsigned-tx-codec.js +24 -66
  58. package/dist/src/codec/val.d.ts +27 -0
  59. package/dist/src/codec/val.js +33 -0
  60. package/dist/src/contract/contract.d.ts +1 -0
  61. package/dist/src/contract/contract.js +20 -13
  62. package/dist/src/contract/index.d.ts +1 -0
  63. package/dist/src/contract/index.js +1 -0
  64. package/dist/src/contract/ralph.d.ts +0 -4
  65. package/dist/src/contract/ralph.js +50 -179
  66. package/dist/src/contract/script-simulator.d.ts +27 -0
  67. package/dist/src/contract/script-simulator.js +757 -0
  68. package/dist/src/exchange/exchange.js +1 -1
  69. package/package.json +3 -4
  70. package/src/address/address.ts +15 -9
  71. package/src/api/api-alephium.ts +2 -4
  72. package/src/codec/array-codec.ts +16 -34
  73. package/src/codec/asset-output-codec.ts +38 -83
  74. package/src/codec/bigint-codec.ts +16 -2
  75. package/src/codec/bytestring-codec.ts +10 -28
  76. package/src/codec/codec.ts +121 -15
  77. package/src/codec/compact-int-codec.ts +230 -207
  78. package/src/codec/contract-codec.ts +20 -33
  79. package/src/codec/contract-output-codec.ts +22 -48
  80. package/src/codec/contract-output-ref-codec.ts +6 -17
  81. package/src/codec/either-codec.ts +4 -53
  82. package/src/codec/index.ts +4 -3
  83. package/src/codec/input-codec.ts +14 -36
  84. package/src/codec/instr-codec.ts +1229 -455
  85. package/src/codec/{signed-int-codec.ts → int-as-4bytes-codec.ts} +6 -10
  86. package/src/codec/lockup-script-codec.ts +28 -76
  87. package/src/codec/method-codec.ts +23 -61
  88. package/src/codec/option-codec.ts +13 -36
  89. package/src/codec/output-codec.ts +2 -2
  90. package/src/codec/reader.ts +56 -0
  91. package/src/codec/script-codec.ts +9 -31
  92. package/src/codec/signature-codec.ts +3 -18
  93. package/src/codec/timestamp-codec.ts +42 -0
  94. package/src/codec/token-codec.ts +7 -26
  95. package/src/codec/transaction-codec.ts +29 -58
  96. package/src/codec/unlock-script-codec.ts +44 -171
  97. package/src/codec/unsigned-tx-codec.ts +34 -63
  98. package/src/codec/val.ts +40 -0
  99. package/src/contract/contract.ts +24 -20
  100. package/src/contract/index.ts +1 -0
  101. package/src/contract/ralph.ts +76 -172
  102. package/src/contract/script-simulator.ts +838 -0
  103. package/src/exchange/exchange.ts +1 -1
  104. package/dist/src/codec/long-codec.d.ts +0 -8
  105. package/dist/src/codec/long-codec.js +0 -55
  106. package/dist/src/codec/signed-int-codec.d.ts +0 -8
  107. package/src/codec/long-codec.ts +0 -58
@@ -15,79 +15,53 @@ 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 { Parser } from 'binary-parser'
19
- import { DecodedArray } from './array-codec'
20
- import { DecodedCompactInt, compactSignedIntCodec, compactUnsignedIntCodec } from './compact-int-codec'
21
- import { P2C } from './lockup-script-codec'
22
- import { Codec } from './codec'
18
+ import { u256Codec } from './compact-int-codec'
19
+ import { P2C, p2cCodec } from './lockup-script-codec'
20
+ import { ObjectCodec } from './codec'
23
21
  import { Token, tokensCodec } from './token-codec'
24
22
  import { ContractOutput as ApiContractOutput } from '../api/api-alephium'
25
23
  import { blakeHash, createHint } from './hash'
26
24
  import { binToHex, bs58, concatBytes, hexToBinUnsafe } from '../utils'
27
- import { signedIntCodec } from './signed-int-codec'
25
+ import { intAs4BytesCodec } from './int-as-4bytes-codec'
28
26
  import { lockupScriptCodec } from './lockup-script-codec'
29
27
 
30
28
  export interface ContractOutput {
31
- amount: DecodedCompactInt
29
+ amount: bigint
32
30
  lockupScript: P2C
33
- tokens: DecodedArray<Token>
31
+ tokens: Token[]
34
32
  }
35
33
 
36
- export class ContractOutputCodec implements Codec<ContractOutput> {
37
- parser = Parser.start()
38
- .nest('amount', {
39
- type: compactUnsignedIntCodec.parser
40
- })
41
- .nest('lockupScript', {
42
- type: Parser.start().buffer('contractId', { length: 32 })
43
- })
44
- .nest('tokens', {
45
- type: tokensCodec.parser
46
- })
47
-
48
- encode(input: ContractOutput): Uint8Array {
49
- const amount = compactUnsignedIntCodec.encode(input.amount)
50
- const lockupScript = input.lockupScript.contractId
51
- const tokens = tokensCodec.encode(input.tokens.value)
52
-
53
- return concatBytes([amount, lockupScript, tokens])
54
- }
55
-
56
- decode(input: Uint8Array): ContractOutput {
57
- return this.parser.parse(input)
58
- }
59
-
34
+ export class ContractOutputCodec extends ObjectCodec<ContractOutput> {
60
35
  static convertToApiContractOutput(txIdBytes: Uint8Array, output: ContractOutput, index: number): ApiContractOutput {
61
- const hint = createHint(output.lockupScript.contractId)
62
- const key = binToHex(blakeHash(concatBytes([txIdBytes, signedIntCodec.encode(index)])))
63
- const attoAlphAmount = compactUnsignedIntCodec.toU256(output.amount).toString()
64
- const address = bs58.encode(new Uint8Array([0x03, ...output.lockupScript.contractId]))
65
- const tokens = output.tokens.value.map((token) => {
36
+ const hint = createHint(output.lockupScript)
37
+ const key = binToHex(blakeHash(concatBytes([txIdBytes, intAs4BytesCodec.encode(index)])))
38
+ const attoAlphAmount = output.amount.toString()
39
+ const address = bs58.encode(new Uint8Array([0x03, ...output.lockupScript]))
40
+ const tokens = output.tokens.map((token) => {
66
41
  return {
67
42
  id: binToHex(token.tokenId),
68
- amount: compactUnsignedIntCodec.toU256(token.amount).toString()
43
+ amount: token.amount.toString()
69
44
  }
70
45
  })
71
46
  return { hint, key, attoAlphAmount, address, tokens, type: 'ContractOutput' }
72
47
  }
73
48
 
74
49
  static convertToOutput(apiContractOutput: ApiContractOutput): ContractOutput {
75
- const amount: DecodedCompactInt = compactUnsignedIntCodec.fromU256(BigInt(apiContractOutput.attoAlphAmount))
76
- const lockupScript: P2C = lockupScriptCodec.decode(bs58.decode(apiContractOutput.address)).script as P2C
50
+ const amount = BigInt(apiContractOutput.attoAlphAmount)
51
+ const lockupScript: P2C = lockupScriptCodec.decode(bs58.decode(apiContractOutput.address)).value as P2C
77
52
 
78
- const tokensValue = apiContractOutput.tokens.map((token) => {
53
+ const tokens = apiContractOutput.tokens.map((token) => {
79
54
  return {
80
55
  tokenId: hexToBinUnsafe(token.id),
81
- amount: compactUnsignedIntCodec.fromU256(BigInt(token.amount))
56
+ amount: BigInt(token.amount)
82
57
  }
83
58
  })
84
- const tokens: DecodedArray<Token> = {
85
- length: compactSignedIntCodec.fromI32(tokensValue.length),
86
- value: tokensValue
87
- }
88
-
89
59
  return { amount, lockupScript, tokens }
90
60
  }
91
61
  }
92
62
 
93
- export const contractOutputCodec = new ContractOutputCodec()
63
+ export const contractOutputCodec = new ContractOutputCodec({
64
+ amount: u256Codec,
65
+ lockupScript: p2cCodec,
66
+ tokens: tokensCodec
67
+ })
@@ -15,28 +15,17 @@ 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 { Parser } from 'binary-parser'
19
18
  import { ArrayCodec } from './array-codec'
20
- import { Codec } from './codec'
21
- import { signedIntCodec } from './signed-int-codec'
22
- import { concatBytes } from '../utils'
19
+ import { byte32Codec, ObjectCodec } from './codec'
20
+ import { intAs4BytesCodec } from './int-as-4bytes-codec'
23
21
 
24
22
  export interface ContractOutputRef {
25
23
  hint: number
26
24
  key: Uint8Array
27
25
  }
28
26
 
29
- export class ContractOutputRefCodec implements Codec<ContractOutputRef> {
30
- parser = Parser.start().int32('hint').buffer('key', { length: 32 })
31
-
32
- encode(input: ContractOutputRef): Uint8Array {
33
- return concatBytes([signedIntCodec.encode(input.hint), input.key])
34
- }
35
-
36
- decode(input: Uint8Array): ContractOutputRef {
37
- return this.parser.parse(input)
38
- }
39
- }
40
-
41
- export const contractOutputRefCodec = new ContractOutputRefCodec()
27
+ export const contractOutputRefCodec = new ObjectCodec<ContractOutputRef>({
28
+ hint: intAs4BytesCodec,
29
+ key: byte32Codec
30
+ })
42
31
  export const contractOutputRefsCodec = new ArrayCodec(contractOutputRefCodec)
@@ -15,59 +15,10 @@ 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 { Parser } from 'binary-parser'
19
- import { Codec } from './codec'
18
+ import { Codec, EnumCodec } from './codec'
20
19
 
21
- export interface Either<L, R> {
22
- either: number
23
- value: L | R
24
- }
25
-
26
- export class EitherCodec<L, R> implements Codec<Either<L, R>> {
27
- constructor(
28
- private leftCodec: Codec<L>,
29
- private rightCodec: Codec<R>,
30
- public parser = Parser.start()
31
- .uint8('either')
32
- .choice('value', {
33
- tag: 'either',
34
- choices: {
35
- 0: leftCodec.parser,
36
- 1: rightCodec.parser
37
- }
38
- })
39
- ) {}
40
-
41
- encode(input: Either<L, R>): Uint8Array {
42
- const result = [input.either]
43
- if (input.either === 0) {
44
- result.push(...this.leftCodec.encode(input.value as L))
45
- } else {
46
- result.push(...this.rightCodec.encode(input.value as R))
47
- }
48
- return new Uint8Array(result)
49
- }
50
-
51
- decode(input: Uint8Array): Either<L, R> {
52
- const result = this.parser.parse(input)
53
- return {
54
- ...result,
55
- value:
56
- result.either === 0 ? this.leftCodec.decode(result.value.value) : this.rightCodec.decode(result.value.value)
57
- }
58
- }
59
-
60
- fromLeft(left: L): Either<L, R> {
61
- return {
62
- either: 0,
63
- value: left
64
- }
65
- }
20
+ export type Either<L, R> = { kind: 'Left'; value: L } | { kind: 'Right'; value: R }
66
21
 
67
- fromRight(right: R): Either<L, R> {
68
- return {
69
- either: 1,
70
- value: right
71
- }
72
- }
22
+ export function either<L, R>(name: string, l: Codec<L>, r: Codec<R>): Codec<Either<L, R>> {
23
+ return new EnumCodec(name, { Left: l, Right: r })
73
24
  }
@@ -20,7 +20,7 @@ export * from './array-codec'
20
20
  export * as assetOutput from './asset-output-codec'
21
21
  export * from './bigint-codec'
22
22
  export * from './bytestring-codec'
23
- export * from './codec'
23
+ export { Codec, boolCodec } from './codec'
24
24
  export * from './compact-int-codec'
25
25
  export * as contractOutput from './contract-output-codec'
26
26
  export * from './contract-output-ref-codec'
@@ -29,12 +29,13 @@ export * from './input-codec'
29
29
  export * from './instr-codec'
30
30
  export * as lockupScript from './lockup-script-codec'
31
31
  export * as unlockScript from './unlock-script-codec'
32
- export * from './long-codec'
32
+ export * as val from './val'
33
+ export * from './timestamp-codec'
33
34
  export * from './method-codec'
34
35
  export * from './option-codec'
35
36
  export * as script from './script-codec'
36
37
  export * from './signature-codec'
37
- export * from './signed-int-codec'
38
+ export * from './int-as-4bytes-codec'
38
39
  export * as token from './token-codec'
39
40
  export * from './transaction-codec'
40
41
  export * from './unsigned-tx-codec'
@@ -15,47 +15,24 @@ 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 { Parser } from 'binary-parser'
19
18
  import { AssetInput } from '../api/api-alephium'
20
- import { binToHex, concatBytes, hexToBinUnsafe } from '../utils'
19
+ import { binToHex, hexToBinUnsafe } from '../utils'
21
20
  import { UnlockScript, unlockScriptCodec } from './unlock-script-codec'
22
- import { Codec } from './codec'
23
- import { signedIntCodec } from './signed-int-codec'
21
+ import { byte32Codec, ObjectCodec } from './codec'
24
22
  import { ArrayCodec } from './array-codec'
23
+ import { intAs4BytesCodec } from './int-as-4bytes-codec'
25
24
 
26
25
  export interface Input {
27
- outputRef: {
28
- hint: number
29
- key: Uint8Array
30
- }
26
+ hint: number
27
+ key: Uint8Array
31
28
  unlockScript: UnlockScript
32
29
  }
33
30
 
34
- export class InputCodec implements Codec<Input> {
35
- parser = Parser.start()
36
- .nest('outputRef', {
37
- type: Parser.start().int32('hint').buffer('key', { length: 32 })
38
- })
39
- .nest('unlockScript', {
40
- type: unlockScriptCodec.parser
41
- })
42
-
43
- encode(input: Input): Uint8Array {
44
- return concatBytes([
45
- signedIntCodec.encode(input.outputRef.hint),
46
- input.outputRef.key,
47
- unlockScriptCodec.encode(input.unlockScript)
48
- ])
49
- }
50
-
51
- decode(input: Uint8Array): Input {
52
- return this.parser.parse(input)
53
- }
54
-
31
+ export class InputCodec extends ObjectCodec<Input> {
55
32
  static toAssetInputs(inputs: Input[]): AssetInput[] {
56
33
  return inputs.map((input) => {
57
- const hint = input.outputRef.hint
58
- const key = binToHex(input.outputRef.key)
34
+ const hint = input.hint
35
+ const key = binToHex(input.key)
59
36
  const unlockScript = unlockScriptCodec.encode(input.unlockScript)
60
37
  return {
61
38
  outputRef: { hint, key },
@@ -69,13 +46,14 @@ export class InputCodec implements Codec<Input> {
69
46
  const hint = input.outputRef.hint
70
47
  const key = hexToBinUnsafe(input.outputRef.key)
71
48
  const unlockScript = unlockScriptCodec.decode(hexToBinUnsafe(input.unlockScript))
72
- return {
73
- outputRef: { hint, key },
74
- unlockScript
75
- }
49
+ return { hint, key, unlockScript }
76
50
  })
77
51
  }
78
52
  }
79
53
 
80
- export const inputCodec = new InputCodec()
54
+ export const inputCodec = new InputCodec({
55
+ hint: intAs4BytesCodec,
56
+ key: byte32Codec,
57
+ unlockScript: unlockScriptCodec
58
+ })
81
59
  export const inputsCodec = new ArrayCodec(inputCodec)