Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
b457e88c
Commit
b457e88c
authored
Jan 17, 2015
by
Marek Kotewicz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support for int array types[]
parent
2d8383d3
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
180 additions
and
109 deletions
+180
-109
ethereum.js
dist/ethereum.js
+71
-56
ethereum.js.map
dist/ethereum.js.map
+2
-2
ethereum.min.js
dist/ethereum.min.js
+1
-1
abi.js
lib/abi.js
+72
-47
abi.parsers.js
test/abi.parsers.js
+34
-3
No files found.
dist/ethereum.js
View file @
b457e88c
...
...
@@ -33,15 +33,6 @@ BigNumber.config({ ROUNDING_MODE: BigNumber.ROUND_DOWN });
var
ETH_PADDING
=
32
;
// TODO: make these be actually accurate instead of falling back onto JS's doubles.
var
hexToDec
=
function
(
hex
)
{
return
parseInt
(
hex
,
16
).
toString
();
};
var
decToHex
=
function
(
dec
)
{
return
parseInt
(
dec
).
toString
(
16
);
};
/// Finds first index of array element matching pattern
/// @param array
/// @param callback pattern
...
...
@@ -86,55 +77,66 @@ var namedType = function (name) {
};
};
var
arrayType
=
function
(
type
)
{
return
type
.
slice
(
-
2
)
===
'[]'
;
};
/// Formats input value to byte representation of int
/// If value is negative, return it's two's complement
/// If the value is floating point, round it down
/// @returns right-aligned byte representation of int
var
formatInputInt
=
function
(
value
)
{
var
padding
=
ETH_PADDING
*
2
;
if
(
value
instanceof
BigNumber
||
typeof
value
===
'number'
)
{
if
(
typeof
value
===
'number'
)
value
=
new
BigNumber
(
value
);
value
=
value
.
round
();
if
(
value
.
lessThan
(
0
))
value
=
new
BigNumber
(
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
,
16
).
plus
(
value
).
plus
(
1
);
value
=
value
.
toString
(
16
);
}
else
if
(
value
.
indexOf
(
'0x'
)
===
0
)
value
=
value
.
substr
(
2
);
else
if
(
typeof
value
===
'string'
)
value
=
formatInputInt
(
new
BigNumber
(
value
));
else
value
=
(
+
value
).
toString
(
16
);
return
padLeft
(
value
,
padding
);
};
/// Formats input value to byte representation of string
/// @returns left-algined byte representation of string
var
formatInputString
=
function
(
value
)
{
return
web3
.
fromAscii
(
value
,
ETH_PADDING
).
substr
(
2
);
};
/// Formats input value to byte representation of bool
/// @returns right-aligned byte representation bool
var
formatInputBool
=
function
(
value
)
{
return
'000000000000000000000000000000000000000000000000000000000000000'
+
(
value
?
'1'
:
'0'
);
};
var
dynamicTypeBytes
=
function
(
type
,
value
)
{
// TODO: decide what to do with array of strings
if
(
arrayType
(
type
)
||
prefixedType
(
'string'
)(
type
))
return
formatInputInt
(
value
.
length
);
return
""
;
};
/// Setups input formatters for solidity types
/// @returns an array of input formatters
var
setupInputTypes
=
function
()
{
/// Formats input value to byte representation of int
/// If value is negative, return it's two's complement
/// If the value is floating point, round it down
/// @returns right-aligned byte representation of int
var
formatInt
=
function
(
value
)
{
var
padding
=
ETH_PADDING
*
2
;
if
(
value
instanceof
BigNumber
||
typeof
value
===
'number'
)
{
if
(
typeof
value
===
'number'
)
value
=
new
BigNumber
(
value
);
value
=
value
.
round
();
if
(
value
.
lessThan
(
0
))
value
=
new
BigNumber
(
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
,
16
).
plus
(
value
).
plus
(
1
);
value
=
value
.
toString
(
16
);
}
else
if
(
value
.
indexOf
(
'0x'
)
===
0
)
value
=
value
.
substr
(
2
);
else
if
(
typeof
value
===
'string'
)
value
=
formatInt
(
new
BigNumber
(
value
));
else
value
=
(
+
value
).
toString
(
16
);
return
padLeft
(
value
,
padding
);
};
/// Formats input value to byte representation of string
/// @returns left-algined byte representation of string
var
formatString
=
function
(
value
)
{
return
web3
.
fromAscii
(
value
,
ETH_PADDING
).
substr
(
2
);
};
/// Formats input value to byte representation of bool
/// @returns right-aligned byte representation bool
var
formatBool
=
function
(
value
)
{
return
'000000000000000000000000000000000000000000000000000000000000000'
+
(
value
?
'1'
:
'0'
);
};
return
[
{
type
:
prefixedType
(
'uint'
),
format
:
formatInt
},
{
type
:
prefixedType
(
'int'
),
format
:
formatInt
},
{
type
:
prefixedType
(
'hash'
),
format
:
formatInt
},
{
type
:
prefixedType
(
'string'
),
format
:
formatString
},
{
type
:
prefixedType
(
'real'
),
format
:
formatInt
},
{
type
:
prefixedType
(
'ureal'
),
format
:
formatInt
},
{
type
:
namedType
(
'address'
),
format
:
formatInt
},
{
type
:
namedType
(
'bool'
),
format
:
formatBool
}
{
type
:
prefixedType
(
'uint'
),
format
:
formatIn
putIn
t
},
{
type
:
prefixedType
(
'int'
),
format
:
formatIn
putIn
t
},
{
type
:
prefixedType
(
'hash'
),
format
:
formatIn
putIn
t
},
{
type
:
prefixedType
(
'string'
),
format
:
format
Input
String
},
{
type
:
prefixedType
(
'real'
),
format
:
formatIn
putIn
t
},
{
type
:
prefixedType
(
'ureal'
),
format
:
formatIn
putIn
t
},
{
type
:
namedType
(
'address'
),
format
:
formatIn
putIn
t
},
{
type
:
namedType
(
'bool'
),
format
:
format
Input
Bool
}
];
};
...
...
@@ -156,7 +158,11 @@ var toAbiInput = function (json, methodName, params) {
var
method
=
json
[
index
];
var
padding
=
ETH_PADDING
*
2
;
for
(
var
i
=
0
;
i
<
method
.
inputs
.
length
;
i
++
)
{
method
.
inputs
.
forEach
(
function
(
input
,
index
)
{
bytes
+=
dynamicTypeBytes
(
input
.
type
,
params
[
index
]);
});
method
.
inputs
.
forEach
(
function
(
input
,
i
)
{
var
typeMatch
=
false
;
for
(
var
j
=
0
;
j
<
inputTypes
.
length
&&
!
typeMatch
;
j
++
)
{
typeMatch
=
inputTypes
[
j
].
type
(
method
.
inputs
[
i
].
type
,
params
[
i
]);
...
...
@@ -166,8 +172,17 @@ var toAbiInput = function (json, methodName, params) {
}
var
formatter
=
inputTypes
[
j
-
1
].
format
;
bytes
+=
(
formatter
?
formatter
(
params
[
i
])
:
params
[
i
]);
}
var
toAppend
=
""
;
if
(
arrayType
(
method
.
inputs
[
i
].
type
))
toAppend
=
params
[
i
].
reduce
(
function
(
acc
,
curr
)
{
return
acc
+
formatter
(
curr
);
},
""
);
else
toAppend
=
formatter
(
params
[
i
]);
bytes
+=
toAppend
;
});
return
bytes
;
};
...
...
dist/ethereum.js.map
View file @
b457e88c
This diff is collapsed.
Click to expand it.
dist/ethereum.min.js
View file @
b457e88c
This diff is collapsed.
Click to expand it.
lib/abi.js
View file @
b457e88c
...
...
@@ -76,55 +76,66 @@ var namedType = function (name) {
};
};
var
arrayType
=
function
(
type
)
{
return
type
.
slice
(
-
2
)
===
'[]'
;
};
/// Formats input value to byte representation of int
/// If value is negative, return it's two's complement
/// If the value is floating point, round it down
/// @returns right-aligned byte representation of int
var
formatInputInt
=
function
(
value
)
{
var
padding
=
ETH_PADDING
*
2
;
if
(
value
instanceof
BigNumber
||
typeof
value
===
'number'
)
{
if
(
typeof
value
===
'number'
)
value
=
new
BigNumber
(
value
);
value
=
value
.
round
();
if
(
value
.
lessThan
(
0
))
value
=
new
BigNumber
(
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
,
16
).
plus
(
value
).
plus
(
1
);
value
=
value
.
toString
(
16
);
}
else
if
(
value
.
indexOf
(
'0x'
)
===
0
)
value
=
value
.
substr
(
2
);
else
if
(
typeof
value
===
'string'
)
value
=
formatInputInt
(
new
BigNumber
(
value
));
else
value
=
(
+
value
).
toString
(
16
);
return
padLeft
(
value
,
padding
);
};
/// Formats input value to byte representation of string
/// @returns left-algined byte representation of string
var
formatInputString
=
function
(
value
)
{
return
web3
.
fromAscii
(
value
,
ETH_PADDING
).
substr
(
2
);
};
/// Formats input value to byte representation of bool
/// @returns right-aligned byte representation bool
var
formatInputBool
=
function
(
value
)
{
return
'000000000000000000000000000000000000000000000000000000000000000'
+
(
value
?
'1'
:
'0'
);
};
var
dynamicTypeBytes
=
function
(
type
,
value
)
{
// TODO: decide what to do with array of strings
if
(
arrayType
(
type
)
||
prefixedType
(
'string'
)(
type
))
return
formatInputInt
(
value
.
length
);
return
""
;
};
/// Setups input formatters for solidity types
/// @returns an array of input formatters
var
setupInputTypes
=
function
()
{
/// Formats input value to byte representation of int
/// If value is negative, return it's two's complement
/// If the value is floating point, round it down
/// @returns right-aligned byte representation of int
var
formatInt
=
function
(
value
)
{
var
padding
=
ETH_PADDING
*
2
;
if
(
value
instanceof
BigNumber
||
typeof
value
===
'number'
)
{
if
(
typeof
value
===
'number'
)
value
=
new
BigNumber
(
value
);
value
=
value
.
round
();
if
(
value
.
lessThan
(
0
))
value
=
new
BigNumber
(
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
,
16
).
plus
(
value
).
plus
(
1
);
value
=
value
.
toString
(
16
);
}
else
if
(
value
.
indexOf
(
'0x'
)
===
0
)
value
=
value
.
substr
(
2
);
else
if
(
typeof
value
===
'string'
)
value
=
formatInt
(
new
BigNumber
(
value
));
else
value
=
(
+
value
).
toString
(
16
);
return
padLeft
(
value
,
padding
);
};
/// Formats input value to byte representation of string
/// @returns left-algined byte representation of string
var
formatString
=
function
(
value
)
{
return
web3
.
fromAscii
(
value
,
ETH_PADDING
).
substr
(
2
);
};
/// Formats input value to byte representation of bool
/// @returns right-aligned byte representation bool
var
formatBool
=
function
(
value
)
{
return
'000000000000000000000000000000000000000000000000000000000000000'
+
(
value
?
'1'
:
'0'
);
};
return
[
{
type
:
prefixedType
(
'uint'
),
format
:
formatInt
},
{
type
:
prefixedType
(
'int'
),
format
:
formatInt
},
{
type
:
prefixedType
(
'hash'
),
format
:
formatInt
},
{
type
:
prefixedType
(
'string'
),
format
:
formatString
},
{
type
:
prefixedType
(
'real'
),
format
:
formatInt
},
{
type
:
prefixedType
(
'ureal'
),
format
:
formatInt
},
{
type
:
namedType
(
'address'
),
format
:
formatInt
},
{
type
:
namedType
(
'bool'
),
format
:
formatBool
}
{
type
:
prefixedType
(
'uint'
),
format
:
formatIn
putIn
t
},
{
type
:
prefixedType
(
'int'
),
format
:
formatIn
putIn
t
},
{
type
:
prefixedType
(
'hash'
),
format
:
formatIn
putIn
t
},
{
type
:
prefixedType
(
'string'
),
format
:
format
Input
String
},
{
type
:
prefixedType
(
'real'
),
format
:
formatIn
putIn
t
},
{
type
:
prefixedType
(
'ureal'
),
format
:
formatIn
putIn
t
},
{
type
:
namedType
(
'address'
),
format
:
formatIn
putIn
t
},
{
type
:
namedType
(
'bool'
),
format
:
format
Input
Bool
}
];
};
...
...
@@ -146,7 +157,12 @@ var toAbiInput = function (json, methodName, params) {
var
method
=
json
[
index
];
var
padding
=
ETH_PADDING
*
2
;
for
(
var
i
=
0
;
i
<
method
.
inputs
.
length
;
i
++
)
{
/// first we iterate in search for dynamic
method
.
inputs
.
forEach
(
function
(
input
,
index
)
{
bytes
+=
dynamicTypeBytes
(
input
.
type
,
params
[
index
]);
});
method
.
inputs
.
forEach
(
function
(
input
,
i
)
{
var
typeMatch
=
false
;
for
(
var
j
=
0
;
j
<
inputTypes
.
length
&&
!
typeMatch
;
j
++
)
{
typeMatch
=
inputTypes
[
j
].
type
(
method
.
inputs
[
i
].
type
,
params
[
i
]);
...
...
@@ -156,8 +172,17 @@ var toAbiInput = function (json, methodName, params) {
}
var
formatter
=
inputTypes
[
j
-
1
].
format
;
bytes
+=
(
formatter
?
formatter
(
params
[
i
])
:
params
[
i
]);
}
var
toAppend
=
""
;
if
(
arrayType
(
method
.
inputs
[
i
].
type
))
toAppend
=
params
[
i
].
reduce
(
function
(
acc
,
curr
)
{
return
acc
+
formatter
(
curr
);
},
""
);
else
toAppend
=
formatter
(
params
[
i
]);
bytes
+=
toAppend
;
});
return
bytes
;
};
...
...
test/abi.parsers.js
View file @
b457e88c
...
...
@@ -307,8 +307,14 @@ describe('abi', function() {
var
parser
=
abi
.
inputParser
(
d
);
// then
assert
.
equal
(
parser
.
test
(
'hello'
),
"68656c6c6f000000000000000000000000000000000000000000000000000000"
);
assert
.
equal
(
parser
.
test
(
'world'
),
"776f726c64000000000000000000000000000000000000000000000000000000"
);
assert
.
equal
(
parser
.
test
(
'hello'
),
"000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000"
);
assert
.
equal
(
parser
.
test
(
'world'
),
"0000000000000000000000000000000000000000000000000000000000000005776f726c64000000000000000000000000000000000000000000000000000000"
);
});
it
(
'should use proper method name'
,
function
()
{
...
...
@@ -346,9 +352,34 @@ describe('abi', function() {
//then
assert
.
equal
(
parser
.
test
(
1
),
"0000000000000000000000000000000000000000000000000000000000000001"
);
assert
.
equal
(
parser
.
test2
(
'hello'
),
"68656c6c6f000000000000000000000000000000000000000000000000000000"
);
assert
.
equal
(
parser
.
test2
(
'hello'
),
"000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000"
);
});
it
(
'should parse input array of ints'
,
function
()
{
// given
var
d
=
clone
(
description
);
d
[
0
].
inputs
=
[
{
type
:
"int[]"
}
];
// when
var
parser
=
abi
.
inputParser
(
d
);
// then
assert
.
equal
(
parser
.
test
([
5
,
6
]),
"0000000000000000000000000000000000000000000000000000000000000002"
+
"0000000000000000000000000000000000000000000000000000000000000005"
+
"0000000000000000000000000000000000000000000000000000000000000006"
);
});
});
describe
(
'outputParser'
,
function
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment