add getTopics, decodeEvent

This commit is contained in:
CounterFire2023 2024-01-18 12:04:34 +08:00
parent 104626bf72
commit d1fccbac26
7 changed files with 8542 additions and 1151 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -1,3 +1,5 @@
import { AbiItem } from 'web3-utils';
declare function recoverTypedSignatureV4(signObj: any, signature: string): string; declare function recoverTypedSignatureV4(signObj: any, signature: string): string;
declare function formatAddress(address: string): string; declare function formatAddress(address: string): string;
declare function buildLoginSignMsg(nonce: string, tips: string): { declare function buildLoginSignMsg(nonce: string, tips: string): {
@ -35,5 +37,10 @@ declare const sign: ({ user, token, amount, saltNonce, }: {
}>; }>;
declare function toEIP55(address: string): string; declare function toEIP55(address: string): string;
declare function checkPersionalSign(message: string, address: string, signature: string): boolean; declare function checkPersionalSign(message: string, address: string, signature: string): boolean;
declare const getTopics: (abi: AbiItem) => string;
declare const decodeEvent: (abi: AbiItem, eventData: {
data: string;
topics: string[];
}) => any;
export { buildLoginSignMsg, checkPersionalSign, formatAddress, recoverTypedSignatureV4, sign, toEIP55 }; export { buildLoginSignMsg, checkPersionalSign, decodeEvent, formatAddress, getTopics, recoverTypedSignatureV4, sign, toEIP55 };

View File

@ -1,3 +1,5 @@
import { AbiItem } from 'web3-utils';
declare function recoverTypedSignatureV4(signObj: any, signature: string): string; declare function recoverTypedSignatureV4(signObj: any, signature: string): string;
declare function formatAddress(address: string): string; declare function formatAddress(address: string): string;
declare function buildLoginSignMsg(nonce: string, tips: string): { declare function buildLoginSignMsg(nonce: string, tips: string): {
@ -35,5 +37,10 @@ declare const sign: ({ user, token, amount, saltNonce, }: {
}>; }>;
declare function toEIP55(address: string): string; declare function toEIP55(address: string): string;
declare function checkPersionalSign(message: string, address: string, signature: string): boolean; declare function checkPersionalSign(message: string, address: string, signature: string): boolean;
declare const getTopics: (abi: AbiItem) => string;
declare const decodeEvent: (abi: AbiItem, eventData: {
data: string;
topics: string[];
}) => any;
export { buildLoginSignMsg, checkPersionalSign, formatAddress, recoverTypedSignatureV4, sign, toEIP55 }; export { buildLoginSignMsg, checkPersionalSign, decodeEvent, formatAddress, getTopics, recoverTypedSignatureV4, sign, toEIP55 };

4818
dist/utils/chain.util.js vendored

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -1,9 +1,11 @@
import { recoverTypedSignature, SignTypedDataVersion } from '@metamask/eth-sig-util' import { recoverTypedSignature, SignTypedDataVersion } from '@metamask/eth-sig-util'
import { soliditySha3, toWei } from 'web3-utils' //@ts-ignore
import { soliditySha3, toWei, keccak256, _jsonInterfaceMethodToString, AbiInput, AbiItem } from 'web3-utils'
import { bytesToHex } from '@noble/hashes/utils' import { bytesToHex } from '@noble/hashes/utils'
import { keccak_256 } from '@noble/hashes/sha3' import { keccak_256 } from '@noble/hashes/sha3'
import { recoverPersonalSignature } from '@metamask/eth-sig-util' import { recoverPersonalSignature } from '@metamask/eth-sig-util'
import Web3 from 'web3' import Web3 from 'web3'
import web3abi from 'web3-eth-abi'
export function recoverTypedSignatureV4(signObj: any, signature: string) { export function recoverTypedSignatureV4(signObj: any, signature: string) {
return recoverTypedSignature({ return recoverTypedSignature({
@ -96,4 +98,38 @@ export function checkPersionalSign(message: string, address: string, signature:
} }
const recovered = recoverPersonalSignature({ data: message, signature }) const recovered = recoverPersonalSignature({ data: message, signature })
return recovered === address return recovered === address
}
export const getTopics = (abi: AbiItem) => {
return keccak256(_jsonInterfaceMethodToString(abi))
}
export const decodeEvent = (abi: AbiItem, eventData: { data: string; topics: string[] }) => {
const abiInputs = abi.inputs
let result = web3abi.decodeLog(abiInputs, eventData.data, eventData.topics.slice(1))
let decodedData: any = {}
for (let i = 0; i < abiInputs.length; i++) {
const input: AbiInput = abiInputs[i]
if (input.type === 'tuple[]') {
// @ts-ignore
decodedData[input.name] = result[i].map(item => {
let itemData = {}
for (let j = 0; j < input.components.length; j++) {
const component = input.components[j]
itemData[component.name] = item[j]
}
return itemData
})
} else if (input.type === 'tuple') {
let itemData = {}
for (let j = 0; j < input.components.length; j++) {
const component = input.components[j]
itemData[component.name] = result[i][j]
}
decodedData[input.name] = itemData
} else {
decodedData[input.name] = result[i]
}
}
return decodedData
} }