@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.
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/address/address.d.ts +2 -1
- package/dist/src/address/address.js +27 -19
- package/dist/src/contract/contract.d.ts +10 -3
- package/dist/src/contract/contract.js +59 -17
- package/dist/src/signer/tx-builder.d.ts +1 -1
- package/dist/src/signer/tx-builder.js +13 -8
- package/dist/src/transaction/utils.d.ts +2 -0
- package/dist/src/transaction/utils.js +23 -1
- package/dist/src/utils/utils.d.ts +1 -0
- package/dist/src/utils/utils.js +9 -1
- package/package.json +2 -2
- package/src/address/address.ts +24 -18
- package/src/contract/contract.ts +95 -19
- package/src/signer/tx-builder.ts +14 -9
- package/src/transaction/utils.ts +26 -0
- package/src/utils/utils.ts +8 -0
package/src/signer/tx-builder.ts
CHANGED
|
@@ -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
|
-
|
|
117
|
-
const
|
|
118
|
-
const decoded =
|
|
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:
|
|
121
|
-
toGroup:
|
|
125
|
+
fromGroup: fromGroup,
|
|
126
|
+
toGroup: toGroup,
|
|
122
127
|
unsignedTx: params.unsignedTx,
|
|
123
|
-
txId:
|
|
124
|
-
gasAmount: decoded.
|
|
125
|
-
gasPrice:
|
|
128
|
+
txId: txId,
|
|
129
|
+
gasAmount: decoded.gasAmount,
|
|
130
|
+
gasPrice: decoded.gasPrice
|
|
126
131
|
}
|
|
127
132
|
}
|
|
128
133
|
}
|
package/src/transaction/utils.ts
CHANGED
|
@@ -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
|
+
}
|
package/src/utils/utils.ts
CHANGED
|
@@ -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
|