Skip to content

Commit

Permalink
fear:transcode
Browse files Browse the repository at this point in the history
  • Loading branch information
YongZL committed Jan 15, 2024
1 parent de54937 commit 07b96e4
Show file tree
Hide file tree
Showing 11 changed files with 436 additions and 122 deletions.
24 changes: 11 additions & 13 deletions app/blob-TX/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { ConnectKitButton } from 'connectkit'
import styled from 'styled-components'
import { LoadingFull } from '@/components/ALoading'
import { SuccessFull } from '@/components/ASuccess'
import { BlobClient, EncodeBlobs } from '@ethda/blobs'
import { ethers, parseEther } from 'ethers'
import { useSendTransaction } from 'wagmi'
import { EncodeBlobs } from '@/utils'
import { ethers } from 'ethers'
import { BlobClient } from '@/client'

const StyledButton = styled.button`
cursor: pointer;
Expand Down Expand Up @@ -48,7 +49,6 @@ const BlobTX = () => {
const [selectedBlob, setSelectedBlob] = useState<boolean>(true)
const [inputText, setInputText] = useState<string>('')
const { data: hash, sendTransaction } = useSendTransaction()

const handleBlobClick = (blob: boolean) => {
setSelectedBlob(blob)
}
Expand Down Expand Up @@ -81,16 +81,14 @@ const BlobTX = () => {
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((result) => {
.then(async (result) => {
console.log('Response:', result)
const blobs = EncodeBlobs(Buffer.from(inputText, 'utf-8'))
const signer = new ethers.Wallet('PRIVATE KEY', new ethers.providers.JsonRpcProvider('https://rpc.ethda.io'))
const blobClient = new BlobClient(signer)
const hash = await blobClient.sendTx(blobs, {}, result)
const receipt = await blobClient.getTxReceipt(hash)
})

// sendTransaction({ to, value: parseEther(value) })
// const textContent = 'Hello, this is some text content.'

// const content: any = inputText
// const blobs = EncodeBlobs(Buffer.from(textBlob, 'utf-8'))
// console.log('blobs', blobs)
}
console.log('hhashhashhashash', hash)

Expand All @@ -101,7 +99,7 @@ const BlobTX = () => {
return (
<div className=' font-[Montserrat] '>
<Header
className={` ${!clickStart ? 'bg-[#FBE8DE]' : 'bg-[#FFFFFFCC]'} py-[27px]`}
className={` ${!clickStart ? 'bg-[#FBE8DE] mo:bg-[#FCE1D6] mo:border-b-[#FCE1D6]' : 'bg-[#FFFFFFCC]'} py-[27px]`}
containerClassName='!w-full pl-9 pr-[31px] mo:w-full mo:pl-0 mo:pr-0 '
logo={` b-EthDA.svg`}
headerTextClassName='!text-[#000000] gap-[50px]'
Expand Down Expand Up @@ -154,7 +152,7 @@ const BlobTX = () => {
<input type='file' hidden ref={inputImgRef} accept='image/*' onChange={onFileChange} />
<div
onClick={handleFileSelect}
className=' cursor-pointer w-[100px] h-[100px] bg-[#FFF8F4] border-2 border-dashed rounded-[5px] border-[#FC7823] flex items-center justify-center'
className=' cursor-pointer w-[100px] h-[100px] bg-[#FFF8F4] border border-dashed rounded-[5px] border-[#FC7823] flex items-center justify-center'
>
<img src='chooseAnyImg.svg'></img>
</div>
Expand Down
177 changes: 177 additions & 0 deletions client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import { resolve } from 'path'
import { constants, ethers } from 'ethers'

import { Common } from '@ethereumjs/common'
import { BlobEIP4844Transaction } from '@ethereumjs/tx'
import { commitmentsToVersionedHashes, delay, getBytes, parseBigintValue } from './utils'
import { TypedDataSigner } from '@ethersproject/abstract-signer'
import { keccak256 } from 'ethereum-cryptography/keccak'
import { RLP } from '@ethereumjs/rlp'
import { ecsign } from '@ethereumjs/util'

export class BlobClient {
private _provider: ethers.providers.JsonRpcProvider
private _signer: ethers.Signer | any

constructor(provider: (ethers.Signer & TypedDataSigner) | ethers.providers.JsonRpcProvider) {
if (provider instanceof ethers.providers.JsonRpcProvider) {
this._provider = provider
} else {
this._provider = (provider as ethers.Signer & TypedDataSigner).provider as ethers.providers.JsonRpcProvider
this._signer = provider
}

const SETUP_FILE_PATH = resolve(__dirname, 'lib', 'trusted_setup.txt')
console.log(SETUP_FILE_PATH)
// loadTrustedSetup(SETUP_FILE_PATH)
}

async sanityCheck(tx) {
let { chainId, nonce, to, value, data, maxPriorityFeePerGas, maxFeePerGas, gasLimit, maxFeePerBlobGas } = tx

if (!chainId) {
chainId = (await this._provider.getNetwork()).chainId
}

if (!nonce) {
nonce = await this._signer.getTransactionCount()
}

value = !value ? '0x' : parseBigintValue(value)

// if (!maxFeePerGas) {
// const params = { from: this._wallet.address, to, data, value };
// // gasLimit = await this.estimateGas(params);
// maxFeePerGas = await this.suggestGasPrice();
// if (!maxFeePerGas) {
// throw Error('estimateGas: execution reverted');
// }
// } else {
// maxFeePerGas = parseBigintValue(maxFeePerGas);
// }

maxFeePerGas = BigInt(1000000000)

// TODO
maxFeePerBlobGas = !maxFeePerBlobGas ? 2000_000_000_000 : parseBigintValue(maxFeePerBlobGas)

to = to ?? constants.AddressZero

gasLimit = 21000

data = data ?? '0x'

maxPriorityFeePerGas = maxPriorityFeePerGas ?? 0

return {
chainId,
nonce,
to,
value,
data,
maxPriorityFeePerGas,
maxFeePerGas,
gasLimit,
maxFeePerBlobGas,
}
}

async sendTx(blobs: any, tx: any, convertData: any) {
console.log('receive', tx)
/* eslint-disable prefer-const */
let { chainId, nonce, to, value, data, maxPriorityFeePerGas, maxFeePerGas, gasLimit, maxFeePerBlobGas } = await this.sanityCheck(tx)

// blobs
const commitments = convertData.commitments
const proofs = convertData.proofs
const versionedHashes = convertData.versionedHashes
// for (let i = 0; i < blobs.length; i++) {
// commitments.push(blobToKzgCommitment(blobs[i]))
// proofs.push(computeBlobKzgProof(blobs[i], commitments[i]))
// versionedHashes.push(commitmentsToVersionedHashes(commitments[i]))
// }

const common = Common.custom(
{
name: 'ethda',
networkId: chainId,
chainId: chainId,
},
{
eips: [1559, 3860, 4844],
},
)
console.log(chainId)

const message = [nonce, maxFeePerGas, gasLimit, to, value, data, 1001n, 0, 0]

const signHash = keccak256(RLP.encode(message))
const pk = getBytes((this._signer as ethers.Wallet).privateKey)
let { v, r, s } = ecsign(signHash, pk)
v = 2n * 1001n + 8n + v
console.log(message, nonce, v, Buffer.from(r).toString('hex'), Buffer.from(s).toString('hex'))
const blobTx = new BlobEIP4844Transaction(
{
chainId,
nonce,
to,
value,
data,
maxPriorityFeePerGas: maxFeePerGas,
maxFeePerGas,
gasLimit,
maxFeePerBlobGas,
blobVersionedHashes: versionedHashes,
blobs,
kzgCommitments: commitments,
kzgProofs: proofs,
v: v - 2n * 1001n - 35n,
r: r,
s: s,
},
{ common },
)
console.log(blobTx)

const rawData = blobTx.serializeNetworkWrapper()

const hex = Buffer.from(rawData).toString('hex')
return await this._provider.send('eth_sendRawTransaction', ['0x' + hex])
}

async isTransactionMined(transactionHash: any) {
const txReceipt = await this._provider.getTransactionReceipt(transactionHash)
if (txReceipt && txReceipt.blockNumber) {
return txReceipt
}
return null
}

async getTxReceipt(transactionHash: any) {
let txReceipt
while (!txReceipt) {
txReceipt = await this.isTransactionMined(transactionHash)
if (txReceipt) break
await delay(5000)
}
return txReceipt
}

async downloadBlobs(txHash: any) {
const tx = await this._provider.send('eth_getTransactionByHash', [txHash])

return {
blob_hashes: tx?.blob_hashes,
sidecar: tx?.sidecar,
}
}

getBlobHash(blob: any) {
// const commit = blobToKzgCommitment(blob)
const commit: any = ''
const localHash = commitmentsToVersionedHashes(commit)
const hash = new Uint8Array(32)
hash.set(localHash.subarray(0, 32 - 8))
return ethers.utils.hexlify(hash)
}
}
2 changes: 1 addition & 1 deletion components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const Header: FC<HeaderType> = ({
push(address)
}
return (
<header className={classNames('py-5 border-b border-b-[rgba(255,255,255,.2)] mo:bg-[#FCE1D6] mo:border-b-[#FCE1D6]', className)}>
<header className={classNames('py-5 border-b border-b-[rgba(255,255,255,.2)]', className)}>
<div className=' mo:mx-[30px] '>
<div className={classNames('flex justify-between items-center', containerClassName)}>
<Image src={logo} alt={'logo.svg'} width={119} height={26} />
Expand Down
8 changes: 8 additions & 0 deletions constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const NULL_ADDRESS = '0x0000000000000000000000000000000000000000';
export const ZERO_BYTES32 =
'0x0000000000000000000000000000000000000000000000000000000000000000';

export const BlobTxBytesPerFieldElement = 32; // Size in bytes of a field element
export const BlobTxFieldElementsPerBlob = 4096;
export const BLOB_SIZE =
BlobTxBytesPerFieldElement * BlobTxFieldElementsPerBlob;
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@
},
"dependencies": {
"@ethda/blobs": "^0.2.0",
"@ethereumjs/common": "^4.1.0",
"@ethereumjs/rlp": "^5.0.1",
"@ethereumjs/tx": "^5.1.0",
"@ethereumjs/util": "^9.0.1",
"@formkit/auto-animate": "^0.8.1",
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-popover": "^1.0.7",
"@ethersproject/abstract-signer": "^5.7.0",
"ethereum-cryptography": "^2.1.2",
"classnames": "^2.3.2",
"connectkit": "^1.4.0",
"ethers": "^6.9.2",
"ethers": "^5.6.0",
"ethjs-util": "^0.1.3",
"fs": "0.0.1-security",
"next": "14.0.1",
"pino-pretty": "^10.3.1",
Expand Down
Loading

0 comments on commit 07b96e4

Please sign in to comment.