ADD hex.compress and hex.decompress

pull/1/head
pubkey 7 years ago
parent 59d0955e29
commit e059ee03ff
  1. 22
      README.md
  2. 42
      src/hex.js
  3. 3
      src/index.js
  4. 13
      src/util.js
  5. 22
      test/tutorials/encrypted-message.test.js
  6. 17
      test/unit.test.js
  7. 11
      typings/index.d.ts

@ -16,7 +16,6 @@ In this tutorial we will sign data in javascript and validate the signature insi
In this tutorial we will use the ethereum-identites and asymmetric cryptography to send a encrypted and signed message from alice to bob. [Encrypted Message](./tutorials/encrypted-message.md).
## Functions
### Install
@ -185,3 +184,24 @@ const serializedTx = EthCrypto.signTransaction(
);
const receipt = await web3.eth.sendSignedTransaction(serializedTx);
```
### hex compress/decompress
Compress or decompress a hex-string to make it smaller. You can either compress to utf16 which reduces the size to about 1/4, or to base64 which reduces the size to about 4/5.
```javascript
const hexString = '0x107be946709e41b7895eea9f2dacf998a0a9124acbb786f0fd1a826101581a07';
const utf16 = EthCrypto.hex.compress(hexString); // compress to utf16
// > 'ၻ炞䆷襞ⶬ輦ꂩቊ쮷蛰ﴚ艡Řᨇ'
const base64 = EthCrypto.hex.compress(hexString, true); // compress to base64
// > 'EHvpRnCeQbeJXuqfLaz5mKCpEkrLt4bw/RqCYQFYGgc='
EthCrypto.hex.decompress(utf16); // decompress from utf16
// > '0x107be946709e41b7895eea9f2d...'
EthCrypto.hex.decompress(base64, true); // decompress from utf16
// > '0x107be946709e41b7895eea9f2d...'
```

@ -0,0 +1,42 @@
/**
* compress/decompress hex-strings to utf16 or base64
* thx @juvian
* @link https://stackoverflow.com/a/40471908/3443137
*/
import * as util from './util';
export function compress(hex, base64 = false) {
hex = util.removeTrailing0x(hex);
// if base64:true, we use our own function because it results in a smaller output
if (base64 === true)
return new Buffer(hex, 'hex').toString('base64');
let string = '';
while (hex.length % 4 != 0) { // we need it to be multiple of 4
hex = '0' + hex;
}
for (let i = 0; i < hex.length; i += 4) {
// get char from ascii code which goes from 0 to 65536
string += String.fromCharCode(parseInt(hex.substring(i, i + 4), 16));
}
return string;
}
export function decompress(compressedString, base64 = false) {
// if base64:true, we use our own function because it results in a smaller output
if (base64 === true) {
const ret = new Buffer(compressedString, 'base64').toString('hex');
return util.addTrailing0x(ret);
}
let hex = '';
for (let i = 0; i < compressedString.length; i++) {
// get character ascii code and convert to hexa string, adding necessary 0s
hex += ((i == 0 ? '' : '000') + compressedString.charCodeAt(i).toString(16)).slice(-4);
}
hex = hex.toLowerCase();
return util.addTrailing0x(hex);
}

@ -8,6 +8,7 @@ import sign from './sign';
import signTransaction from './sign-transaction';
import txDataByCompiled from './tx-data-by-compiled';
import * as hash from './hash';
import * as hex from './hex';
import * as vrs from './vrs';
import * as util from './util';
@ -22,6 +23,7 @@ export {
signTransaction,
txDataByCompiled,
hash,
hex,
vrs,
util
};
@ -37,6 +39,7 @@ export default {
signTransaction,
txDataByCompiled,
hash,
hex,
vrs,
util
};

@ -1,3 +1,16 @@
import Web3 from 'web3';
export const web3 = new Web3();
export function removeTrailing0x(str) {
if (str.startsWith('0x'))
return str.substring(2);
else return str;
}
export function addTrailing0x(str) {
if (!str.startsWith('0x'))
return '0x' + str;
else return str;
}

@ -28,8 +28,8 @@ describe('encrypted-message.md', () => {
bob.publicKey,
JSON.stringify(payload)
);
console.log('encrypted:');
console.dir(encrypted);
// console.log('encrypted:');
// console.dir(encrypted);
// decrypt
const decrypted = await EthCrypto.decryptWithPrivateKey(
@ -38,8 +38,8 @@ describe('encrypted-message.md', () => {
);
const decryptedPayload = JSON.parse(decrypted);
const senderAddress = EthCrypto.addressByPublicKey(decryptedPayload.sender);
console.log('decryptedPayload:');
console.dir(decryptedPayload);
// console.log('decryptedPayload:');
// console.dir(decryptedPayload);
// check signature
const signer = EthCrypto.recover(
@ -49,13 +49,13 @@ describe('encrypted-message.md', () => {
if (signer !== senderAddress)
throw new Error('signature not valid');
console.log(
'Got message from ' +
senderAddress +
': "' +
decryptedPayload.message + '"'
);
/* console.log(
'Got message from ' +
senderAddress +
': "' +
decryptedPayload.message + '"'
);
*/
assert.equal(decryptedPayload.message, secretMessage);

@ -8,6 +8,8 @@ const TEST_DATA = {
privateKey: '0x107be946709e41b7895eea9f2dacf998a0a9124acbb786f0fd1a826101581a07',
publicKey: 'bf1cc3154424dc22191941d9f4f50b063a2b663a2337e5548abea633c1d06eceacf2b81dd326d278cd992d5e03b0df140f2df389ac9a1c2415a220a4a9e8c046'
};
const HEX_STRING = '0x55030130e79efc853f8644d32c11a58d47018cc3a08a16ac4fb9c09af4a634b16d1e37f44c60be0001670b7147dbacc6e057ac7595d74ecfd7ff58a593ee9db3cee601ee06234d200e1f2e35533533754ecbf910b86c1b7fc556b1cc2516f6dd3a25360bcd68f1af4f9450952cc9ef53de5b0c42f8f07976a05d0cfc0ee21acda7ad31cc77640fdd55349c460f94d71656e79048e5991aeb8852ad094bc96e8983232710f5b983ba07bc542ac3f4116a5d066b965e9071cb9912ed1a3da98cdd06e5ef75738fb915a6cef05497f49215bba156c2ba525b2a268be95c3efabb3f1d10fc3b3a57f8a06ef048735a5f3cf9fbbe2203b1b39568ff99e78094bf78c61514ebcbdc75fa90e7d06bc11a49959c2c4632d87384a2667f06e03216bba3b345af2cf89c439c12d4c24dc392d3ffdc9e807b00772b99299178415966d86b59478f21ae005e74c68057d5a3ccbefa08';
describe('unit.test.js', () => {
describe('.createIdentity()', () => {
@ -163,7 +165,22 @@ describe('unit.test.js', () => {
});
});
});
describe('hex', () => {
it('compress/decompress to utf16', () => {
const compressed = EthCrypto.hex.compress(HEX_STRING, false);
assert.ok(compressed.length < HEX_STRING.length);
const decompressed = EthCrypto.hex.decompress(compressed, false);
assert.equal(decompressed, HEX_STRING);
});
it('compress/decompress to base64', () => {
const compressed = EthCrypto.hex.compress(HEX_STRING, true);
assert.ok(compressed.length < HEX_STRING.length);
const decompressed = EthCrypto.hex.decompress(compressed, true);
assert.equal(decompressed, HEX_STRING);
});
});
/*
describe('.testBlock()', ()=> {
describe('positive', ()=> {});

11
typings/index.d.ts vendored

@ -66,9 +66,17 @@ export type hash = {
};
export type util = {
web3: Web3
web3: Web3;
removeTrailing0x(str: string): string;
addTrailing0x(str: string): string;
};
export type hex = {
compress(hex: string, base64?: boolean): string;
decompress(str: string, base64?: boolean): string;
};
export function publicKeyToAddress(publicKey: string): string;
declare const _default: {
@ -82,6 +90,7 @@ declare const _default: {
signTransaction,
txDataByCompiled,
hash,
hex,
vrs,
util
};

Loading…
Cancel
Save