From 9363099c218576dba5c3abc2905ac307c1aa9e7d Mon Sep 17 00:00:00 2001 From: moreal Date: Wed, 29 May 2024 18:16:07 +0900 Subject: [PATCH] test(background): remove unused tests --- background/test/api/graphql.test.js | 38 ------------- background/test/mock.js | 77 --------------------------- background/test/study.test.js | 56 ------------------- background/test/utils/aes256.test.js | 11 ---- background/test/wallet/wallet.test.js | 57 -------------------- 5 files changed, 239 deletions(-) delete mode 100644 background/test/api/graphql.test.js delete mode 100644 background/test/mock.js delete mode 100644 background/test/study.test.js delete mode 100644 background/test/utils/aes256.test.js delete mode 100644 background/test/wallet/wallet.test.js diff --git a/background/test/api/graphql.test.js b/background/test/api/graphql.test.js deleted file mode 100644 index 2e1755a..0000000 --- a/background/test/api/graphql.test.js +++ /dev/null @@ -1,38 +0,0 @@ -import Graphql from "@/api/graphql" -import Mock from "../mock" -Mock.mockGraphql() - -describe("graphql.js", () => { - let api - beforeAll(() => { - api = new Graphql() - }) - - test('Update Endpoints', async () => { - expect(api.endpoints.length).toBe(1) - Mock.graphql({ - endpoint: 'https://mercury2.9cscan.com/graphql/', - name:"getLastBlockIndex", - data: {"data": {"chainQuery": {"blockQuery": {"blocks": [{"index": 2972520}]}}}}, - once: true - }) - await api.updateNetwork() - expect(api.endpoints.length).toBe(2) - await api.updateNetwork() - expect(api.endpoints.length).toBe(1) - - await expect(api.updateNetwork('testnet')).rejects.toEqual('Unknown Network testnet') - }) - - test('Checking can call external', () => { - expect(api.canCallExternal('updateEndpoints')).toBeFalsy() - expect(api.canCallExternal('unsignedTx')).toBeFalsy() - expect(api.canCallExternal('attachSignature')).toBeFalsy() - expect(api.canCallExternal('stageTx')).toBeFalsy() - }) - - test('call test', () => { - expect(api.getBalance('address')).not.toBeNull() - expect(api.getActivationStatus('address')).not.toBeNull() - }) -}) \ No newline at end of file diff --git a/background/test/mock.js b/background/test/mock.js deleted file mode 100644 index 56efaf1..0000000 --- a/background/test/mock.js +++ /dev/null @@ -1,77 +0,0 @@ -import axios from "axios"; - -const MockAdapter = require("axios-mock-adapter") -const axiosMock = new MockAdapter(axios) - -export default { - graphql({endpoint = /.*/g, name='', data={}, once=false, responseCode=200}) { - let onPost = axiosMock.onPost(endpoint, { - asymmetricMatch: function (actual) { - return actual['query'] && actual['query'].indexOf(name) >= 0; - }, - }) - if (once) { - onPost.replyOnce(responseCode, data); - } else { - onPost.reply(responseCode, data); - } - }, - mockGraphql() { - this.graphql({ - endpoint: 'https://mars2.9cscan.com/graphql/', - name:"getLastBlockIndex", - data: {"data": {"chainQuery": {"blockQuery": {"blocks": [{"index": 2972520}]}}}} - }) - - this.graphql({ - name:"getBalance", - data: {"data": {"goldBalance": 100}} - }) - this.graphql({ - name:"getActivationStatus", - data: {"data": {"activationStatus": {"addressActivated": true}}} - }) - - this.graphql({ - name:"unsignedTx", - data: {"data": {transaction:{createUnsignedTx:"unsignedTx"}}} - }) - this.graphql({ - name:"attachSignature", - data: {"data": {transaction: {attachSignature: "tx"}}} - }) - this.graphql({ - name:"stageTx", - data: {data:{stageTxV2:"txId"}} - }) - - - }, - mockStorage() { - let data = {} - global.chrome = { - storage: { - local: { - set: (obj) => { - for (let key of Object.keys(obj)) { - data[key] = obj[key] - } - }, - get: (names, callback) => { - let result = {} - for (let name of names) { - result[name] = data[name] - } - callback(result) - }, - remove: (key) => { - delete data[key] - }, - clear: () => { - data = {} - } - } - } - } - } -} \ No newline at end of file diff --git a/background/test/study.test.js b/background/test/study.test.js deleted file mode 100644 index a767188..0000000 --- a/background/test/study.test.js +++ /dev/null @@ -1,56 +0,0 @@ -import {hexZeroPad, joinSignature, serializeTransaction, splitSignature} from "ethers/lib/utils"; -const BN = require('bn.js'); -const EC = require("elliptic").ec; -const ec = new EC("secp256k1"); -const ethers = require("ethers") -const eccrypto = require("eccrypto") -import crypto from "crypto"; -import {BigNumber} from "ethers"; -import keccak256 from "keccak256"; - -//0x130283eE1f337E69236B9E5A844Ad14774450AAC -//022fd56e799cfa3db6230a8a18a1084eafaf44196c1f31dedc218e304f959139 -//ho -//0x21f3d880d38b195352c39f3ea13bb6b79380994d5fcab17e49a16bbd920e4dd445c0eeb3ac1568a3346300d5b1e734269bc655a75ce11b17448c0915614505401c - -const unsignedTx = 'ZDE6YWxkdTc6dHlwZV9pZHUxNTp0cmFuc2Zlcl9hc3NldDJ1Njp2YWx1ZXNkdTY6YW1vdW50bGR1MTM6ZGVjaW1hbFBsYWNlczE6AnU3Om1pbnRlcnNsMjA6R9CCoRXGPntYsVMtIOYxU46vrd5ldTY6dGlja2VydTM6TkNHZWkxMDBlZXU5OnJlY2lwaWVudDIwOllFSfxR0cBuiqjUralH+pHwvGvQdTY6c2VuZGVyMjA6EwKD7h8zfmkja55ahErRR3RFCqxlZWUxOmczMjpFgiUNDaM7BneahHXSg9XdIQxoO5uZnXTQP6xPWPprzjE6bmkyOWUxOnA2NToExhj966pCmc3EyQMXklv8Ii3E54RH0KcMYg0xHa6LfQ1vWAvV9E7amPFJWzCsVWHLVcE+oZ9AZ7yOS6fJomTMNzE6czIwOhMCg+4fM35pI2ueWoRK0Ud0RQqsMTp0dTI3OjIwMjItMDgtMzFUMTY6MTQ6MDEuNjkwMzczWjE6dWwyMDpZRUn8UdHAboqo1K2pR/qR8Lxr0DIwOhMCg+4fM35pI2ueWoRK0Ud0RQqsZWU=' -//unsignedTxId: [[233, 224, 251, 85, 177, 184, 15, 43, 121, 125, 201, 48, 51, 128, 6, 227, 251, 97, 194, 155, 133, 116, 132, 51, 171, 168, 219, 172, 105, 54, 255, 43] -//base64Sign: MEQCIB5GoEvpsq4P3OuJhouEO3+hXxAsIPDkTBJ5AL0n+KppAiA3kZf6+OvmRCMEbFu/MIPzWcxGEDUAD3d/JNPe0EKnlA== - -function pkToBuffer(pk) { - return Buffer.from(ethers.utils.arrayify(pk, {allowMissingPrefix: true})) -} - -// console.log('keccak', crypto.createHash('sha256').update('ZDE6YWxkdTc6dHlwZV9pZHUxNTp0cmFuc2Zlcl9hc3NldDJ1Njp2YWx1ZXNkdTY6YW1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boqo1K2pR/qR8Lxr0DIwOhMCg+4fM35pI2ueWoRK0Ud0RQqsZWU=')) - -describe("test", () => { - test('encrypt & decrypt', async () => { - const pk = "0x022fd56e799cfa3db6230a8a18a1084eafaf44196c1f31dedc218e304f959139" - const wallet = new ethers.Wallet(pk) - const walletSignature = await wallet.signMessage('ho') - console.log('serialized', serializeTransaction({value:'0x1234'})) - wallet.signTransaction() - - const message = ethers.utils.hashMessage('ho') - const signature = ec.sign(ethers.utils.arrayify(message), pkToBuffer(pk), {canonical: true}) - const eccryptoSignature = joinSignature(splitSignature({ - recoveryParam: signature.recoveryParam, - r: hexZeroPad("0x" + signature.r.toString(16), 32), - s: hexZeroPad("0x" + signature.s.toString(16), 32), - })) - console.log(walletSignature == eccryptoSignature) - - await eccrypto.sign(pkToBuffer(pk), ethers.utils.arrayify(message)).then(sig => { - const eccryptoSign = ethers.utils.hexlify(sig) - const recoverSignature = splitSignature(walletSignature) - const randomSig = ec.sign('', eccrypto.generatePrivate(), {canonical: true}) - randomSig.r = new BN(recoverSignature.r.substring(2), 16) - randomSig.s = new BN(recoverSignature.s.substring(2), 16) - randomSig.recoveryParam = recoverSignature.recoveryParam - const recoverSign = ethers.utils.hexlify(randomSig.toDER()) - - console.log(eccryptoSign == recoverSign) - }) - }) -}) \ No newline at end of file diff --git a/background/test/utils/aes256.test.js b/background/test/utils/aes256.test.js deleted file mode 100644 index f923217..0000000 --- a/background/test/utils/aes256.test.js +++ /dev/null @@ -1,11 +0,0 @@ -import aes256 from "@/utils/aes256" - -describe("aes256 test", () => { - test('encrypt & decrypt', () => { - const passphrase = 'PASSPHRASE' - const plainText = 'Hello World' - const encrypted = aes256.encrypt(plainText, passphrase) - expect(() => aes256.decrypt(encrypted, 'Invalid Passphrase')).toThrowError() - expect(aes256.decrypt(encrypted, passphrase)).toEqual(plainText) - }) -}) \ No newline at end of file diff --git a/background/test/wallet/wallet.test.js b/background/test/wallet/wallet.test.js deleted file mode 100644 index 07eeb27..0000000 --- a/background/test/wallet/wallet.test.js +++ /dev/null @@ -1,57 +0,0 @@ -import Wallet from "@/wallet/wallet" -import Mock from "../mock" -import {ENCRYPTED_WALLET} from "@/constants/constants" -import crypto from "crypto"; -const ethers = require("ethers") - -Mock.mockGraphql() -Mock.mockStorage() -jest.setTimeout(1000 * 60) -const passphrase = 'pass' -describe("wallet.js", () => { - let wallet, address - beforeAll(async () => { - wallet = new Wallet(passphrase) - let w = ethers.Wallet.createRandom({locale: 'en'}) - address = w.address - let ew = await w.encrypt(passphrase) - wallet.storage.secureSet(ENCRYPTED_WALLET + address.toLowerCase(), ew) - }) - - test('Checking can call external', () => { - expect(wallet.canCallExternal('decryptWallet')).toBeFalsy() - }) - - test('Generate sequential wallet test', async () => { - let {address:address0} = await wallet.createSequentialWallet(address, 0) - let {address:address1} = await wallet.createSequentialWallet(address, 1) - expect(address0).toBe(address) - expect(address1).not.toBe(address) - expect(address1.length).toBe(address.length) - }) - - test('Generate private wallet test', async () => { - let privateKey = await wallet.getPrivateKey(address, passphrase) - let {address:address0} = await wallet.createPrivateKeyWallet(privateKey) - expect(address0).toBe(address) - }) - - test('Sign & Validate test', async () => { - let data = ['Hello World'] - let signature = await wallet.sign(address, data) - expect(await wallet.validateSignature(signature, data, address)).toBeTruthy() - }) - - - test('Mock send NCG test', async () => { - let receiver = '0x48BD02A8ADe581A55743646c8880F307F2e8e79D' - await expect(async () => await wallet.sendNCG(address, 'other', 100, 1)).rejects.toEqual("Invalid Nonce") - let result = await wallet.sendNCG(address, receiver, 100, await wallet.nextNonce(address)) - - expect(result['status']).toBe('STAGING') - expect(result['signer']).toBe(address) - expect(result['data']['sender']).toBe(address) - expect(result['data']['receiver']).toBe(receiver) - expect(result['data']['amount']).toBe(100) - }) -}) \ No newline at end of file