Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT MERGE] fix: change scriptHash generation for spending condition #278

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 6 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 @@ -227,8 +229,9 @@ module.exports = async (state, tx, bridgeState, nodeConfig = {}) => {
// we only allow one spending condition in an transaction, do we want to throw if we find more?
spendingInput = input;
spendingInputUnspent = unspent;
spendingAddrBuf = utils.ripemd160(spendingInput.script);
spendingAddress = `0x${spendingAddrBuf.toString('hex')}`;
const { script } = spendingInput;
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
26 changes: 16 additions & 10 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,7 +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);
const scriptHash = getScriptHash(conditionScript);
const deposit = Tx.deposit(
123,
5000000000,
Expand Down Expand Up @@ -191,7 +193,11 @@ describe('checkSpendCond', () => {
condition.signAll(PRIV_1);
apiResult = await checkSpendingCondition(bridgeState, condition.hex());
expect(apiResult.outputs).toEqual(outs);
expect(apiResult.error.startsWith('Error: outputs do not match computation results')).toEqual(true);
expect(
apiResult.error.startsWith(
'Error: outputs do not match computation results'
)
).toEqual(true);
});

test('Spending Condition: NFT', async () => {
Expand All @@ -203,7 +209,7 @@ describe('checkSpendCond', () => {
),
'hex'
);
const scriptHash = utils.ripemd160(script);
const scriptHash = getScriptHash(script);
const NFTDeposit = Tx.deposit(
123, // depositId
nftAddr,
Expand Down Expand Up @@ -288,7 +294,7 @@ describe('checkSpendCond', () => {
),
'hex'
);
const scriptHash = utils.ripemd160(script);
const scriptHash = getScriptHash(script);
const NFTDeposit = Tx.deposit(
123, // depositId
nftAddr,
Expand Down Expand Up @@ -366,7 +372,7 @@ describe('checkSpendCond', () => {
),
'hex'
);
const scriptHash = utils.ripemd160(script);
const scriptHash = getScriptHash(script);

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

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

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

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

const script = Buffer.from(tmp, 'hex');
const scriptHash = utils.ripemd160(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