-
Notifications
You must be signed in to change notification settings - Fork 0
/
quaiTransport.ts
137 lines (117 loc) · 3.95 KB
/
quaiTransport.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// quaiTransport.ts
import {
type CustomTransport,
custom,
hexToBytes,
parseTransaction,
Transaction,
numberToHex,
hexToBigInt,
type Hex
} from 'viem'
import protobuf from 'protobufjs'
// Define the protobuf schema
const transactionProto = `
syntax = "proto3";
message Transaction {
bytes to = 1;
bytes value = 2;
uint64 nonce = 3;
uint64 gas_limit = 4;
bytes gas_price = 5;
bytes data = 6;
uint64 chain_id = 7;
bytes v = 8;
bytes r = 9;
bytes s = 10;
}
`
const root = protobuf.parse(transactionProto).root
const TransactionMessage = root.lookupType('Transaction')
// Helper functions for safe conversions
function safeHexToBytes(hex: Hex | undefined | null, defaultValue: Uint8Array = new Uint8Array()): Uint8Array {
if (!hex) return defaultValue
return hexToBytes(hex)
}
function safeBigIntToBytes(value: bigint | undefined | null, defaultValue: bigint = 0n): Uint8Array {
const bigIntValue = value ?? defaultValue
const hex = numberToHex(bigIntValue, { size: 32 })
return hexToBytes(hex)
}
function safeNumberToBytes(value: number | undefined | null, defaultValue: number = 0): Uint8Array {
const numberValue = value ?? defaultValue
const hex = numberToHex(numberValue, { size: 8 })
return hexToBytes(hex)
}
interface QuaiTransportConfig {
rpcUrl?: string
timeout?: number
}
export function createQuaiTransport(config: QuaiTransportConfig = {}): CustomTransport {
const {
rpcUrl = 'http://localhost:9200',
timeout = 10000
} = config
return custom({
async request({ method, params }) {
try {
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), timeout)
if (method === 'eth_sendRawTransaction') {
const rawTx = params[0] as `0x${string}`
const parsedTx = parseTransaction(rawTx)
// Create protobuf message with safe conversions
const message = TransactionMessage.create({
to: safeHexToBytes(parsedTx.to),
value: safeBigIntToBytes(parsedTx.value),
nonce: Number(parsedTx.nonce ?? 0),
gasLimit: Number(parsedTx.gas ?? 0),
gasPrice: safeBigIntToBytes(parsedTx.gasPrice),
data: safeHexToBytes(parsedTx.data, new Uint8Array()),
chainId: Number(parsedTx.chainId ?? 0),
v: safeBigIntToBytes(parsedTx.v),
r: safeHexToBytes(parsedTx.r),
s: safeHexToBytes(parsedTx.s)
})
// Verify the message
const error = TransactionMessage.verify(message)
if (error) {
throw new Error(`Invalid transaction: ${error}`)
}
// Encode to protobuf
const encodedTx = TransactionMessage.encode(message).finish()
const protoHex = `0x${Buffer.from(encodedTx).toString('hex')}`
// Update params with protobuf encoded transaction
params = [protoHex]
}
const response = await fetch(rpcUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method,
params
}),
signal: controller.signal
})
clearTimeout(timeoutId)
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const data = await response.json()
if (data.error) {
throw new Error(data.error.message || 'RPC Error')
}
return data.result
} catch (error) {
if (error.name === 'AbortError') {
throw new Error('Request timeout')
}
throw error
}
}
})
}