Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #183 from jimni1222/deCompress
Browse files Browse the repository at this point in the history
Implemented decompressPublicKey in utils
  • Loading branch information
jimni1222 authored Dec 11, 2019
2 parents e7708c9 + be10d02 commit 407cd38
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 122 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"build": "gulp default",
"lint": "./node_modules/.bin/eslint './**/*.js'",
"lintFix": "./node_modules/.bin/eslint './**/*.js' --fix",
"serTest": "mocha test/transactionType/serializationTest.js && mocha test/compressPublicKey.js && mocha test/encodeContractDeploy.js && mocha test/isCompressedPublicKey.js && mocha test/parseAccountKey.js && mocha test/decodeTransaction.js",
"serTest": "mocha test/transactionType/serializationTest.js && mocha test/compressionPublicKey.js && mocha test/encodeContractDeploy.js && mocha test/parseAccountKey.js && mocha test/decodeTransaction.js",
"walletTest": "mocha test/accountLib.js && mocha test/accounts.privateKeyToPublicKey.js && mocha test/accounts.recover.js && mocha test/packages/caver.klay.accounts.js && mocha test/isValidPrivateKey.js && mocha test/privateKeyToAccount.js",
"txTest": "mocha test/sendSignedTransaction.js && mocha test/estimateComputationCost.js && mocha test/getTransactionReceipt.js && mocha test/getTransaction.js && mocha test/setContractOptions.js && mocha test/encodeContractDeploy.js && mocha test/accounts.signTransaction.js && mocha test/sendTransactionCallback.js && mocha test/signWithMultiSig.js && mocha test/transactionType/legacyTransaction.js && mocha test/transactionType/valueTransfer* && mocha test/transactionType/accountUpdate.js && mocha test/transactionType/contract* && mocha test/transactionType/cancelTransaction.js && mocha test/transactionType/feeDelegated*",
"etcTest": "mocha test/packages/caver.utils.js && mocha test/confirmationListener.js && mocha test/hashMessage.js && mocha test/iban.* && mocha test/randomHex.js && mocha test/sha3.js && mocha test/toChecksumAddress.js && mocha test/unitMap.js && mocha test/default* && mocha test/getNodeInfo.js && mocha test/eventEmitter.js && mocha test/packages/caver.klay.net.js && mocha test/getNetworkType.js && mocha test/invalidResponse.js && mocha test/isContractDeployment.js && mocha test/personal.js && mocha test/multiProviderTest.js && mocha test/subscription.js && mocha test/supportsSubscriptions.js && mocha test/contract.once.js && mocha test/setProvider.js",
Expand Down
1 change: 1 addition & 0 deletions packages/caver-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ module.exports = {
isValidPublicKey: utils.isValidPublicKey,
isCompressedPublicKey: utils.isCompressedPublicKey,
compressPublicKey: utils.compressPublicKey,
decompressPublicKey: utils.decompressPublicKey,

// For account key
isValidRole: utils.isValidRole,
Expand Down
15 changes: 15 additions & 0 deletions packages/caver-utils/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,20 @@ const compressPublicKey = uncompressedPublicKey => {
return compressedPublicKey
}

const decompressPublicKey = compressedPublicKey => {
if (!isCompressedPublicKey(compressedPublicKey)) return compressedPublicKey

const compressedWithoutPrefix = compressedPublicKey.replace('0x', '')

const curve = secp256k1.curve
const decoded = curve.decodePoint(compressedWithoutPrefix, 'hex')

const xPoint = decoded.x.toString(16)
const yPoint = decoded.y.toString(16)

return `0x${xPoint}${yPoint}`
}

const isContractDeployment = txObject => {
if (txObject.type) {
if (txObject.type.includes('SMART_CONTRACT_DEPLOY')) return true
Expand Down Expand Up @@ -796,6 +810,7 @@ module.exports = {
isValidPublicKey,
isCompressedPublicKey,
compressPublicKey,
decompressPublicKey,
isTxHash,
isTxHashStrict,

Expand Down
64 changes: 0 additions & 64 deletions test/compressPublicKey.js

This file was deleted.

105 changes: 105 additions & 0 deletions test/compressionPublicKey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
Copyright 2018 The caver-js Authors
This file is part of the caver-js library.
The caver-js library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The caver-js library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the caver-js. If not, see <http://www.gnu.org/licenses/>.
*/

const { expect } = require('./extendedChai')

const Caver = require('../index.js')

const caver = new Caver()

const testCases = [
{
uncompressed:
'0xdbac81e8486d68eac4e6ef9db617f7fbd79a04a3b323c982a09cdfc61f0ae0e8906d7170ba349c86879fb8006134cbf57bda9db9214a90b607b6b4ab57fc026e',
compressed: '0x02dbac81e8486d68eac4e6ef9db617f7fbd79a04a3b323c982a09cdfc61f0ae0e8',
},
{
uncompressed:
'0x6b9edd63b1bb0ed12c7de51d9a7d672e97f6a937c371a545dfc5acf3799ccbf19a692c368625f0f43a17201517123b7e26d53ef722f012c893ab4a5f865f50b2',
compressed: '0x026b9edd63b1bb0ed12c7de51d9a7d672e97f6a937c371a545dfc5acf3799ccbf1',
},
{
uncompressed:
'0x93e9b3177137bb792fa5ebcb3cece17121f81bfb8ae39f6a5c4074b9ca207b96a645fac91e884df17f335d2908eaf103675d12eb327768e816060d1f65c25ac8',
compressed: '0x0293e9b3177137bb792fa5ebcb3cece17121f81bfb8ae39f6a5c4074b9ca207b96',
},
{
uncompressed:
'0x1e7bcc70c72770dbb72fea022e8a6d07f814d2ebe4de9ae3f7af75bf706902a7b73ff919898c836396a6b0c96812c3213b99372050853bd1678da0ead14487d7',
compressed: '0x031e7bcc70c72770dbb72fea022e8a6d07f814d2ebe4de9ae3f7af75bf706902a7',
},
{
uncompressed:
'0xc5ae07417506b7351379f43b8745327d5e5bffecaa209109b03820072d02d6185cf4e5c13397d82e4b8ba9681fa093fc2a1d8788c19d89e4b024ca41b8a66067',
compressed: '0x03c5ae07417506b7351379f43b8745327d5e5bffecaa209109b03820072d02d618',
},
]

describe('caver.utils.compressPublicKey', () => {
it('CAVERJS-UNIT-SER-022 : Should return compressed public key if the argument is uncompressed public key', () => {
expect(caver.utils.compressPublicKey(testCases[0].uncompressed)).to.equal(testCases[0].compressed)
expect(caver.utils.compressPublicKey(testCases[1].uncompressed)).to.equal(testCases[1].compressed)
expect(caver.utils.compressPublicKey(testCases[2].uncompressed)).to.equal(testCases[2].compressed)
expect(caver.utils.compressPublicKey(testCases[3].uncompressed)).to.equal(testCases[3].compressed)
expect(caver.utils.compressPublicKey(testCases[4].uncompressed)).to.equal(testCases[4].compressed)
})

it('CAVERJS-UNIT-SER-023 : Should return same one with the argument if the argument is compressed public key', () => {
expect(caver.utils.compressPublicKey(testCases[0].compressed)).to.equal(testCases[0].compressed)
expect(caver.utils.compressPublicKey(testCases[1].compressed)).to.equal(testCases[1].compressed)
expect(caver.utils.compressPublicKey(testCases[2].compressed)).to.equal(testCases[2].compressed)
expect(caver.utils.compressPublicKey(testCases[3].compressed)).to.equal(testCases[3].compressed)
expect(caver.utils.compressPublicKey(testCases[4].compressed)).to.equal(testCases[4].compressed)
})
})

describe('caver.utils.isCompressedPublicKey', () => {
it('CAVERJS-UNIT-SER-025: Should return false if the argument is uncompressed public key', () => {
expect(caver.utils.isCompressedPublicKey(testCases[0].uncompressed)).to.be.false
expect(caver.utils.isCompressedPublicKey(testCases[1].uncompressed)).to.be.false
expect(caver.utils.isCompressedPublicKey(testCases[2].uncompressed)).to.be.false
expect(caver.utils.isCompressedPublicKey(testCases[3].uncompressed)).to.be.false
expect(caver.utils.isCompressedPublicKey(testCases[4].uncompressed)).to.be.false
})

it('CAVERJS-UNIT-SER-026: Should return true if the argument is compressed public key', () => {
expect(caver.utils.isCompressedPublicKey(testCases[0].compressed)).to.be.true
expect(caver.utils.isCompressedPublicKey(testCases[1].compressed)).to.be.true
expect(caver.utils.isCompressedPublicKey(testCases[2].compressed)).to.be.true
expect(caver.utils.isCompressedPublicKey(testCases[3].compressed)).to.be.true
expect(caver.utils.isCompressedPublicKey(testCases[4].compressed)).to.be.true
})
})

describe('caver.utils.decompressPublicKey', () => {
it('CAVERJS-UNIT-SER-066: Should return uncompressed public key if the argument is compressed public key', () => {
expect(caver.utils.decompressPublicKey(testCases[0].compressed)).to.equal(testCases[0].uncompressed)
expect(caver.utils.decompressPublicKey(testCases[1].compressed)).to.equal(testCases[1].uncompressed)
expect(caver.utils.decompressPublicKey(testCases[2].compressed)).to.equal(testCases[2].uncompressed)
expect(caver.utils.decompressPublicKey(testCases[3].compressed)).to.equal(testCases[3].uncompressed)
expect(caver.utils.decompressPublicKey(testCases[4].compressed)).to.equal(testCases[4].uncompressed)
})

it('CAVERJS-UNIT-SER-067: Should return same one with the argument if the argument is uncompressed public key', () => {
expect(caver.utils.decompressPublicKey(testCases[0].uncompressed)).to.equal(testCases[0].uncompressed)
expect(caver.utils.decompressPublicKey(testCases[1].uncompressed)).to.equal(testCases[1].uncompressed)
expect(caver.utils.decompressPublicKey(testCases[2].uncompressed)).to.equal(testCases[2].uncompressed)
expect(caver.utils.decompressPublicKey(testCases[3].uncompressed)).to.equal(testCases[3].uncompressed)
expect(caver.utils.decompressPublicKey(testCases[4].uncompressed)).to.equal(testCases[4].uncompressed)
})
})
57 changes: 0 additions & 57 deletions test/isCompressedPublicKey.js

This file was deleted.

0 comments on commit 407cd38

Please sign in to comment.