Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
h0ngcha0 committed Sep 11, 2024
1 parent 4a7d962 commit e5ecc54
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 55 deletions.
21 changes: 14 additions & 7 deletions packages/web3/src/address/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import BN from 'bn.js'
import { TOTAL_NUMBER_OF_GROUPS } from '../constants'
import blake from 'blakejs'
import bs58 from '../utils/bs58'
import { binToHex, concatBytes, groupFromBytes, hexToBinUnsafe, isHexString } from '../utils'
import { binToHex, concatBytes, hexToBinUnsafe, isHexString, xorByte } from '../utils'
import { KeyType } from '../signer'
import { P2MPKH, lockupScriptCodec } from '../codec/lockup-script-codec'
import { i32Codec } from '../codec'
import { LockupScript } from '../codec/lockup-script-codec'
import djb2 from '../utils/djb2'

const ec = new EC('secp256k1')
const PublicKeyHashSize = 32
Expand Down Expand Up @@ -114,17 +115,17 @@ export function groupOfAddress(address: string): number {

// Pay to public key hash address
function groupOfP2pkhAddress(address: Uint8Array): number {
return groupFromBytes(address)
return groupFromBytesForAssetAddress(address)
}

// Pay to multiple public key hash address
function groupOfP2mpkhAddress(address: Uint8Array): number {
return groupFromBytes(address.slice(1, 33))
return groupFromBytesForAssetAddress(address.slice(1, 33))
}

// Pay to script hash address
function groupOfP2shAddress(address: Uint8Array): number {
return groupFromBytes(address)
return groupFromBytesForAssetAddress(address)
}

export function contractIdFromAddress(address: string): Uint8Array {
Expand Down Expand Up @@ -220,14 +221,20 @@ export function subContractId(parentContractId: string, pathInHex: string, group

export function groupOfLockupScript(lockupScript: LockupScript): number {
if (lockupScript.kind === 'P2PKH') {
return groupFromBytes(lockupScript.value)
return groupFromBytesForAssetAddress(lockupScript.value)
} else if (lockupScript.kind === 'P2MPKH') {
return groupFromBytes(lockupScript.value.publicKeyHashes[0])
return groupFromBytesForAssetAddress(lockupScript.value.publicKeyHashes[0])
} else if (lockupScript.kind === 'P2SH') {
return groupFromBytes(lockupScript.value)
return groupFromBytesForAssetAddress(lockupScript.value)
} else {
// P2C
const contractId = lockupScript.value
return contractId[`${contractId.length - 1}`]
}
}

function groupFromBytesForAssetAddress(bytes: Uint8Array): number {
const hint = djb2(bytes) | 1
const hash = xorByte(hint)
return hash % TOTAL_NUMBER_OF_GROUPS
}
7 changes: 5 additions & 2 deletions packages/web3/src/signer/tx-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
} from './types'
import { unsignedTxCodec, UnsignedTxCodec } from '../codec'
import { groupIndexOfTransaction } from '../transaction'
import { blakeHash } from '../codec/hash'

export abstract class TransactionBuilder {
abstract get nodeProvider(): NodeProvider
Expand Down Expand Up @@ -116,13 +117,15 @@ export abstract class TransactionBuilder {
}

buildUnsignedTx(params: SignUnsignedTxParams): Omit<SignUnsignedTxResult, 'signature'> {
const decoded = unsignedTxCodec.decode(hexToBinUnsafe(params.unsignedTx))
const unsignedTxBin = hexToBinUnsafe(params.unsignedTx)
const decoded = unsignedTxCodec.decode(unsignedTxBin)
const txId = binToHex(blakeHash(unsignedTxBin))
const [fromGroup, toGroup] = groupIndexOfTransaction(decoded)
return {
fromGroup: fromGroup,
toGroup: toGroup,
unsignedTx: params.unsignedTx,
txId: UnsignedTxCodec.txId(decoded),
txId: txId,
gasAmount: decoded.gasAmount,
gasPrice: decoded.gasPrice
}
Expand Down
17 changes: 11 additions & 6 deletions packages/web3/src/transaction/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ You should have received a copy of the GNU Lesser General Public License
along with the library. If not, see <http://www.gnu.org/licenses/>.
*/

import { getSigners } from '@alephium/web3-test'
import { getSigner, getSigners } from '@alephium/web3-test'
import { groupIndexOfTransaction } from './utils'
import { ONE_ALPH } from '../constants'
import { ONE_ALPH, TOTAL_NUMBER_OF_GROUPS } from '../constants'
import { bs58, hexToBinUnsafe } from '../utils'
import { unsignedTxCodec } from '../codec'
import { groupOfAddress } from '../address'
Expand All @@ -32,7 +32,8 @@ describe('transaction utils', () => {
})

it('should calculate the group of transfer transaction', async () => {
const [signer1, signer2] = await getSigners(2)
const signer1 = await getSigner(undefined, randomGroup())
const signer2 = await getSigner(undefined, randomGroup())
const fromAccount = await signer1.getSelectedAccount()
const toAccount = await signer2.getSelectedAccount()

Expand All @@ -50,7 +51,7 @@ describe('transaction utils', () => {

it('should calculate the group of multisig transaction', async () => {
const nodeProvider = web3.getCurrentNodeProvider()
const [signer1, signer2, signer3] = await getSigners(3)
const [signer1, signer2, signer3] = await getSigners(3, undefined, randomGroup())
const pkh1 = toPublicKeyHash(signer1.publicKey)
const pkh2 = toPublicKeyHash(signer2.publicKey)
const pkh3 = toPublicKeyHash(signer3.publicKey)
Expand Down Expand Up @@ -86,8 +87,8 @@ describe('transaction utils', () => {

it('should calculate the group of p2sh transaction', async () => {
const nodeProvider = web3.getCurrentNodeProvider()
const [signer1] = await getSigners(2)
const schnorrSigner = PrivateKeyWallet.Random(undefined, nodeProvider, 'bip340-schnorr')
const signer1 = await getSigner(undefined, randomGroup())
const schnorrSigner = PrivateKeyWallet.Random(randomGroup(), nodeProvider, 'bip340-schnorr')
const fromAccount = await signer1.getSelectedAccount()

{
Expand Down Expand Up @@ -123,4 +124,8 @@ describe('transaction utils', () => {
function toPublicKeyHash(publicKey: string): Uint8Array {
return blake2b(hexToBinUnsafe(publicKey), undefined, 32)
}

function randomGroup(): number {
return Math.floor(Math.random() * TOTAL_NUMBER_OF_GROUPS)
}
})
8 changes: 7 additions & 1 deletion packages/web3/src/transaction/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
import { groupOfLockupScript } from '../address'
import { node } from '../api'
import { UnsignedTx } from '../codec'
import { TOTAL_NUMBER_OF_GROUPS } from '../constants'
import { getCurrentNodeProvider } from '../global'
import { groupFromHint } from '../utils'
import { xorByte } from '../utils'

function isConfirmed(txStatus: node.TxStatus): txStatus is node.Confirmed {
return txStatus.type === 'Confirmed'
Expand Down Expand Up @@ -56,3 +57,8 @@ export function groupIndexOfTransaction(unsignedTx: UnsignedTx): [number, number

return [fromGroup, toGroup]
}

function groupFromHint(hint: number): number {
const hash = xorByte(hint)
return hash % TOTAL_NUMBER_OF_GROUPS
}
38 changes: 0 additions & 38 deletions packages/web3/src/utils/group.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/web3/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ export * from './utils'
export * from './subscription'
export * from './sign'
export * from './number'
export * from './group'
8 changes: 8 additions & 0 deletions packages/web3/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ export function concatBytes(arrays: Uint8Array[]): Uint8Array {
return result
}

export function xorByte(intValue: number): number {
const byte0 = (intValue >> 24) & 0xff
const byte1 = (intValue >> 16) & 0xff
const byte2 = (intValue >> 8) & 0xff
const byte3 = intValue & 0xff
return (byte0 ^ byte1 ^ byte2 ^ byte3) & 0xff
}

type _Eq<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false
export type Eq<X, Y> = _Eq<{ [P in keyof X]: X[P] }, { [P in keyof Y]: Y[P] }>
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
Expand Down

0 comments on commit e5ecc54

Please sign in to comment.