Skip to content

Commit

Permalink
remove unnecessary things
Browse files Browse the repository at this point in the history
  • Loading branch information
avkos committed Jun 13, 2024
1 parent 0c5918f commit 03252b3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 53 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
"web3-eth-accounts": "^4.1.2",
"web3-eth-contract": "^4.5.0",
"web3-types": "^1.6.0",
"web3-utils": "^4.3.0",
"web3-validator": "^2.0.6"
"web3-utils": "^4.3.0"
},
"devDependencies": {
"@chainsafe/eslint-config": "^2.1.1",
Expand Down
17 changes: 1 addition & 16 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,7 @@ function recoverSignerAddress(

return web3Accounts.recover(message, v, r, s, undefined, true);
}
function _getBytes(value: Bytes): Uint8Array {
if (value instanceof Uint8Array) {
return value;
}

if (typeof value === 'string' && value.match(/^0x([0-9a-f][0-9a-f])*$/i)) {
const result = new Uint8Array((value.length - 2) / 2);
let offset = 2;
for (let i = 0; i < result.length; i++) {
result[i] = parseInt(value.substring(offset, offset + 2), 16);
offset += 2;
}
return result;
}
throw new Error('Invalid BytesLike value');
}
export class SignatureObject {
public r: Uint8Array;
public s: Uint8Array;
Expand All @@ -178,7 +163,7 @@ export class SignatureObject {
v?: web3Types.Numbers,
) {
if (typeof rOrSignature === 'string') {
const bytes: Uint8Array = _getBytes(rOrSignature);
const bytes: Uint8Array = web3Utils.hexToBytes(rOrSignature);

if (bytes.length === 64) {
const r = bytes.slice(0, 32);
Expand Down
41 changes: 41 additions & 0 deletions test/integration/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Web3 } from 'web3';
import * as web3Accounts from 'web3-eth-accounts';
import { utils } from '../../src';
import * as constants from '../../src/constants';
import { EIP712Signer } from '../../src/Eip712';

describe('utils', () => {
describe('#isTypedDataSignatureCorrect()', () => {
it('should return true if correct', async () => {
const PRIVATE_KEY =
'0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110';
const ADDRESS = '0x36615Cf349d7F6344891B1e7CA7C72883F5dc049';
const tx = {
type: 113,
chainId: 270,
from: ADDRESS,
to: '0xa61464658AfeAf65CccaaFD3a512b69A83B77618',
value: 7_000_000n,
};
const eip712Signer = new EIP712Signer(
web3Accounts.privateKeyToAccount(PRIVATE_KEY),
270,
);
const signature = eip712Signer.sign(tx);
const web3 = new Web3('http://localhost:8545');

expect(signature.signature).toBe(
'0x5ea12f3d54a1624d7e7f5161dbf6ab746c3335e643b2966264e740cf8e10e9b64b0251fb79d9a5b11730387085a0d58f105926f72e20242ecb274639991939ca1b',
);
const isValidSignature = await utils.isTypedDataSignatureCorrect(
web3,
ADDRESS,
eip712Signer.getDomain(),
constants.EIP712_TYPES,
utils.EIP712.getSignInput(tx),
signature.signature,
);
expect(isValidSignature).toBe(true);
});
});
});
35 changes: 0 additions & 35 deletions test/unit/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { types } from '../../src';
import { utils } from '../../src';
import { ADDRESS1, ADDRESS2 } from '../utils';
import * as constants from '../../src/constants';
import { EIP712Signer } from '../../src/Eip712';

describe('utils', () => {
describe('#getHashedL2ToL1Msg()', () => {
Expand Down Expand Up @@ -290,38 +289,4 @@ describe('utils', () => {
expect(isValidSignature).toBe(true);
});
});

describe('#isTypedDataSignatureCorrect()', () => {
it('should return true if correct', async () => {
const PRIVATE_KEY =
'0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110';
const ADDRESS = '0x36615Cf349d7F6344891B1e7CA7C72883F5dc049';
const tx = {
type: 113,
chainId: 270,
from: ADDRESS,
to: '0xa61464658AfeAf65CccaaFD3a512b69A83B77618',
value: 7_000_000n,
};
const eip712Signer = new EIP712Signer(
web3Accounts.privateKeyToAccount(PRIVATE_KEY),
270,
);
const signature = eip712Signer.sign(tx);
const web3 = new Web3('http://localhost:8545');

expect(signature.signature).toBe(
'0x5ea12f3d54a1624d7e7f5161dbf6ab746c3335e643b2966264e740cf8e10e9b64b0251fb79d9a5b11730387085a0d58f105926f72e20242ecb274639991939ca1b',
);
const isValidSignature = await utils.isTypedDataSignatureCorrect(
web3,
ADDRESS,
eip712Signer.getDomain(),
constants.EIP712_TYPES,
utils.EIP712.getSignInput(tx),
signature.signature,
);
expect(isValidSignature).toBe(true);
});
});
});

0 comments on commit 03252b3

Please sign in to comment.