add toEIP55, checkPersionalSign

This commit is contained in:
CounterFire2023 2024-01-17 17:57:13 +08:00
parent e842df426c
commit b5f10e3d21
7 changed files with 2336 additions and 1559 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -33,5 +33,7 @@ declare const sign: ({ user, token, amount, saltNonce, }: {
saltNonce: string;
signature: string;
}>;
declare function toEIP55(address: string): string;
declare function checkPersionalSign(message: string, address: string, signature: string): boolean;
export { buildLoginSignMsg, formatAddress, recoverTypedSignatureV4, sign };
export { buildLoginSignMsg, checkPersionalSign, formatAddress, recoverTypedSignatureV4, sign, toEIP55 };

View File

@ -33,5 +33,7 @@ declare const sign: ({ user, token, amount, saltNonce, }: {
saltNonce: string;
signature: string;
}>;
declare function toEIP55(address: string): string;
declare function checkPersionalSign(message: string, address: string, signature: string): boolean;
export { buildLoginSignMsg, formatAddress, recoverTypedSignatureV4, sign };
export { buildLoginSignMsg, checkPersionalSign, formatAddress, recoverTypedSignatureV4, sign, toEIP55 };

1927
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,5 +1,8 @@
import { recoverTypedSignature, SignTypedDataVersion } from '@metamask/eth-sig-util'
import { soliditySha3, toWei } from 'web3-utils'
import { bytesToHex } from '@noble/hashes/utils'
import { keccak_256 } from '@noble/hashes/sha3'
import { recoverPersonalSignature } from '@metamask/eth-sig-util'
import Web3 from 'web3'
export function recoverTypedSignatureV4(signObj: any, signature: string) {
@ -72,3 +75,25 @@ export const sign = async ({
signature = signature.replace(/00$/, '1b').replace(/01$/, '1c')
return { token, amount: amountBn, startTime, saltNonce, signature }
}
export function toEIP55(address: string) {
const lowerAddress = `${address}`.toLowerCase().replace('0x', '')
var hash = bytesToHex(keccak_256(lowerAddress))
var ret = '0x'
for (var i = 0; i < lowerAddress.length; i++) {
if (parseInt(hash[i], 16) >= 8) {
ret += lowerAddress[i].toUpperCase()
} else {
ret += lowerAddress[i]
}
}
return ret
}
export function checkPersionalSign(message: string, address: string, signature: string) {
if (!signature.startsWith('0x')) {
signature = '0x' + signature
}
const recovered = recoverPersonalSignature({ data: message, signature })
return recovered === address
}