aboutsummaryrefslogtreecommitdiffstats
path: root/coder.encodeParam.js
blob: 60d1c618e485ace4b4a4660d0c6570e0fa41ad40 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var chai = require('chai');
var assert = chai.assert;
var coder = require('../lib/solidity/coder');


describe('lib/solidity/coder', function () {
    describe('encodeParam', function () {
        var test = function (t) {
            it('should turn ' + t.value + ' to ' + t.expected, function () {
                assert.equal(coder.encodeParam(t.type, t.value), t.expected);
            });
        };


        test({ type: 'int', value: 1,               expected: '0000000000000000000000000000000000000000000000000000000000000001'});
        test({ type: 'int', value: 16,              expected: '0000000000000000000000000000000000000000000000000000000000000010'});
        test({ type: 'int', value: -1,              expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
        test({ type: 'int', value: 0.1,             expected: '0000000000000000000000000000000000000000000000000000000000000000'});
        test({ type: 'int', value: 3.9,             expected: '0000000000000000000000000000000000000000000000000000000000000003'});
        test({ type: 'int256', value: 1,            expected: '0000000000000000000000000000000000000000000000000000000000000001'});
        test({ type: 'int256', value: 16,           expected: '0000000000000000000000000000000000000000000000000000000000000010'});
        test({ type: 'int256', value: -1,           expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
        test({ type: 'bytes32', value: 'gavofyork', expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
        test({ type: 'bytes', value: 'gavofyork',   expected: '0000000000000000000000000000000000000000000000000000000000000020' +
                                                              '0000000000000000000000000000000000000000000000000000000000000009' +
                                                              '6761766f66796f726b0000000000000000000000000000000000000000000000'});
        test({ type: 'int[]', value: [3],           expected: '0000000000000000000000000000000000000000000000000000000000000020' +
                                                              '0000000000000000000000000000000000000000000000000000000000000001' +
                                                              '0000000000000000000000000000000000000000000000000000000000000003'});
        test({ type: 'int256[]', value: [3],        expected: '0000000000000000000000000000000000000000000000000000000000000020' +
                                                              '0000000000000000000000000000000000000000000000000000000000000001' +
                                                              '0000000000000000000000000000000000000000000000000000000000000003'});
        test({ type: 'int[]', value: [1,2,3],       expected: '0000000000000000000000000000000000000000000000000000000000000020' +
                                                              '0000000000000000000000000000000000000000000000000000000000000003' +
                                                              '0000000000000000000000000000000000000000000000000000000000000001' +
                                                              '0000000000000000000000000000000000000000000000000000000000000002' +
                                                              '0000000000000000000000000000000000000000000000000000000000000003'});
        test({ type: 'bool', value: true,           expected: '0000000000000000000000000000000000000000000000000000000000000001'});
        test({ type: 'bool', value: false,          expected: '0000000000000000000000000000000000000000000000000000000000000000'});
        test({ type: 'address', value: '0x407d73d8a49eeb85d32cf465507dd71d507100c1',          
                                                    expected: '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1'});
        test({ type: 'real', value: 1,              expected: '0000000000000000000000000000000100000000000000000000000000000000'});
        test({ type: 'real', value: 2.125,          expected: '0000000000000000000000000000000220000000000000000000000000000000'});
        test({ type: 'real', value: 8.5,            expected: '0000000000000000000000000000000880000000000000000000000000000000'});
        test({ type: 'real', value: -1,             expected: 'ffffffffffffffffffffffffffffffff00000000000000000000000000000000'});
        test({ type: 'ureal', value: 1,             expected: '0000000000000000000000000000000100000000000000000000000000000000'});
        test({ type: 'ureal', value: 2.125,         expected: '0000000000000000000000000000000220000000000000000000000000000000'});
        test({ type: 'ureal', value: 8.5,           expected: '0000000000000000000000000000000880000000000000000000000000000000'});
    });
});


describe('lib/solidity/coder', function () {
    describe('encodeParams', function () {
        var test = function (t) {
            it('should turn ' + t.values + ' to ' + t.expected, function () {
                assert.equal(coder.encodeParams(t.types, t.values), t.expected);
            });
        };

         
        test({ types: ['int'], values: [1],                 expected: '0000000000000000000000000000000000000000000000000000000000000001'});
        test({ types: ['int'], values: [16],                expected: '0000000000000000000000000000000000000000000000000000000000000010'});
        test({ types: ['int'], values: [-1],                expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
        test({ types: ['int256'], values: [1],              expected: '0000000000000000000000000000000000000000000000000000000000000001'});
        test({ types: ['int256'], values: [16],             expected: '0000000000000000000000000000000000000000000000000000000000000010'});
        test({ types: ['int256'], values: [-1],             expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
        test({ types: ['bytes32'], values: ['gavofyork'],   expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
        test({ types: ['bytes'], values: ['gavofyork'],     expected: '0000000000000000000000000000000000000000000000000000000000000020' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000009' +
                                                                      '6761766f66796f726b0000000000000000000000000000000000000000000000'});
        test({ types: ['int[]'], values: [[3]],             expected: '0000000000000000000000000000000000000000000000000000000000000020' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000001' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000003'});
        test({ types: ['int256[]'], values: [[3]],          expected: '0000000000000000000000000000000000000000000000000000000000000020' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000001' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000003'});
        test({ types: ['int256[]'], values: [[1,2,3]],      expected: '0000000000000000000000000000000000000000000000000000000000000020' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000003' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000001' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000002' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000003'});
        test({ types: ['int[]', 'int[]'], values: [[1,2], [3,4]],             
                                                            expected: '0000000000000000000000000000000000000000000000000000000000000040' +
                                                                      '00000000000000000000000000000000000000000000000000000000000000a0' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000002' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000001' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000002' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000002' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000003' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000004'});
        test({ types: ['bytes32', 'int'], values: ['gavofyork', 5],
                                                            expected: '6761766f66796f726b0000000000000000000000000000000000000000000000' + 
                                                                      '0000000000000000000000000000000000000000000000000000000000000005'});
        test({ types: ['int', 'bytes32'], values: [5, 'gavofyork'],
                                                            expected: '0000000000000000000000000000000000000000000000000000000000000005' + 
                                                                      '6761766f66796f726b0000000000000000000000000000000000000000000000'});
        test({ types: ['bytes', 'int'], values: ['gavofyork', 5],
                                                            expected: '0000000000000000000000000000000000000000000000000000000000000040' + 
                                                                      '0000000000000000000000000000000000000000000000000000000000000005' + 
                                                                      '0000000000000000000000000000000000000000000000000000000000000009' + 
                                                                      '6761766f66796f726b0000000000000000000000000000000000000000000000'});
        test({ types: ['bytes', 'bool', 'int[]'], values: ['gavofyork', true, [1, 2, 3]],
                                                            expected: '0000000000000000000000000000000000000000000000000000000000000060' + 
                                                                      '0000000000000000000000000000000000000000000000000000000000000001' + 
                                                                      '00000000000000000000000000000000000000000000000000000000000000a0' + 
                                                                      '0000000000000000000000000000000000000000000000000000000000000009' + 
                                                                      '6761766f66796f726b0000000000000000000000000000000000000000000000' + 
                                                                      '0000000000000000000000000000000000000000000000000000000000000003' + 
                                                                      '0000000000000000000000000000000000000000000000000000000000000001' + 
                                                                      '0000000000000000000000000000000000000000000000000000000000000002' + 
                                                                      '0000000000000000000000000000000000000000000000000000000000000003'});
        test({ types: ['bytes', 'int[]'], values: ['gavofyork', [1, 2, 3]],
                                                            expected: '0000000000000000000000000000000000000000000000000000000000000040' + 
                                                                      '0000000000000000000000000000000000000000000000000000000000000080' + 
                                                                      '0000000000000000000000000000000000000000000000000000000000000009' + 
                                                                      '6761766f66796f726b0000000000000000000000000000000000000000000000' + 
                                                                      '0000000000000000000000000000000000000000000000000000000000000003' + 
                                                                      '0000000000000000000000000000000000000000000000000000000000000001' + 
                                                                      '0000000000000000000000000000000000000000000000000000000000000002' + 
                                                                      '0000000000000000000000000000000000000000000000000000000000000003'});
        test({ types: ['int', 'bytes'], values: [5, 'gavofyork'],
                                                            expected: '0000000000000000000000000000000000000000000000000000000000000005' + 
                                                                      '0000000000000000000000000000000000000000000000000000000000000040' + 
                                                                      '0000000000000000000000000000000000000000000000000000000000000009' + 
                                                                      '6761766f66796f726b0000000000000000000000000000000000000000000000'});
        test({ types: ['int', 'bytes', 'int', 'int', 'int', 'int[]'], values: [1, 'gavofyork', 2, 3, 4, [5, 6, 7]],
                                                            expected: '0000000000000000000000000000000000000000000000000000000000000001' +
                                                                      '00000000000000000000000000000000000000000000000000000000000000c0' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000002' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000003' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000004' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000100' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000009' +
                                                                      '6761766f66796f726b0000000000000000000000000000000000000000000000' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000003' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000005' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000006' +
                                                                      '0000000000000000000000000000000000000000000000000000000000000007'});
    });
});