aboutsummaryrefslogtreecommitdiffstats
path: root/packages
diff options
context:
space:
mode:
authorGreg Hysen <greg.hysen@gmail.com>2018-11-20 05:01:26 +0800
committerGreg Hysen <greg.hysen@gmail.com>2018-11-29 08:38:10 +0800
commit5b187935dc6bf194555e1652dbf53d1c637d4add (patch)
tree76b483bf416a2cb8fbac8283191ae34651e02625 /packages
parentd70d07366fcc9ed2a750a5427af9d210c486f344 (diff)
downloaddexon-sol-tools-5b187935dc6bf194555e1652dbf53d1c637d4add.tar
dexon-sol-tools-5b187935dc6bf194555e1652dbf53d1c637d4add.tar.gz
dexon-sol-tools-5b187935dc6bf194555e1652dbf53d1c637d4add.tar.bz2
dexon-sol-tools-5b187935dc6bf194555e1652dbf53d1c637d4add.tar.lz
dexon-sol-tools-5b187935dc6bf194555e1652dbf53d1c637d4add.tar.xz
dexon-sol-tools-5b187935dc6bf194555e1652dbf53d1c637d4add.tar.zst
dexon-sol-tools-5b187935dc6bf194555e1652dbf53d1c637d4add.zip
tests for String
Diffstat (limited to 'packages')
-rw-r--r--packages/utils/src/abi_encoder/evm_data_types.ts4
-rw-r--r--packages/utils/test/abi_encoder_test.ts160
2 files changed, 123 insertions, 41 deletions
diff --git a/packages/utils/src/abi_encoder/evm_data_types.ts b/packages/utils/src/abi_encoder/evm_data_types.ts
index ff1bc594d..b6a62a5d4 100644
--- a/packages/utils/src/abi_encoder/evm_data_types.ts
+++ b/packages/utils/src/abi_encoder/evm_data_types.ts
@@ -318,7 +318,7 @@ export class Bytes extends PayloadDataType {
public encodeValue(value: string | Buffer): Buffer {
if (typeof value === 'string' && !value.startsWith('0x')) {
- throw new Error(`Input value must be hex (prefixed with 0x). Actual value is '${value}'`);
+ throw new Error(`Tried to encode non-hex value. Value must inlcude '0x' prefix. Got '${value}'`);
}
const valueBuf = ethUtil.toBuffer(value);
if (value.length % 2 !== 0) {
@@ -365,7 +365,7 @@ export class SolString extends PayloadDataType {
public encodeValue(value: string): Buffer {
const wordsForValue = Math.ceil(value.length / 32);
const paddedBytesForValue = wordsForValue * 32;
- const valueBuf = ethUtil.setLengthRight(ethUtil.toBuffer(value), paddedBytesForValue);
+ const valueBuf = ethUtil.setLengthRight(new Buffer(value), paddedBytesForValue);
const lengthBuf = ethUtil.setLengthLeft(ethUtil.toBuffer(value.length), 32);
const encodedValueBuf = Buffer.concat([lengthBuf, valueBuf]);
return encodedValueBuf;
diff --git a/packages/utils/test/abi_encoder_test.ts b/packages/utils/test/abi_encoder_test.ts
index 5fe625092..b541cd758 100644
--- a/packages/utils/test/abi_encoder_test.ts
+++ b/packages/utils/test/abi_encoder_test.ts
@@ -5,6 +5,7 @@ import { chaiSetup } from './utils/chai_setup';
import { BigNumber, AbiEncoder } from '../src/';
import * as AbiSamples from './abi_samples';
import { DecodingRules } from '../src/abi_encoder';
+import ethUtil = require('ethereumjs-util');
chaiSetup.configure();
const expect = chai.expect;
@@ -1418,55 +1419,136 @@ describe.only('ABI Encoder', () => {
});
});
- /*
-
-
+ describe.only('Dynamic Bytes Dynamic', () => {
+ it('Fits into one EVM word', async () => {
+ // Create DataType object
+ const testDataItem = { name: 'Dynamic Bytes', type: 'bytes' };
+ const dataType = new AbiEncoder.Bytes(testDataItem);
+ // Construct args to be encoded
+ // Note: There will be padding because this is a bytes32 but we are only passing in 4 bytes.
+ const args = '0x1a18bf61';
+ // Encode Args and validate result
+ const encodedArgs = dataType.encode(args);
+ const expectedEncodedArgs = '0x00000000000000000000000000000000000000000000000000000000000000041a18bf6100000000000000000000000000000000000000000000000000000000';
+ expect(encodedArgs).to.be.equal(expectedEncodedArgs);
+ // Decode Encoded Args and validate result
+ const decodedArgs = dataType.decode(encodedArgs);
+ const decodedArgsAsJson = JSON.stringify(decodedArgs);
+ const argsAsJson = JSON.stringify(args);
+ expect(decodedArgsAsJson).to.be.equal(argsAsJson);
+ });
- describe('Bytes (Dynamic)', () => {
- const testBytesDataItem = { name: 'testBytes', type: 'bytes' };
- it('Less than 32 bytes', async () => {
- const bytesDataType = new AbiEncoder.Bytes(testBytesDataItem);
- bytesDataType.assignValue('0x010203');
- const expectedAbiEncodedBytes =
- '0x00000000000000000000000000000000000000000000000000000000000000030102030000000000000000000000000000000000000000000000000000000000';
+ it('Spans multiple EVM words', async () => {
+ // Create DataType object
+ const testDataItem = { name: 'Dynamic Bytes', type: 'bytes' };
+ const dataType = new AbiEncoder.Bytes(testDataItem);
+ // Construct args to be encoded
+ // Note: There will be padding because this is a bytes32 but we are only passing in 4 bytes.
+ const args = '0x' + '61'.repeat(40);
+ // Encode Args and validate result
+ const encodedArgs = dataType.encode(args);
+ const expectedEncodedArgs = '0x000000000000000000000000000000000000000000000000000000000000002861616161616161616161616161616161616161616161616161616161616161616161616161616161000000000000000000000000000000000000000000000000';
+ expect(encodedArgs).to.be.equal(expectedEncodedArgs);
+ // Decode Encoded Args and validate result
+ const decodedArgs = dataType.decode(encodedArgs);
+ const decodedArgsAsJson = JSON.stringify(decodedArgs);
+ const argsAsJson = JSON.stringify(args);
+ expect(decodedArgsAsJson).to.be.equal(argsAsJson);
+ });
- expect(bytesDataType.getHexValue()).to.be.equal(expectedAbiEncodedBytes);
+ it('Input as Buffer', async () => {
+ // Create DataType object
+ const testDataItem = { name: 'Dynamic Bytes', type: 'bytes' };
+ const dataType = new AbiEncoder.Bytes(testDataItem);
+ // Construct args to be encoded
+ // Note: There will be padding because this is a bytes32 but we are only passing in 4 bytes.
+ const args = '0x1a18bf61';
+ const argsAsBuffer = ethUtil.toBuffer(args);
+ // Encode Args and validate result
+ const encodedArgs = dataType.encode(argsAsBuffer);
+ const expectedEncodedArgs = '0x00000000000000000000000000000000000000000000000000000000000000041a18bf6100000000000000000000000000000000000000000000000000000000';
+ expect(encodedArgs).to.be.equal(expectedEncodedArgs);
+ // Decode Encoded Args and validate result
+ const decodedArgs = dataType.decode(encodedArgs);
+ const decodedArgsAsJson = JSON.stringify(decodedArgs);
+ const argsAsJson = JSON.stringify(args);
+ expect(decodedArgsAsJson).to.be.equal(argsAsJson);
});
- it('Greater than 32 bytes', async () => {
- const bytesDataType = new AbiEncoder.Bytes(testBytesDataItem);
- const testValue = '0x' + '61'.repeat(40);
- bytesDataType.assignValue(testValue);
- const expectedAbiEncodedBytes =
- '0x000000000000000000000000000000000000000000000000000000000000002861616161616161616161616161616161616161616161616161616161616161616161616161616161000000000000000000000000000000000000000000000000';
- expect(bytesDataType.getHexValue()).to.be.equal(expectedAbiEncodedBytes);
+ it('Should throw when pass in bad hex (no 0x prefix)', async () => {
+ // Create DataType object
+ const testDataItem = { name: 'Static Bytes', type: 'bytes' };
+ const dataType = new AbiEncoder.Bytes(testDataItem);
+ // Construct args to be encoded
+ const args = '01';
+ // Encode Args and validate result
+ expect(() => { dataType.encode(args) }).to.throw("Tried to encode non-hex value. Value must inlcude '0x' prefix. Got '01'");
});
- // @TODO: Add test for throw on half-byte
- // @TODO: Test with no 0x prefix
- // @TODO: Test with Buffer as input
+ it('Should throw when pass in bad hex (include a half-byte)', async () => {
+ // Create DataType object
+ const testDataItem = { name: 'Static Bytes', type: 'bytes' };
+ const dataType = new AbiEncoder.Bytes(testDataItem);
+ // Construct args to be encoded
+ const args = '0x010';
+ // Encode Args and validate result
+ expect(() => { dataType.encode(args) }).to.throw('Tried to assign 0x010, which is contains a half-byte. Use full bytes only.');
+ });
});
- describe('String', () => {
- const testStringDataItem = { name: 'testString', type: 'string' };
- it('Less than 32 bytes', async () => {
- const stringDataType = new AbiEncoder.SolString(testStringDataItem);
- stringDataType.assignValue('five');
- const expectedAbiEncodedString =
- '0x00000000000000000000000000000000000000000000000000000000000000046669766500000000000000000000000000000000000000000000000000000000';
+ describe.only('String', () => {
+ it('Fits into one EVM word', async () => {
+ // Create DataType object
+ const testDataItem = { name: 'String', type: 'string' };
+ const dataType = new AbiEncoder.SolString(testDataItem);
+ // Construct args to be encoded
+ // Note: There will be padding because this is a bytes32 but we are only passing in 4 bytes.
+ const args = 'five';
+ // Encode Args and validate result
+ const encodedArgs = dataType.encode(args);
+ const expectedEncodedArgs = '0x00000000000000000000000000000000000000000000000000000000000000046669766500000000000000000000000000000000000000000000000000000000';
+ expect(encodedArgs).to.be.equal(expectedEncodedArgs);
+ // Decode Encoded Args and validate result
+ const decodedArgs = dataType.decode(encodedArgs);
+ const decodedArgsAsJson = JSON.stringify(decodedArgs);
+ const argsAsJson = JSON.stringify(args);
+ expect(decodedArgsAsJson).to.be.equal(argsAsJson);
+ });
- console.log(stringDataType.getHexValue());
- console.log(expectedAbiEncodedString);
- expect(stringDataType.getHexValue()).to.be.equal(expectedAbiEncodedString);
+ it('Spans multiple EVM words', async () => {
+ // Create DataType object
+ const testDataItem = { name: 'String', type: 'string' };
+ const dataType = new AbiEncoder.SolString(testDataItem);
+ // Construct args to be encoded
+ // Note: There will be padding because this is a bytes32 but we are only passing in 4 bytes.
+ const args = 'a'.repeat(40);
+ // Encode Args and validate result
+ const encodedArgs = dataType.encode(args);
+ const expectedEncodedArgs = '0x000000000000000000000000000000000000000000000000000000000000002861616161616161616161616161616161616161616161616161616161616161616161616161616161000000000000000000000000000000000000000000000000';
+ expect(encodedArgs).to.be.equal(expectedEncodedArgs);
+ // Decode Encoded Args and validate result
+ const decodedArgs = dataType.decode(encodedArgs);
+ const decodedArgsAsJson = JSON.stringify(decodedArgs);
+ const argsAsJson = JSON.stringify(args);
+ expect(decodedArgsAsJson).to.be.equal(argsAsJson);
});
- it('Greater than 32 bytes', async () => {
- const stringDataType = new AbiEncoder.SolString(testStringDataItem);
- const testValue = 'a'.repeat(40);
- stringDataType.assignValue(testValue);
- const expectedAbiEncodedString =
- '0x000000000000000000000000000000000000000000000000000000000000002861616161616161616161616161616161616161616161616161616161616161616161616161616161000000000000000000000000000000000000000000000000';
- expect(stringDataType.getHexValue()).to.be.equal(expectedAbiEncodedString);
+ it('String that begins with 0x prefix', async () => {
+ // Create DataType object
+ const testDataItem = { name: 'String', type: 'string' };
+ const dataType = new AbiEncoder.SolString(testDataItem);
+ // Construct args to be encoded
+ // Note: There will be padding because this is a bytes32 but we are only passing in 4 bytes.
+ const args = '0x' + 'a'.repeat(40);
+ // Encode Args and validate result
+ const encodedArgs = dataType.encode(args);
+ const expectedEncodedArgs = '0x000000000000000000000000000000000000000000000000000000000000002a30786161616161616161616161616161616161616161616161616161616161616161616161616161616100000000000000000000000000000000000000000000';
+ expect(encodedArgs).to.be.equal(expectedEncodedArgs);
+ // Decode Encoded Args and validate result
+ const decodedArgs = dataType.decode(encodedArgs);
+ const decodedArgsAsJson = JSON.stringify(decodedArgs);
+ const argsAsJson = JSON.stringify(args);
+ expect(decodedArgsAsJson).to.be.equal(argsAsJson);
});
- }); */
+ });
});