Skip to content

Commit

Permalink
feat: keccak
Browse files Browse the repository at this point in the history
  • Loading branch information
AbigailDeng authored and AbigailDeng committed Mar 21, 2023
1 parent 879808c commit c06678b
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 393 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"test:browser:watch": "jest --config=jest.browser.config.js --watch",
"test:node": "jest --config=jest.node.config.js",
"test:node:watch": "jest --config=jest.node.config.js --watch",
"test:coverage": "jest --config=jest.browser.config.js --coverage"
"test:coverage": "jest --config=jest.browser.config.js ./test/unit/contract/contractMethod.test.js --coverage"
},
"keywords": [
"aelf-web3"
Expand Down Expand Up @@ -61,6 +61,7 @@
"hdkey": "^1.1.1",
"isomorphic-fetch": "^3.0.0",
"js-sha256": "^0.9.0",
"keccak": "^3.0.3",
"query-string": "5.1.1",
"randombytes": "^2.1.0",
"readable-stream": "^3.6.0",
Expand Down
70 changes: 44 additions & 26 deletions src/contract/contractMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@
* @file contract method
* @author atom-yang
*/
import {
getTransaction,
Transaction
} from '../util/proto';
import { getTransaction, Transaction } from '../util/proto';
import {
transformArrayToMap,
transformMapToArray,
transform,
INPUT_TRANSFORMERS,
OUTPUT_TRANSFORMERS
OUTPUT_TRANSFORMERS,
} from '../util/transform';
import {
isBoolean,
isFunction,
noop,
uint8ArrayToHex,
unpackSpecifiedTypeData
unpackSpecifiedTypeData,
} from '../util/utils';
import wallet from '../wallet';

Expand Down Expand Up @@ -61,7 +58,7 @@ export default class ContractMethod {
}
const result = unpackSpecifiedTypeData({
data: inputPacked,
dataType: this._inputType
dataType: this._inputType,
});
let params = transform(this._inputType, result, OUTPUT_TRANSFORMERS);
params = transformArrayToMap(this._inputType, params);
Expand All @@ -74,7 +71,7 @@ export default class ContractMethod {
}
let result = unpackSpecifiedTypeData({
data: output,
dataType: this._outputType
dataType: this._outputType,
});
result = transform(this._outputType, result, OUTPUT_TRANSFORMERS);
result = transformArrayToMap(this._outputType, result);
Expand All @@ -86,7 +83,9 @@ export default class ContractMethod {
return null;
}
let params = transformMapToArray(this._outputType, result);

params = transform(this._outputType, params, INPUT_TRANSFORMERS);

const message = this._outputType.fromObject(params);
return this._outputType.encode(message).finish();
}
Expand All @@ -104,7 +103,9 @@ export default class ContractMethod {
}

prepareParametersAsync(args) {
const filterArgs = args.filter(arg => !isFunction(arg) && !isBoolean(arg.sync));
const filterArgs = args.filter(
arg => !isFunction(arg) && !isBoolean(arg.sync)
);
const encoded = this.packInput(filterArgs[0]);

return this._chain.getChainStatus().then(status => {
Expand All @@ -114,18 +115,22 @@ export default class ContractMethod {
}

prepareParameters(args) {
const filterArgs = args.filter(arg => !isFunction(arg) && !isBoolean(arg.sync));
const filterArgs = args.filter(
arg => !isFunction(arg) && !isBoolean(arg.sync)
);
const encoded = this.packInput(filterArgs[0]);

const { BestChainHeight, BestChainHash } = this._chain.getChainStatus({
sync: true
sync: true,
});

return this.handleTransaction(BestChainHeight, BestChainHash, encoded);
}

prepareParametersWithBlockInfo(args) {
const filterArgs = args.filter(arg => !isFunction(arg) && !isBoolean(arg.sync));
const filterArgs = args.filter(
arg => !isFunction(arg) && !isBoolean(arg.sync)
);
const encoded = this.packInput(filterArgs[0]);

const { height, hash } = filterArgs[1]; // blockInfo
Expand All @@ -138,7 +143,7 @@ export default class ContractMethod {
if (argsObject.isSync) {
const parameters = this.prepareParameters(args);
return this._chain.sendTransaction(parameters, {
sync: true
sync: true,
});
}
// eslint-disable-next-line arrow-body-style
Expand All @@ -151,22 +156,26 @@ export default class ContractMethod {
const argsObject = this.extractArgumentsIntoObject(args);
if (argsObject.isSync) {
const parameters = this.prepareParameters(args);
return this.unpackOutput(this._chain.callReadOnly(parameters, {
sync: true
}));
return this.unpackOutput(
this._chain.callReadOnly(parameters, {
sync: true,
})
);
}
// eslint-disable-next-line arrow-body-style
return this.prepareParametersAsync(args).then(parameters => {
return this._chain.callReadOnly(parameters, (error, result) => {
argsObject.callback(error, this.unpackOutput(result));
}).then(this.unpackOutput);
return this._chain
.callReadOnly(parameters, (error, result) => {
argsObject.callback(error, this.unpackOutput(result));
})
.then(this.unpackOutput);
});
}

extractArgumentsIntoObject(args) {
const result = {
callback: noop,
isSync: false
isSync: false,
};
if (args.length === 0) {
// has no callback, default to be async mode
Expand All @@ -176,7 +185,7 @@ export default class ContractMethod {
result.callback = args[args.length - 1];
}
args.forEach(arg => {
if (isBoolean((arg.sync))) {
if (isBoolean(arg.sync)) {
result.isSync = arg.sync;
}
});
Expand All @@ -185,7 +194,9 @@ export default class ContractMethod {

// getData(...args) {
getSignedTx(...args) {
const filterArgs = args.filter(arg => !isFunction(arg) && !isBoolean(arg.sync));
const filterArgs = args.filter(
arg => !isFunction(arg) && !isBoolean(arg.sync)
);

if (filterArgs[1]) {
const { height, hash } = filterArgs[1]; // blockInfo
Expand All @@ -199,11 +210,18 @@ export default class ContractMethod {
}

getRawTx(blockHeightInput, blockHashInput, packedInput) {
const rawTx = getTransaction(this._wallet.address, this._contractAddress, this._name, packedInput);
const rawTx = getTransaction(
this._wallet.address,
this._contractAddress,
this._name,
packedInput
);

rawTx.refBlockNumber = blockHeightInput;
const blockHash = blockHashInput.match(/^0x/) ? blockHashInput.substring(2) : blockHashInput;
rawTx.refBlockPrefix = (Buffer.from(blockHash, 'hex')).slice(0, 4);
const blockHash = blockHashInput.match(/^0x/)
? blockHashInput.substring(2)
: blockHashInput;
rawTx.refBlockPrefix = Buffer.from(blockHash, 'hex').slice(0, 4);
return rawTx;
}

Expand All @@ -214,7 +232,7 @@ export default class ContractMethod {
method: 'broadcast_tx',
callback,
params,
format: this.unpackOutput
format: this.unpackOutput,
};
}

Expand Down
1 change: 1 addition & 0 deletions src/util/httpProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let RequestLibrary = {};
let isFetch = false;
if (process.env.RUNTIME_ENV === 'browser') {
// For browsers use DOM Api XMLHttpRequest
// serviceworker without window and document, only with self
// eslint-disable-next-line no-restricted-globals
const _self = typeof self === 'object' ? self : {};
const _window = typeof window === 'object' ? window : _self;
Expand Down
21 changes: 21 additions & 0 deletions src/util/keccak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const createKeccakHash = require('keccak');

const keccak = bits => str => {
let msg;
if (str.slice(0, 2) === '0x') {
msg = [];
for (let i = 2, l = str.length; i < l; i += 2) {
msg.push(parseInt(str.slice(i, i + 2), 16));
}
msg = Buffer.from(msg);
} else {
msg = str;
}
const instance = createKeccakHash(`keccak${bits}`);
return `0x${instance.update(msg).digest('hex')}`;
};

export const keccak256 = keccak(256);
export const keccak512 = keccak(512);
export const keccak256s = keccak(256);
export const keccak512s = keccak(512);
49 changes: 0 additions & 49 deletions src/util/keccak/api.js

This file was deleted.

26 changes: 0 additions & 26 deletions src/util/keccak/index.js

This file was deleted.

Loading

0 comments on commit c06678b

Please sign in to comment.