@alephium/web3 1.7.1 → 1.7.3

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.
@@ -16,7 +16,7 @@ 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 } from '../utils'
19
+ import { binToHex, hexToBinUnsafe } from '../utils'
20
20
  import { fromApiNumber256, node, NodeProvider, toApiNumber256Optional, toApiTokens } from '../api'
21
21
  import { addressFromPublicKey, contractIdFromAddress } from '../address'
22
22
  import { toApiDestinations } from './signer'
@@ -32,6 +32,9 @@ import {
32
32
  SignUnsignedTxParams,
33
33
  SignUnsignedTxResult
34
34
  } from './types'
35
+ import { unsignedTxCodec, UnsignedTxCodec } from '../codec'
36
+ import { groupIndexOfTransaction } from '../transaction'
37
+ import { blakeHash } from '../codec/hash'
35
38
 
36
39
  export abstract class TransactionBuilder {
37
40
  abstract get nodeProvider(): NodeProvider
@@ -113,16 +116,18 @@ export abstract class TransactionBuilder {
113
116
  return { ...response, groupIndex: response.fromGroup, gasPrice: fromApiNumber256(response.gasPrice) }
114
117
  }
115
118
 
116
- async buildUnsignedTx(params: SignUnsignedTxParams): Promise<Omit<SignUnsignedTxResult, 'signature'>> {
117
- const data = { unsignedTx: params.unsignedTx }
118
- const decoded = await this.nodeProvider.transactions.postTransactionsDecodeUnsignedTx(data)
119
+ buildUnsignedTx(params: SignUnsignedTxParams): Omit<SignUnsignedTxResult, 'signature'> {
120
+ const unsignedTxBin = hexToBinUnsafe(params.unsignedTx)
121
+ const decoded = unsignedTxCodec.decode(unsignedTxBin)
122
+ const txId = binToHex(blakeHash(unsignedTxBin))
123
+ const [fromGroup, toGroup] = groupIndexOfTransaction(decoded)
119
124
  return {
120
- fromGroup: decoded.fromGroup,
121
- toGroup: decoded.toGroup,
125
+ fromGroup: fromGroup,
126
+ toGroup: toGroup,
122
127
  unsignedTx: params.unsignedTx,
123
- txId: decoded.unsignedTx.txId,
124
- gasAmount: decoded.unsignedTx.gasAmount,
125
- gasPrice: fromApiNumber256(decoded.unsignedTx.gasPrice)
128
+ txId: txId,
129
+ gasAmount: decoded.gasAmount,
130
+ gasPrice: decoded.gasPrice
126
131
  }
127
132
  }
128
133
  }
@@ -16,8 +16,12 @@ 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 { groupOfLockupScript } from '../address'
19
20
  import { node } from '../api'
21
+ import { UnsignedTx } from '../codec'
22
+ import { TOTAL_NUMBER_OF_GROUPS } from '../constants'
20
23
  import { getCurrentNodeProvider } from '../global'
24
+ import { xorByte } from '../utils'
21
25
 
22
26
  function isConfirmed(txStatus: node.TxStatus): txStatus is node.Confirmed {
23
27
  return txStatus.type === 'Confirmed'
@@ -36,3 +40,25 @@ export async function waitForTxConfirmation(
36
40
  await new Promise((r) => setTimeout(r, requestInterval))
37
41
  return waitForTxConfirmation(txId, confirmations, requestInterval)
38
42
  }
43
+
44
+ export function groupIndexOfTransaction(unsignedTx: UnsignedTx): [number, number] {
45
+ if (unsignedTx.inputs.length === 0) throw new Error('Empty inputs for unsignedTx')
46
+
47
+ const fromGroup = groupFromHint(unsignedTx.inputs[0].hint)
48
+
49
+ let toGroup = fromGroup
50
+ for (const output of unsignedTx.fixedOutputs) {
51
+ const outputGroup = groupOfLockupScript(output.lockupScript)
52
+ if (outputGroup !== fromGroup) {
53
+ toGroup = outputGroup
54
+ break
55
+ }
56
+ }
57
+
58
+ return [fromGroup, toGroup]
59
+ }
60
+
61
+ function groupFromHint(hint: number): number {
62
+ const hash = xorByte(hint)
63
+ return hash % TOTAL_NUMBER_OF_GROUPS
64
+ }
@@ -156,6 +156,14 @@ export function concatBytes(arrays: Uint8Array[]): Uint8Array {
156
156
  return result
157
157
  }
158
158
 
159
+ export function xorByte(intValue: number): number {
160
+ const byte0 = (intValue >> 24) & 0xff
161
+ const byte1 = (intValue >> 16) & 0xff
162
+ const byte2 = (intValue >> 8) & 0xff
163
+ const byte3 = intValue & 0xff
164
+ return (byte0 ^ byte1 ^ byte2 ^ byte3) & 0xff
165
+ }
166
+
159
167
  type _Eq<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false
160
168
  export type Eq<X, Y> = _Eq<{ [P in keyof X]: X[P] }, { [P in keyof Y]: Y[P] }>
161
169
  // eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars