Skip to content

Commit 06d0b65

Browse files
authored
Merge branch 'chainsql-test' into develop
2 parents 267e3c2 + 061fe9d commit 06d0b65

18 files changed

Lines changed: 1945 additions & 114 deletions

lib/crypto.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
// requires
3-
const keypairs = require('chainsql-keypairs');
3+
const keypairs = require('chainsql-keypairs-test');
44

55
const hashjs = require('hash.js');
66
const elliptic = require('elliptic');

lib/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
const path = require('path');
33
const common = require('chainsql-lib').ChainsqlLibCommon;
4-
const keypairs = require('chainsql-keypairs');
4+
const keypairs = require('chainsql-keypairs-test');
55
const cryptoo = require('crypto');
66
const crypto = require('./crypto');
77
const opType = require('./config').opType;

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "chainsql",
3-
"version": "0.70.1",
2+
"name": "chainsql-test",
3+
"version": "1.0.1",
44
"description": "An database driver for chainsql ",
55
"main": "src/index.js",
66
"scripts": {
@@ -9,7 +9,7 @@
99
"dependencies": {
1010
"bignumber.js": "^2.4.0",
1111
"chainsql-keypairs": "^0.10.16",
12-
"chainsql-lib": "^0.17.44",
12+
"chainsql-lib": "~1.0.0",
1313
"co": "^4.6.0",
1414
"elliptic": "^5.1.0",
1515
"hash.js": "^1.0.3",
@@ -22,7 +22,8 @@
2222
"ws": "^7.2.0"
2323
},
2424
"devDependencies": {
25-
"mocha": "~2.3.3"
25+
"mocha": "~2.3.3",
26+
"chai": "^4.2.0"
2627
},
2728
"repository": {
2829
"type": "git",

src/index.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22
const crypto = require('../lib/crypto');
3-
const keypairs = require('chainsql-keypairs');
3+
const keypairs = require('chainsql-keypairs-test');
44
const EventManager = require('./eventManager')
55
const _ = require('lodash');
66

@@ -20,6 +20,7 @@ const Table = require('./table');
2020
const Contract = require('./smartContract');
2121
const util = require('../lib/util');
2222
const { utils } = require('elliptic');
23+
const co = require('co');
2324
const opType = require('../lib/config').opType;
2425
const convertStringToHex = util.convertStringToHex;
2526
const getCryptAlgTypeFromAccout = util.getCryptAlgTypeFromAccout;
@@ -320,6 +321,10 @@ ChainsqlAPI.prototype.createTable = function (name, raw, inputOpt) {
320321
var token = generateToken(that.connect.secret);
321322
var symKey = decodeToken(that, token);
322323
var regSoftGMSeed = /^[a-zA-Z1-9]{51,51}/
324+
325+
326+
// 原始的大小
327+
console.log("pre :",payment.raw);
323328

324329
if(that.connect.secret === "gmAlg") {
325330
payment.raw = crypto.symEncrypt(symKey, payment.raw, "gmAlg").toUpperCase();
@@ -329,7 +334,13 @@ ChainsqlAPI.prototype.createTable = function (name, raw, inputOpt) {
329334
else {
330335
payment.raw = crypto.symEncrypt(symKey, payment.raw).toUpperCase();
331336
}
337+
338+
//
339+
console.log("after :",payment.raw);
340+
332341
payment.token = token.toUpperCase();
342+
343+
console.log("token :",payment.token);
333344
} else {
334345
payment.raw = convertStringToHex(payment.raw);
335346
}
@@ -685,7 +696,6 @@ function handleCommit(ChainSQL, object, resolve, reject) {
685696
var secret = decodeToken(ChainSQL, token);
686697
if (cache[i].Raw) {
687698
if (cache[i].OpType != opType.t_grant) {
688-
689699
var regSoftGMSeed = /^[a-zA-Z1-9]{51,51}/
690700
let algType = "aes";
691701
if(ChainSQL.connect.secret === "gmAlg"){
@@ -1136,14 +1146,11 @@ ChainsqlAPI.prototype.createSchema = function(schemaInfo){
11361146
let bValid = (schemaInfo !== undefined) && (schemaInfo.SchemaName !== undefined) && (schemaInfo.WithState !== undefined) &&
11371147
(schemaInfo.Validators !== undefined) && (schemaInfo.Validators instanceof Array) &&
11381148
(schemaInfo.PeerList !== undefined) && (schemaInfo.PeerList instanceof Array);
1139-
1140-
if(!bValid){
1149+
1150+
if(!bValid){
11411151
throw new Error("Invalid schemaInfo parameter");
11421152
}
11431153

1144-
// 继承自主链的状态
1145-
// 锚定区块的hash值
1146-
11471154
var peerlists = []
11481155
var i = 0;
11491156
var len = schemaInfo.PeerList.length

src/smartContract.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var util = require('../lib/util');
44

55
const _ = require('lodash');
66
var chainsqlLibUtils = require('chainsql-lib').ChainsqlLibUtil;
7-
const keypairs = require('chainsql-keypairs');
7+
const keypairs = require('chainsql-keypairs-test');
88
const chainsqlUtils = require('../lib/util');
99
const chainsqlError = require('../lib/error');
1010
var abi = require('web3-eth-abi');

src/table.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Table extends Submit {
2222
this.cache = [];
2323
this.nameInDB = '';
2424
this.confidential = true;
25+
this.txsHashFillField = null;
2526
}
2627

2728
submit (cb) {
@@ -61,13 +62,18 @@ class Table extends Submit {
6162
}
6263
}
6364

64-
Table.prototype.insert = function(raw, field) {
65+
Table.prototype.insert = function(raw, autoField ,txsHashFillField) {
6566
if (!this.tab) throw chainsqlError('you must appoint the table name');
6667
if (this.exec !== '' && this.exec !== 'r_insert') throw chainsqlError('Object can not hava function insert');
6768
var that = this;
68-
if (field) {
69-
this.field = field;
69+
if (autoField) {
70+
this.field = autoField;
71+
}
72+
73+
if (txsHashFillField) {
74+
this.txsHashFillField = txsHashFillField;
7075
}
76+
7177
if (Object.prototype.toString.call(raw) === '[object Array]') {
7278
raw.forEach(function(item) {
7379
that.query.push(item);
@@ -92,7 +98,7 @@ Table.prototype.insert = function(raw, field) {
9298
}
9399
}
94100

95-
Table.prototype.update = function(raw,field) {
101+
Table.prototype.update = function(raw,field,txsHashFillField) {
96102
if (!this.tab) throw chainsqlError('you must appoint the table name');
97103
if (this.exec !== 'r_get') throw chainsqlError('Object can not hava function update');
98104
this.query.unshift(raw);
@@ -101,6 +107,10 @@ Table.prototype.update = function(raw,field) {
101107
this.field = field;
102108
}
103109

110+
if (txsHashFillField) {
111+
this.txsHashFillField = txsHashFillField;
112+
}
113+
104114
this.exec = 'r_update';
105115
if (this.transaction) {
106116
this.cache.push({
@@ -412,10 +422,18 @@ Table.prototype.prepareJson = function() {
412422
}],
413423
tsType: 'SQLStatement'
414424
};
415-
if ( (that.exec == 'r_insert' || that.exec == 'r_update') && that.field) {
416-
payment.autoFillField = convertStringToHex(that.field);
417-
}
425+
if ( (that.exec == 'r_insert' || that.exec == 'r_update') ) {
426+
427+
if(that.field){
428+
payment.autoFillField = convertStringToHex(that.field);
429+
}
418430

431+
if ( that.txsHashFillField) {
432+
payment.txsHashFillField = convertStringToHex(that.txsHashFillField);
433+
}
434+
435+
}
436+
419437
return new Promise(function (resolve, reject) {
420438
prepareTable(that, payment, resolve, reject);
421439
});

src/tablePayment.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ function createPaymentTransaction(paymentArgument) {
6161
AutoFillField:payment.autoFillField,
6262
Token:payment.token,
6363
StrictMode: payment.strictMode,
64-
OperationRule: payment.operationRule
64+
OperationRule: payment.operationRule,
65+
TxsHashFillField:payment.txsHashFillField,
6566
}
6667
return txJSON;
6768
}

test/contract-tabletxs-test.js

Lines changed: 520 additions & 0 deletions
Large diffs are not rendered by default.

test/crypto-test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict' // eslint-disable-line strict
2+
3+
const assert = require('assert')
4+
const ChainsqlAPI = require('../src/index');
5+
6+
const c = new ChainsqlAPI();
7+
8+
function hex2a(hexx) {
9+
var hex = hexx.toString();//force conversion
10+
var str = '';
11+
for (var i = 0; (i < hex.length && hex.substr(i, 2) !== '00'); i += 2)
12+
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
13+
return str;
14+
}
15+
16+
17+
describe('crypto', () => {
18+
19+
it('eciesEncrypt', async function(){
20+
21+
var wallet = c.generateAddress()
22+
var msg = "hello world"
23+
var cipher = c.eciesEncrypt(msg,wallet.publicKey)
24+
var plainTxt = c.eciesDecrypt(cipher,wallet.secret);
25+
assert.equal(msg,plainTxt.toString())
26+
})
27+
28+
29+
30+
it('eciesEncrypt', async function(){
31+
32+
var wallet = c.generateAddress()
33+
var msg = "hello world"
34+
var cipher = c.eciesEncrypt(msg,wallet.publicKey)
35+
var plainTxt = c.eciesDecrypt(cipher,wallet.secret)
36+
assert.equal(msg,plainTxt.toString())
37+
})
38+
39+
40+
it('symEncrypt && symDecrypt', async function(){
41+
42+
var msg = "hello world"
43+
var symKey = "1111111111111111";
44+
var cipher = c.symEncrypt(symKey,msg)
45+
var plainText = c.symDecrypt(symKey,cipher)
46+
assert.equal(msg,plainText)
47+
var cipherGM = c.symEncrypt(symKey,msg,"softGMAlg")
48+
49+
var plainTextGM = c.symDecrypt(symKey,cipherGM,"softGMAlg")
50+
assert.equal(msg,hex2a(plainTextGM))
51+
})
52+
53+
it('asymEncrypt && asymDecrypt', async function(){
54+
55+
var accountInfo = c.generateAddress({algorithm:"softGMAlg"})
56+
var msg = "hello world"
57+
58+
var cipher = c.asymEncrypt(msg,accountInfo.publicKey)
59+
var plainText = c.asymDecrypt(cipher,accountInfo.secret)
60+
assert.equal(msg,hex2a(plainText))
61+
})
62+
63+
})

0 commit comments

Comments
 (0)