web3-utils#soliditySha3 TypeScript Examples
The following examples show how to use
web3-utils#soliditySha3.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: gas-refund.service.ts From rubic-app with GNU General Public License v3.0 | 6 votes |
/**
* Specific hash function based on keccak256. Hashes similarly to functions from a merkle contract.
* @param arg two values to hash. Each value takes 32 byte.
*/
private static merkleKeccak256(arg: Uint8Array): string {
const first = `0x${(<Buffer>arg).toString('hex').slice(0, 64)}`;
const second = `0x${(<Buffer>arg).toString('hex').slice(64)}`;
let res;
if (new BigNumber(first).lt(second)) {
res = soliditySha3(first, second);
} else {
res = soliditySha3(second, first);
}
return res.slice(2);
}
Example #2
Source File: gas-refund.service.ts From rubic-app with GNU General Public License v3.0 | 6 votes |
/**
* Calculates the proof for a refund, sends a refund transaction. If successful, notifies the backend of a successful refund.
* @param promotionId promotion id to refund.
* @param onTransactionHash a function to be called after sending a refund transaction.
* @return stream that emits the transaction hash once and completes.
*/
public refund(
promotionId: number,
onTransactionHash?: (hash: string) => void
): Observable<string> {
const address = this.authService.userAddress;
return from(this.checkChain()).pipe(
filter(success => success),
switchMap(() => this.gasRefundApiService.getPromotionMerkleData(promotionId)),
switchMap(({ leaves, rootIndex, amount }) => {
// leaf is keccak256(abi.encodePacked(address, weiAmount))
const leaf = soliditySha3(address, amount.toFixed(0));
const tree = new MerkleTree(leaves, GasRefundService.merkleKeccak256);
const root = `0x${tree.getRoot().toString('hex')}`;
const proof = tree.getHexProof(leaf);
return from(this.sendRefund(proof, amount, { root, rootIndex }, onTransactionHash)).pipe(
map(receipt => ({ txHash: receipt.transactionHash, leaf }))
);
}),
switchMap(({ txHash, leaf }) => {
return this.gasRefundApiService.markPromotionAsUsed(txHash, leaf).pipe(map(() => txHash));
})
);
}
Example #3
Source File: merkle.ts From bal-mining-scripts with GNU General Public License v3.0 | 6 votes |
export function loadTree(balances, decimals = 18) {
const elements: any = [];
Object.keys(balances).forEach((address) => {
const balance = scale(balances[address], decimals);
const leaf = soliditySha3(address, balance);
elements.push(leaf);
});
return new MerkleTree(elements);
}
Example #4
Source File: migrateChannel.ts From bal-mining-scripts with GNU General Public License v3.0 | 5 votes |
leaf = soliditySha3(recipient, scaledBalance)