Skip to content

Commit 9fcb7cb

Browse files
committed
add interface txSign and some test code
1 parent 0c56634 commit 9fcb7cb

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

src/submit.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,31 @@ Submit.prototype.submit = function (expectOpt) {
3434
});
3535
};
3636

37+
38+
/**
39+
* @return {JsonObject}
40+
* {"signedTransaction":"","id":""}
41+
*/
42+
Submit.prototype.txSign = function () {
43+
let self = this;
44+
return new Promise(function (resolve, reject) {
45+
try {
46+
self.prepareJson().then(function (prepared) {
47+
48+
self.txJSON = prepared.txJSON;
49+
let signedRet = self.signTx();
50+
resolve(signedRet);
51+
}).catch(function (error) {
52+
reject(error);
53+
});
54+
} catch (error) {
55+
reject(error);
56+
}
57+
});
58+
};
59+
60+
61+
3762
Submit.prototype.setMaxLedgerVersionOffset = function (maxLedgerVersionOffset) {
3863
this.instructions.maxLedgerVersionOffset = maxLedgerVersionOffset;
3964
};

test/testTxSign.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
'use strict'
2+
3+
4+
const ChainsqlAPI = require('../src/index');
5+
6+
const c = new ChainsqlAPI();
7+
8+
9+
var user = {
10+
secret: "xxeJcpbcFyGTFCxiGjeDEw1RCimFQ",
11+
address: "z44fybVuUn8jZxZRHpc3pJ62KQJgSEjzjk",
12+
publicKey: "cB4MLVsyn5MnoYHhApEyGtPCuEf9PAGDopmpB7yFwTbhUtzrjRRT"
13+
}
14+
15+
var owner = {
16+
secret: "xnoPBzXtMeMyMHUVTgbuqAfg1SUTb",
17+
address: "zHb9CJAWyB4zj91VRWn96DkukG4bwdtyTh"
18+
}
19+
20+
var sTableName = "tTable1";
21+
22+
main();
23+
24+
async function main() {
25+
try {
26+
27+
await c.connect('ws://127.0.0.1:6006');
28+
console.log('连接成功');
29+
c.as(owner);
30+
await testTxSign();
31+
32+
console.log('测试结束');
33+
} catch (e) {
34+
console.error(e);
35+
}
36+
}
37+
38+
async function testTxSign() {
39+
40+
var rawCreate = [
41+
{ 'field': 'id', 'type': 'int', 'length': 11, 'PK': 1, 'NN': 1 },
42+
{ 'field': 'name', 'type': 'varchar', 'length': 50, 'default': "" },
43+
{ 'field': 'age', 'type': 'int' }
44+
]
45+
var option = {
46+
confidential: false
47+
}
48+
49+
var rawInsert = [
50+
{ 'id': 1, 'age': 333, 'name': 'hello' },
51+
{ 'id': 2, 'age': 444, 'name': 'sss' },
52+
{ 'id': 3, 'age': 555, 'name': 'rrr' }
53+
];
54+
55+
var rawGrant = { select: true, insert: false, update: false, delete: true };
56+
57+
try {
58+
59+
// payment
60+
let paymentSign = await c.pay(user.address, 2000).txSign();
61+
console.log("payment Tx sign : " + JSON.stringify (paymentSign));
62+
63+
// create table
64+
// let createTableSign = await c.createTable(sTableName, rawCreate, option).txSign();
65+
// console.log("create table Tx sign : " + JSON.stringify(createTableSign));
66+
67+
// insert/grant 等 签名交易的生成需要保证表已存在
68+
// let insertTableSign = await c.table(sTableName).insert(rawInsert).txSign();
69+
// console.log("insert table Tx sign : " + JSON.stringify(insertTableSign));
70+
71+
// grant
72+
var grantSign = await c.grant(sTableName, user.address, rawGrant, user.publicKey).txSign();
73+
console.log("insert table Tx sign : " + JSON.stringify(grantSign));
74+
75+
} catch (error) {
76+
console.error(error);
77+
}
78+
79+
80+
}
81+
82+
83+

0 commit comments

Comments
 (0)