Skip to content

Commit

Permalink
fix: rework to use getScriptHash from leap-core
Browse files Browse the repository at this point in the history
  • Loading branch information
troggy committed Jun 15, 2019
1 parent 19a4e11 commit bb63bef
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"jayson": "^2.0.6",
"jsbi-utils": "^1.0.0",
"jsondiffpatch": "^0.2.5",
"leap-core": "0.30.1",
"leap-core": "0.32.1-pr.0",
"level": "^4.0.0",
"lodash": "^4.17.10",
"lodash.get": "^4.4.2",
Expand Down
8 changes: 5 additions & 3 deletions src/tx/applyTx/checkSpendCond.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* found in the LICENSE file in the root directory of this source tree.
*/

const { Type, Output } = require('leap-core');
const { Type, Output, Consensus } = require('leap-core');
const Transaction = require('ethereumjs-tx');
const VM = require('ethereumjs-vm');
const utils = require('ethereumjs-util');
Expand All @@ -30,6 +30,8 @@ const {
} = require('./ercBytecode');
const { isNFT, isNST } = require('./../../utils');

const { getAddress, getScriptHash } = Consensus.spendingCondition;

const { Account } = VM.deps;

const REACTOR_ADDR = Buffer.from(
Expand Down Expand Up @@ -228,8 +230,8 @@ module.exports = async (state, tx, bridgeState, nodeConfig = {}) => {
spendingInput = input;
spendingInputUnspent = unspent;
const { script } = spendingInput;
spendingAddrBuf = utils.ripemd160(script.length, utils.keccak256(script));
spendingAddress = `0x${spendingAddrBuf.toString('hex')}`;
spendingAddrBuf = getScriptHash(script);
spendingAddress = getAddress(script);

// continue, input of spending condition is just for gas and will not be minted
// but any leftover after subtracting gas is returned to the owner as the last output.
Expand Down
23 changes: 11 additions & 12 deletions src/tx/applyTx/checkSpendCond.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Tx, Input, Outpoint, Output } = require('leap-core');
const { Tx, Input, Outpoint, Output, Consensus } = require('leap-core');
const utils = require('ethereumjs-util');
const checkSpendCond = require('./checkSpendCond');
const {
Expand All @@ -7,6 +7,8 @@ const {
} = require('../../api/methods/constants');
const checkSpendingCondition = require('./../../api/methods/checkSpendingCondition');

const { getScriptHash } = Consensus.spendingCondition;

const erc20Tokens = [
'0x1111111111111111111111111111111111111111',
'0x2222222222222222222222222222222222222222',
Expand Down Expand Up @@ -101,10 +103,7 @@ async function expectToThrow(func, args) {
describe('checkSpendCond', () => {
test('valid tx', async () => {
// a deposit to the above script has been done
const scriptHash = utils.ripemd160(
conditionScript.length,
utils.keccak256(conditionScript)
);
const scriptHash = getScriptHash(conditionScript);
const deposit = Tx.deposit(
123,
5000000000,
Expand Down Expand Up @@ -210,7 +209,7 @@ describe('checkSpendCond', () => {
),
'hex'
);
const scriptHash = utils.ripemd160(script.length, utils.keccak256(script));
const scriptHash = getScriptHash(script);
const NFTDeposit = Tx.deposit(
123, // depositId
nftAddr,
Expand Down Expand Up @@ -295,7 +294,7 @@ describe('checkSpendCond', () => {
),
'hex'
);
const scriptHash = utils.ripemd160(script.length, utils.keccak256(script));
const scriptHash = getScriptHash(script);
const NFTDeposit = Tx.deposit(
123, // depositId
nftAddr,
Expand Down Expand Up @@ -373,7 +372,7 @@ describe('checkSpendCond', () => {
),
'hex'
);
const scriptHash = utils.ripemd160(script.length, utils.keccak256(script));
const scriptHash = getScriptHash(script);

const deposit = Tx.deposit(
123, // depositId
Expand Down Expand Up @@ -446,7 +445,7 @@ describe('checkSpendCond', () => {
'0x00000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00005';
const receiver = '0x82e8C6Cf42C8D1fF9594b17A3F50e94a12cC860f'.toLowerCase();
const script = Buffer.from(MultiCondition, 'hex');
const scriptHash = utils.ripemd160(script.length, utils.keccak256(script));
const scriptHash = getScriptHash(script);

// to pay for gas
const leapDeposit = Tx.deposit(
Expand Down Expand Up @@ -566,7 +565,7 @@ describe('checkSpendCond', () => {
'0x00000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa00005';
const receiver = ADDR_1.toLowerCase();
const script = Buffer.from(MultiConditionAllowance, 'hex');
const scriptHash = utils.ripemd160(script.length, utils.keccak256(script));
const scriptHash = getScriptHash(script);
const owner = receiver;

// to pay for gas
Expand Down Expand Up @@ -687,7 +686,7 @@ describe('checkSpendCond', () => {
'0x0000000000000000000000005555555555555555555555555555555555555555';
const receiver = ADDR_1.toLowerCase();
const script = Buffer.from(BreedingCondition, 'hex');
const scriptHash = utils.ripemd160(script.length, utils.keccak256(script));
const scriptHash = getScriptHash(script);

// to pay for gas
const leapDeposit = Tx.deposit(
Expand Down Expand Up @@ -785,7 +784,7 @@ describe('checkSpendCond', () => {
);

const script = Buffer.from(tmp, 'hex');
const scriptHash = utils.ripemd160(script.length, utils.keccak256(script));
const scriptHash = getScriptHash(script);

// to pay for gas
const leapDeposit = Tx.deposit(
Expand Down
23 changes: 4 additions & 19 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -641,15 +641,6 @@ babel-plugin-syntax-object-rest-spread@^6.13.0:
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-object-rest-spread/-/babel-plugin-syntax-object-rest-spread-6.13.0.tgz#fd6536f2bce13836ffa3a5458c4903a597bb3bf5"
integrity sha1-/WU28rzhODb/o6VFjEkDpZe7O/U=

babel-polyfill@^6.26.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-polyfill/-/babel-polyfill-6.26.0.tgz#379937abc67d7895970adc621f284cd966cf2153"
integrity sha1-N5k3q8Z9eJWXCtxiHyhM2WbPIVM=
dependencies:
babel-runtime "^6.26.0"
core-js "^2.5.0"
regenerator-runtime "^0.10.5"

babel-preset-jest@^23.2.0:
version "23.2.0"
resolved "https://registry.yarnpkg.com/babel-preset-jest/-/babel-preset-jest-23.2.0.tgz#8ec7a03a138f001a1a8fb1e8113652bf1a55da46"
Expand Down Expand Up @@ -4135,13 +4126,12 @@ lcid@^1.0.0:
dependencies:
invert-kv "^1.0.0"

leap-core@0.30.1:
version "0.30.1"
resolved "https://registry.yarnpkg.com/leap-core/-/leap-core-0.30.1.tgz#efe8c88f78e8a7c5e2a0e0b9611282cedd117076"
integrity sha512-jJDovG7vPzzOr7LhpAM8BLs17+baTjz2NCaqLPPJGa7pyDuSDItvGVQolaOc9ogHupCNzXqFX/8KnhxImrjneg==
leap-core@0.32.1-pr.0:
version "0.32.1-pr.0"
resolved "https://registry.yarnpkg.com/leap-core/-/leap-core-0.32.1-pr.0.tgz#23f65b2b87c5c0894338fc503682d4637034919f"
integrity sha512-YKyX5DcpigB8PNJdDOCqqgMKPhxKrlitJQxC3bbJp40EIMTxbU85la3utz7+6Km+Kr5qH0SM8EAKtZAbPwXwDQ==
dependencies:
"@types/web3" "^1.0.18"
babel-polyfill "^6.26.0"
ethereumjs-util "6.0.0"
jsbi-utils "^1.0.0"
node-fetch "^2.3.0"
Expand Down Expand Up @@ -5664,11 +5654,6 @@ realpath-native@^1.0.0:
dependencies:
util.promisify "^1.0.0"

regenerator-runtime@^0.10.5:
version "0.10.5"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.10.5.tgz#336c3efc1220adcedda2c9fab67b5a7955a33658"
integrity sha1-M2w+/BIgrc7dosn6tntaeVWjNlg=

regenerator-runtime@^0.11.0:
version "0.11.1"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz#be05ad7f9bf7d22e056f9726cee5017fbf19e2e9"
Expand Down

0 comments on commit bb63bef

Please sign in to comment.