|
| 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