Skip to content

Commit c45a381

Browse files
gaborh-dacorecode
authored andcommitted
Make sdk/node tests work
This commit adds missing files needed for by the SDK/node. These files are brought over from <https://github.com/hyperledger/fabric/tree/master/sdk/node/lib>, and are needed for building the sdk/node and for unit testing. These files were left out during the migration as they were listed in the .gitignore file. In addition, this patchset: - Adds fixes (use byte arrays as arguments and use Txid instead of Uuid). - Updates hash.js to also reuse SJCL's bytes codec for converting from and to BitArrays when using sha3 on the client-side. Change-Id: I8e7f5ef9be7f187bcd2ca6948f04c9b122b9ec31 Signed-off-by: Gabor Hosszu <[email protected]> Signed-off-by: Jonathan Levi <[email protected]>
1 parent 47c3f6c commit c45a381

File tree

5 files changed

+346
-10
lines changed

5 files changed

+346
-10
lines changed

sdk/node/.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ node_modules/*
44
/node.iml
55
/typings/
66
/doc/
7-
/lib/
7+
/lib/*
8+
!/lib/protos/google
9+
!/lib/hash.js

sdk/node/lib/hash.js

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/**
2+
* Copyright 2016 IBM
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
/**
17+
* Licensed Materials - Property of IBM
18+
* © Copyright IBM Corp. 2016
19+
*/
20+
21+
/**
22+
* Implement hash primitives.
23+
* Currently SHA3 is implemented, but needs to also add SHA2.
24+
*
25+
* NOTE: This is in pure java script to be compatible with the sjcl.hmac function.
26+
*/
27+
var sjcl = require('sjcl');
28+
var jssha = require('jssha');
29+
var sha3_256 = require('js-sha3').sha3_256;
30+
var sha3_384 = require('js-sha3').sha3_384;
31+
32+
33+
hash_sha2_256 = function (hash) {
34+
35+
if (hash) {
36+
this._hash = hash._hash;
37+
}
38+
else {
39+
this.reset();
40+
}
41+
};
42+
43+
hash_sha2_256.hash = function (data) {
44+
return (new sjcl.hash.sha256()).update(data).finalize();
45+
};
46+
47+
hash_sha2_256.prototype = {
48+
49+
blockSize: 512,
50+
51+
reset: function () {
52+
this._hash = new sjcl.hash.sha256();
53+
this._hash.reset();
54+
},
55+
56+
update: function (data) {
57+
this._hash.update(data);
58+
return this;
59+
},
60+
61+
finalize: function () {
62+
var hash = this._hash.finalize();
63+
this.reset();
64+
return hash;
65+
66+
}
67+
};
68+
69+
70+
var hash_sha3_256 = function (hash) {
71+
72+
if (hash) {
73+
this._hash = hash._hash;
74+
}
75+
else {
76+
this.reset();
77+
}
78+
};
79+
80+
hash_sha3_256.hash = function (data) {
81+
var hashBits = sjcl.codec.hex.toBits(sha3_256(bitsToBytes(data)));
82+
return hashBits;
83+
};
84+
85+
hash_sha3_256.prototype = {
86+
87+
blockSize: 1088,
88+
89+
reset: function () {
90+
this._hash = sha3_256.create();
91+
},
92+
93+
update: function (data) {
94+
this._hash.update(bitsToBytes(data));
95+
return this;
96+
},
97+
98+
finalize: function () {
99+
var hash = this._hash.hex();
100+
var hashBits = sjcl.codec.hex.toBits(hash);
101+
this.reset();
102+
return hashBits;
103+
104+
}
105+
};
106+
107+
var hash_sha3_384 = function (hash) {
108+
109+
if (hash) {
110+
this._hash = hash._hash;
111+
}
112+
else {
113+
this.reset();
114+
}
115+
};
116+
117+
hash_sha3_384.hash = function (data) {
118+
var hashBits = sjcl.codec.hex.toBits(sha3_384(bitsToBytes(data)));
119+
return hashBits;
120+
};
121+
122+
hash_sha3_384.prototype = {
123+
124+
blockSize: 832,
125+
126+
reset: function () {
127+
this._hash = sha3_384.create();
128+
},
129+
130+
update: function (data) {
131+
this._hash.update(bitsToBytes(data));
132+
return this;
133+
},
134+
135+
finalize: function () {
136+
var hash = this._hash.hex();
137+
var hashBits = sjcl.codec.hex.toBits(hash);
138+
//debug('finalize hashBits:\n',hashBits)
139+
this.reset();
140+
return hashBits;
141+
142+
}
143+
};
144+
145+
/**
146+
* Convert from a bitArray to bytes (using SJCL's codec)
147+
* @param {bits} a bitArray to convert from
148+
* @return {bytes} the bytes converted from the bitArray
149+
*/
150+
bitsToBytes = function (bits) {
151+
return sjcl.codec.bytes.fromBits(bits);
152+
}
153+
154+
/**
155+
* Convert from bytes to a bitArray (using SJCL's codec)
156+
* @param {bytes} a bytes to convert from
157+
* @return {bitArray} the bitArray converted from bytes
158+
*/
159+
bytesToBits = function (bytes) {
160+
return sjcl.codec.bytes.toBits(bytes);
161+
}
162+
163+
exports.hash_sha3_256 = hash_sha3_256;
164+
exports.hash_sha3_384 = hash_sha3_384;
165+
exports.hash_sha2_256 = hash_sha2_256;
166+
exports.sha2_256 = function (data) {
167+
return bitsToBytes(new sjcl.hash.sha256().update(bytesToBits(data)).finalize());
168+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Protocol Buffers - Google's data interchange format
2+
// Copyright 2008 Google Inc. All rights reserved.
3+
// https://developers.google.com/protocol-buffers/
4+
//
5+
// Redistribution and use in source and binary forms, with or without
6+
// modification, are permitted provided that the following conditions are
7+
// met:
8+
//
9+
// * Redistributions of source code must retain the above copyright
10+
// notice, this list of conditions and the following disclaimer.
11+
// * Redistributions in binary form must reproduce the above
12+
// copyright notice, this list of conditions and the following disclaimer
13+
// in the documentation and/or other materials provided with the
14+
// distribution.
15+
// * Neither the name of Google Inc. nor the names of its
16+
// contributors may be used to endorse or promote products derived from
17+
// this software without specific prior written permission.
18+
//
19+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
31+
syntax = "proto3";
32+
33+
package google.protobuf;
34+
35+
option csharp_namespace = "Google.Protobuf.WellKnownTypes";
36+
option go_package = "github.com/golang/protobuf/ptypes/empty";
37+
option java_package = "com.google.protobuf";
38+
option java_outer_classname = "EmptyProto";
39+
option java_multiple_files = true;
40+
option java_generate_equals_and_hash = true;
41+
option objc_class_prefix = "GPB";
42+
option cc_enable_arenas = true;
43+
44+
// A generic empty message that you can re-use to avoid defining duplicated
45+
// empty messages in your APIs. A typical example is to use it as the request
46+
// or the response type of an API method. For instance:
47+
//
48+
// service Foo {
49+
// rpc Bar(google.protobuf.Empty) returns (google.protobuf.Empty);
50+
// }
51+
//
52+
// The JSON representation for `Empty` is empty JSON object `{}`.
53+
message Empty {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Protocol Buffers - Google's data interchange format
2+
// Copyright 2008 Google Inc. All rights reserved.
3+
// https://developers.google.com/protocol-buffers/
4+
//
5+
// Redistribution and use in source and binary forms, with or without
6+
// modification, are permitted provided that the following conditions are
7+
// met:
8+
//
9+
// * Redistributions of source code must retain the above copyright
10+
// notice, this list of conditions and the following disclaimer.
11+
// * Redistributions in binary form must reproduce the above
12+
// copyright notice, this list of conditions and the following disclaimer
13+
// in the documentation and/or other materials provided with the
14+
// distribution.
15+
// * Neither the name of Google Inc. nor the names of its
16+
// contributors may be used to endorse or promote products derived from
17+
// this software without specific prior written permission.
18+
//
19+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
31+
syntax = "proto3";
32+
33+
package google.protobuf;
34+
35+
option csharp_namespace = "Google.Protobuf.WellKnownTypes";
36+
option cc_enable_arenas = true;
37+
option java_package = "com.google.protobuf";
38+
option java_outer_classname = "TimestampProto";
39+
option java_multiple_files = true;
40+
option java_generate_equals_and_hash = true;
41+
option objc_class_prefix = "GPB";
42+
43+
// A Timestamp represents a point in time independent of any time zone
44+
// or calendar, represented as seconds and fractions of seconds at
45+
// nanosecond resolution in UTC Epoch time. It is encoded using the
46+
// Proleptic Gregorian Calendar which extends the Gregorian calendar
47+
// backwards to year one. It is encoded assuming all minutes are 60
48+
// seconds long, i.e. leap seconds are "smeared" so that no leap second
49+
// table is needed for interpretation. Range is from
50+
// 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z.
51+
// By restricting to that range, we ensure that we can convert to
52+
// and from RFC 3339 date strings.
53+
// See [https://www.ietf.org/rfc/rfc3339.txt](https://www.ietf.org/rfc/rfc3339.txt).
54+
//
55+
// Example 1: Compute Timestamp from POSIX `time()`.
56+
//
57+
// Timestamp timestamp;
58+
// timestamp.set_seconds(time(NULL));
59+
// timestamp.set_nanos(0);
60+
//
61+
// Example 2: Compute Timestamp from POSIX `gettimeofday()`.
62+
//
63+
// struct timeval tv;
64+
// gettimeofday(&tv, NULL);
65+
//
66+
// Timestamp timestamp;
67+
// timestamp.set_seconds(tv.tv_sec);
68+
// timestamp.set_nanos(tv.tv_usec * 1000);
69+
//
70+
// Example 3: Compute Timestamp from Win32 `GetSystemTimeAsFileTime()`.
71+
//
72+
// FILETIME ft;
73+
// GetSystemTimeAsFileTime(&ft);
74+
// UINT64 ticks = (((UINT64)ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
75+
//
76+
// // A Windows tick is 100 nanoseconds. Windows epoch 1601-01-01T00:00:00Z
77+
// // is 11644473600 seconds before Unix epoch 1970-01-01T00:00:00Z.
78+
// Timestamp timestamp;
79+
// timestamp.set_seconds((INT64) ((ticks / 10000000) - 11644473600LL));
80+
// timestamp.set_nanos((INT32) ((ticks % 10000000) * 100));
81+
//
82+
// Example 4: Compute Timestamp from Java `System.currentTimeMillis()`.
83+
//
84+
// long millis = System.currentTimeMillis();
85+
//
86+
// Timestamp timestamp = Timestamp.newBuilder().setSeconds(millis / 1000)
87+
// .setNanos((int) ((millis % 1000) * 1000000)).build();
88+
//
89+
//
90+
// Example 5: Compute Timestamp from current time in Python.
91+
//
92+
// now = time.time()
93+
// seconds = int(now)
94+
// nanos = int((now - seconds) * 10**9)
95+
// timestamp = Timestamp(seconds=seconds, nanos=nanos)
96+
//
97+
//
98+
message Timestamp {
99+
100+
// Represents seconds of UTC time since Unix epoch
101+
// 1970-01-01T00:00:00Z. Must be from from 0001-01-01T00:00:00Z to
102+
// 9999-12-31T23:59:59Z inclusive.
103+
int64 seconds = 1;
104+
105+
// Non-negative fractions of a second at nanosecond resolution. Negative
106+
// second values with fractions must still have non-negative nanos values
107+
// that count forward in time. Must be from 0 to 999,999,999
108+
// inclusive.
109+
int32 nanos = 2;
110+
}

0 commit comments

Comments
 (0)