Commit 828fb271 828fb271589a861eebd48c295cf30f0a509af475 by wulianyou

first commit

0 parents
Showing 71 changed files with 4768 additions and 0 deletions
1 'use strict';
2
3 module.exports = {
4 write: true,
5 prefix: '^',
6 plugin: 'autod-egg',
7 test: [
8 'test',
9 'benchmark',
10 ],
11 dep: [
12 'egg',
13 'egg-scripts',
14 ],
15 devdep: [
16 'egg-ci',
17 'egg-bin',
18 'egg-mock',
19 'autod',
20 'autod-egg',
21 'eslint',
22 'eslint-config-egg',
23 ],
24 exclude: [
25 './test/fixtures',
26 './dist',
27 ],
28 };
29
1 module.exports = {
2 nethash: '0ab796cd'
3 }
...\ No newline at end of file ...\ No newline at end of file
1 {
2 "extends": "eslint-config-egg"
3 }
1 # This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2 # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3
4 name: Node.js CI
5
6 on:
7 push:
8 branches:
9 - main
10 - master
11 pull_request:
12 branches:
13 - main
14 - master
15 schedule:
16 - cron: '0 2 * * *'
17
18 jobs:
19 build:
20 runs-on: ${{ matrix.os }}
21
22 strategy:
23 fail-fast: false
24 matrix:
25 node-version: [10]
26 os: [ubuntu-latest, windows-latest, macos-latest]
27
28 steps:
29 - name: Checkout Git Source
30 uses: actions/checkout@v2
31
32 - name: Use Node.js ${{ matrix.node-version }}
33 uses: actions/setup-node@v1
34 with:
35 node-version: ${{ matrix.node-version }}
36
37 - name: Install Dependencies
38 run: npm i -g npminstall && npminstall
39
40 - name: Continuous Integration
41 run: npm run ci
42
43 - name: Code Coverage
44 uses: codecov/codecov-action@v1
45 with:
46 token: ${{ secrets.CODECOV_TOKEN }}
1 logs/
2 npm-debug.log
3 yarn-error.log
4 node_modules/
5 package-lock.json
6 yarn.lock
7 coverage/
8 .idea/
9 run/
10 .DS_Store
11 *.sw*
12 *.un~
13 typings/
14 .nyc_output/
1 'use strict';
2
3 const Controller = require('egg').Controller;
4 const nodeSdk = require('@ddn/node-sdk').default;
5 class TransferController extends Controller {
6 async postTransaction() {
7 const { ctx, config } = this;
8 const body = ctx.request.body;
9 const rule = {
10 secret: { type: 'string' },
11 amount: { type: 'string' },
12 transaction: { type: 'string' },
13 };
14 console.log(body);
15 try {
16 ctx.validate(rule);
17 } catch (error) {
18 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
19 return;
20 }
21 const oldNetTransaction = JSON.parse(body.transaction);
22 if (oldNetTransaction.recipientId !== config.ed25519Address) {
23 ctx.helper.err({
24 ctx,
25 err: '接收地址不正确,请使用正确的地址',
26 });
27 return;
28 }
29 // const oldNetTransaction = JSON.stringify(body.transaction);
30 // 旧网转出
31 const oldNetData = await this.service.sdk.pushTransaction(JSON.stringify({ transaction: oldNetTransaction }), config.ed25519Url);
32 console.log('就往转账', oldNetData, typeof oldNetData);
33 if (!oldNetData || !oldNetData.success) {
34 ctx.helper.err({
35 ctx,
36 data: oldNetData,
37 });
38 return;
39 }
40 const result = await ctx.model.Transfer.create({ toAddress: body.receiverAddress, amount: body.amount, transaction: body.transaction, address: body.senderAddress });
41 ctx.helper.success({
42 ctx,
43 data: result,
44 });
45
46 // const transaction = await nodeSdk.transaction.createTransaction(body.receiverAccount, body.amount, body.remark || '网络升级转账', config.naclSecret);
47 // const trs = JSON.stringify({ transaction });
48 // // 上链操作
49 // const data = await this.service.sdk.pushTransaction(trs);
50 // if (data.success) {
51 // ctx.helper.success({
52 // ctx,
53 // data,
54 // });
55 // ctx.model.Transfer.update({ id: result.id }, { finish: true });
56 // } else {
57 // ctx.helper.err({
58 // ctx,
59 // data,
60 // });
61 // ctx.model.Transfer.update({ id: result.id }, { finish: false });
62 // }
63
64 }
65 async getBalance() {
66 const { ctx, config } = this;
67 const query = ctx.request.query;
68 const rule = {
69 address: { type: 'string' },
70 };
71 console.log(query);
72 try {
73 ctx.validate(rule, query);
74 } catch (error) {
75 console.log(error);
76 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
77 return;
78 }
79 const data = await this.service.sdk.getAccount(query.address, config.ed25519Url);
80 ctx.helper.success({
81 ctx,
82 data,
83 });
84 }
85 async getTransaction() {
86 const { ctx, config } = this;
87 const query = ctx.request.query;
88 const rule = {
89 address: { type: 'string' },
90 };
91 console.log(query);
92 try {
93 ctx.validate(rule, query);
94 } catch (error) {
95 console.log(error);
96 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
97 return;
98 }
99 const data = await this.service.sdk.getAccount(query.address, config.naclUrl);
100 ctx.helper.success({
101 ctx,
102 data,
103 });
104 }
105 async asyncAccount() {
106 const { ctx, config } = this;
107 const limit = 100;
108 let asyncCount = 0;
109 let errAsyncAccount = 0;
110 const data = await this.service.sdk.getAccountCount(config.ed25519Url);
111 if (data.success) {
112 const length = Math.ceil(data.count / 100);
113 for (let index = 0; index < length; index++) {
114 const acountList = await this.service.sdk.getAccountList(index + 1, limit, config.ed25519Url);
115 console.log(acountList)
116 if (acountList.success) {
117 for (let index = 0; index < acountList.accounts.length; index++) {
118 const element = acountList.accounts[index];
119 if (element.balance == 0) continue;
120 const result = await this.app.model.Accounts.create({ balance: element.balance, address: element.address, publicKey: element.publicKey });
121 if (!result) {
122 errAsyncAccount++;
123 this.ctx.logger.error(`存入账户数据出错 数据:${JSON.stringify(element)},地址:${config.ed25519Url},error: ${JSON.stringify(result)}`);
124 } else {
125 asyncCount++;
126 }
127 }
128 } else {
129 this.ctx.logger.error(`同步账户数据出错:页码:${index + 1},每页条数:${limit},地址:${config.ed25519Url}`);
130 }
131 }
132 } else {
133 this.ctx.logger.error(`同步账户数据请求账户数量出错:${config.ed25519Url}`);
134 }
135 ctx.helper.success({
136 ctx,
137 data: { 出错数量: errAsyncAccount, 同步数量: data.count, 已同步数量: asyncCount },
138 });
139 }
140 }
141
142 module.exports = TransferController;
1 'use strict';
2
3 const Controller = require('egg').Controller;
4 const nodeSdk = require('@ddn/node-sdk').default;
5 class TransferController extends Controller {
6 async postTransaction() {
7 const { ctx, config } = this;
8 const body = ctx.request.body;
9 const rule = {
10 secret: { type: 'string' },
11 amount: { type: 'string' },
12 transaction: { type: 'string' },
13 };
14 console.log(body);
15 try {
16 ctx.validate(rule);
17 } catch (error) {
18 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
19 return;
20 }
21 const oldNetTransaction = JSON.parse(body.transaction);
22 if (oldNetTransaction.recipientId !== config.ed25519Address) {
23 ctx.helper.err({
24 ctx,
25 err: '接收地址不正确,请使用正确的地址',
26 });
27 return;
28 }
29 // const oldNetTransaction = JSON.stringify(body.transaction);
30 // 旧网转出
31 const oldNetData = await this.service.sdk.pushTransaction(JSON.stringify({ transaction: oldNetTransaction }), config.ed25519Url);
32 console.log('就往转账', oldNetData, typeof oldNetData);
33 if (!oldNetData || !oldNetData.success) {
34 ctx.helper.err({
35 ctx,
36 data: oldNetData,
37 });
38 return;
39 }
40 const result = await ctx.model.Transfer.create({ toAddress: body.receiverAddress, amount: body.amount, transaction: body.transaction, address: body.senderAddress });
41 ctx.helper.success({
42 ctx,
43 data: result,
44 });
45
46 // const transaction = await nodeSdk.transaction.createTransaction(body.receiverAccount, body.amount, body.remark || '网络升级转账', config.naclSecret);
47 // const trs = JSON.stringify({ transaction });
48 // // 上链操作
49 // const data = await this.service.sdk.pushTransaction(trs);
50 // if (data.success) {
51 // ctx.helper.success({
52 // ctx,
53 // data,
54 // });
55 // ctx.model.Transfer.update({ id: result.id }, { finish: true });
56 // } else {
57 // ctx.helper.err({
58 // ctx,
59 // data,
60 // });
61 // ctx.model.Transfer.update({ id: result.id }, { finish: false });
62 // }
63
64 }
65 async getBalance() {
66 const { ctx, config } = this;
67 const query = ctx.request.query;
68 const rule = {
69 address: { type: 'string' },
70 };
71 console.log(query);
72 try {
73 ctx.validate(rule, query);
74 } catch (error) {
75 console.log(error);
76 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
77 return;
78 }
79 const data = await this.service.sdk.getAccount(query.address, config.ed25519Url);
80 ctx.helper.success({
81 ctx,
82 data,
83 });
84 }
85 async getTransaction() {
86 const { ctx, config } = this;
87 const query = ctx.request.query;
88 const rule = {
89 address: { type: 'string' },
90 };
91 console.log(query);
92 try {
93 ctx.validate(rule, query);
94 } catch (error) {
95 console.log(error);
96 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
97 return;
98 }
99 const data = await this.service.sdk.getAccount(query.address, config.naclUrl);
100 ctx.helper.success({
101 ctx,
102 data,
103 });
104 }
105 async asyncAccount() {
106 const { ctx, config } = this;
107 const limit = 100;
108 let asyncCount = 0;
109 let errAsyncAccount = 0;
110 let balanceIsZero = 0;
111 const data = await this.service.sdk.getAccountCount(config.ed25519Url);
112 if (data.success) {
113 const length = Math.ceil(data.count / 100);
114 for (let index = 0; index < length; index++) {
115 const acountList = await this.service.sdk.getAccountList(index + 1, limit, config.ed25519Url);
116 console.log(acountList)
117 if (acountList.success) {
118 for (let index = 0; index < acountList.accounts.length; index++) {
119 const element = acountList.accounts[index];
120 if (element.balance == 0) {
121 balanceIsZero++
122 continue;
123 }
124 const result = await this.app.model.Accounts.create({ balance: element.balance, address: element.address, publicKey: element.publicKey });
125 if (!result) {
126 errAsyncAccount++;
127 this.ctx.logger.error(`存入账户数据出错 数据:${JSON.stringify(element)},地址:${config.ed25519Url},error: ${JSON.stringify(result)}`);
128 } else {
129 asyncCount++;
130 }
131 }
132 } else {
133 this.ctx.logger.error(`同步账户数据出错:页码:${index + 1},每页条数:${limit},地址:${config.ed25519Url}`);
134 }
135 }
136 } else {
137 this.ctx.logger.error(`同步账户数据请求账户数量出错:${config.ed25519Url}`);
138 }
139 ctx.helper.success({
140 ctx,
141 data: { 出错数量: errAsyncAccount, 同步数量: data.count, 已同步数量: asyncCount, 余额为零数量: balanceIsZero },
142 });
143 }
144 }
145
146 module.exports = TransferController;
1 'use strict';
2
3 const Controller = require('egg').Controller;
4 const nodeSdk = require('@ddn/node-sdk').default;
5 class TransferController extends Controller {
6 async postTransaction() {
7 const { ctx, config } = this;
8 const body = ctx.request.body;
9 const rule = {
10 secret: { type: 'string' },
11 amount: { type: 'string' },
12 transaction: { type: 'string' },
13 };
14 console.log(body);
15 try {
16 ctx.validate(rule);
17 } catch (error) {
18 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
19 return;
20 }
21 const oldNetTransaction = JSON.parse(body.transaction);
22 if (oldNetTransaction.recipientId !== config.ed25519Address) {
23 ctx.helper.err({
24 ctx,
25 err: '接收地址不正确,请使用正确的地址',
26 });
27 return;
28 }
29 // const oldNetTransaction = JSON.stringify(body.transaction);
30 // 旧网转出
31 const oldNetData = await this.service.sdk.pushTransaction(JSON.stringify({ transaction: oldNetTransaction }), config.ed25519Url);
32 console.log('就往转账', oldNetData, typeof oldNetData);
33 if (!oldNetData || !oldNetData.success) {
34 ctx.helper.err({
35 ctx,
36 data: oldNetData,
37 });
38 return;
39 }
40 const result = await ctx.model.Transfer.create({ toAddress: body.receiverAddress, amount: body.amount, transaction: body.transaction, address: body.senderAddress });
41 ctx.helper.success({
42 ctx,
43 data: result,
44 });
45
46 // const transaction = await nodeSdk.transaction.createTransaction(body.receiverAccount, body.amount, body.remark || '网络升级转账', config.naclSecret);
47 // const trs = JSON.stringify({ transaction });
48 // // 上链操作
49 // const data = await this.service.sdk.pushTransaction(trs);
50 // if (data.success) {
51 // ctx.helper.success({
52 // ctx,
53 // data,
54 // });
55 // ctx.model.Transfer.update({ id: result.id }, { finish: true });
56 // } else {
57 // ctx.helper.err({
58 // ctx,
59 // data,
60 // });
61 // ctx.model.Transfer.update({ id: result.id }, { finish: false });
62 // }
63
64 }
65 async getBalance() {
66 const { ctx, config } = this;
67 const query = ctx.request.query;
68 const rule = {
69 address: { type: 'string' },
70 };
71 console.log(query);
72 try {
73 ctx.validate(rule, query);
74 } catch (error) {
75 console.log(error);
76 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
77 return;
78 }
79 const data = await this.service.sdk.getAccount(query.address, config.ed25519Url);
80 ctx.helper.success({
81 ctx,
82 data,
83 });
84 }
85 async getTransaction() {
86 const { ctx, config } = this;
87 const query = ctx.request.query;
88 const rule = {
89 address: { type: 'string' },
90 };
91 console.log(query);
92 try {
93 ctx.validate(rule, query);
94 } catch (error) {
95 console.log(error);
96 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
97 return;
98 }
99 const data = await this.service.sdk.getAccount(query.address, config.naclUrl);
100 ctx.helper.success({
101 ctx,
102 data,
103 });
104 }
105 async asyncAccount() {
106 const { ctx, config } = this;
107 const limit = 100;
108 let asyncCount = 0;
109 let errAsyncAccount = 0;
110 let balanceIsZero = 0;
111 const data = await this.service.sdk.getAccountCount(config.ed25519Url);
112 if (data.success) {
113 const length = Math.ceil(data.count / 100);
114 console.log(data.count)
115 for (let index = 0; index < length; index++) {
116 const acountList = await this.service.sdk.getAccountList(index + 1, limit, config.ed25519Url);
117 console.log(acountList)
118 if (acountList.success) {
119 for (let index = 0; index < acountList.accounts.length; index++) {
120 const element = acountList.accounts[index];
121 if (element.balance == 0) {
122 balanceIsZero++
123 continue;
124 }
125 const result = await this.app.model.Accounts.create({ balance: element.balance, address: element.address, publicKey: element.publicKey });
126 if (!result) {
127 errAsyncAccount++;
128 this.ctx.logger.error(`存入账户数据出错 数据:${JSON.stringify(element)},地址:${config.ed25519Url},error: ${JSON.stringify(result)}`);
129 } else {
130 asyncCount++;
131 }
132 }
133 } else {
134 this.ctx.logger.error(`同步账户数据出错:页码:${index + 1},每页条数:${limit},地址:${config.ed25519Url}`);
135 }
136 }
137 } else {
138 this.ctx.logger.error(`同步账户数据请求账户数量出错:${config.ed25519Url}`);
139 }
140 ctx.helper.success({
141 ctx,
142 data: { 出错数量: errAsyncAccount, 同步数量: data.count, 已同步数量: asyncCount, 余额为零数量: balanceIsZero },
143 });
144 }
145 }
146
147 module.exports = TransferController;
1 'use strict';
2
3 const Controller = require('egg').Controller;
4 const nodeSdk = require('@ddn/node-sdk').default;
5 const accountData=require('../../config/mem_accounts.json')
6 class TransferController extends Controller {
7 async postTransaction() {
8 const { ctx, config } = this;
9 const body = ctx.request.body;
10 const rule = {
11 secret: { type: 'string' },
12 amount: { type: 'string' },
13 transaction: { type: 'string' },
14 };
15 console.log(body);
16 try {
17 ctx.validate(rule);
18 } catch (error) {
19 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
20 return;
21 }
22 const oldNetTransaction = JSON.parse(body.transaction);
23 if (oldNetTransaction.recipientId !== config.ed25519Address) {
24 ctx.helper.err({
25 ctx,
26 err: '接收地址不正确,请使用正确的地址',
27 });
28 return;
29 }
30 // const oldNetTransaction = JSON.stringify(body.transaction);
31 // 旧网转出
32 const oldNetData = await this.service.sdk.pushTransaction(JSON.stringify({ transaction: oldNetTransaction }), config.ed25519Url);
33 console.log('就往转账', oldNetData, typeof oldNetData);
34 if (!oldNetData || !oldNetData.success) {
35 ctx.helper.err({
36 ctx,
37 data: oldNetData,
38 });
39 return;
40 }
41 const result = await ctx.model.Transfer.create({ toAddress: body.receiverAddress, amount: body.amount, transaction: body.transaction, address: body.senderAddress });
42 ctx.helper.success({
43 ctx,
44 data: result,
45 });
46
47 // const transaction = await nodeSdk.transaction.createTransaction(body.receiverAccount, body.amount, body.remark || '网络升级转账', config.naclSecret);
48 // const trs = JSON.stringify({ transaction });
49 // // 上链操作
50 // const data = await this.service.sdk.pushTransaction(trs);
51 // if (data.success) {
52 // ctx.helper.success({
53 // ctx,
54 // data,
55 // });
56 // ctx.model.Transfer.update({ id: result.id }, { finish: true });
57 // } else {
58 // ctx.helper.err({
59 // ctx,
60 // data,
61 // });
62 // ctx.model.Transfer.update({ id: result.id }, { finish: false });
63 // }
64
65 }
66 async getBalance() {
67 const { ctx, config } = this;
68 const query = ctx.request.query;
69 const rule = {
70 address: { type: 'string' },
71 };
72 console.log(query);
73 try {
74 ctx.validate(rule, query);
75 } catch (error) {
76 console.log(error);
77 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
78 return;
79 }
80 const data = await this.service.sdk.getAccount(query.address, config.ed25519Url);
81 ctx.helper.success({
82 ctx,
83 data,
84 });
85 }
86 async getTransaction() {
87 const { ctx, config } = this;
88 const query = ctx.request.query;
89 const rule = {
90 address: { type: 'string' },
91 };
92 console.log(query);
93 try {
94 ctx.validate(rule, query);
95 } catch (error) {
96 console.log(error);
97 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
98 return;
99 }
100 const data = await this.service.sdk.getAccount(query.address, config.naclUrl);
101 ctx.helper.success({
102 ctx,
103 data,
104 });
105 }
106 async asyncAccount() {
107 const { ctx, config } = this;
108 const limit = 100;
109 let asyncCount = 0;
110 let errAsyncAccount = 0;
111 let balanceIsZero = 0;
112 const data = await this.service.sdk.getAccountCount(config.ed25519Url);
113 if (data.success) {
114 const length = Math.ceil(data.count / 100);
115 console.log(data.count)
116 for (let index = 0; index < length; index++) {
117 const acountList = await this.service.sdk.getAccountList(index + 1, limit, config.ed25519Url);
118 console.log(acountList)
119 if (acountList.success) {
120 for (let index = 0; index < acountList.accounts.length; index++) {
121 const element = acountList.accounts[index];
122 if (element.balance == 0) {
123 balanceIsZero++
124 continue;
125 }
126 const result = await this.app.model.Accounts.create({ balance: element.balance, address: element.address, publicKey: element.publicKey });
127 if (!result) {
128 errAsyncAccount++;
129 this.ctx.logger.error(`存入账户数据出错 数据:${JSON.stringify(element)},地址:${config.ed25519Url},error: ${JSON.stringify(result)}`);
130 } else {
131 asyncCount++;
132 }
133 }
134 } else {
135 this.ctx.logger.error(`同步账户数据出错:页码:${index + 1},每页条数:${limit},地址:${config.ed25519Url}`);
136 }
137 }
138 } else {
139 this.ctx.logger.error(`同步账户数据请求账户数量出错:${config.ed25519Url}`);
140 }
141 ctx.helper.success({
142 ctx,
143 data: { 出错数量: errAsyncAccount, 同步数量: data.count, 已同步数量: asyncCount, 余额为零数量: balanceIsZero },
144 });
145 }
146 }
147
148 module.exports = TransferController;
1 'use strict';
2
3 const Controller = require('egg').Controller;
4 const nodeSdk = require('@ddn/node-sdk').default;
5 const accountData=require('../../config/mem_accounts.json')
6 class TransferController extends Controller {
7 async postTransaction() {
8 const { ctx, config } = this;
9 const body = ctx.request.body;
10 const rule = {
11 secret: { type: 'string' },
12 amount: { type: 'string' },
13 transaction: { type: 'string' },
14 };
15 console.log(body);
16 try {
17 ctx.validate(rule);
18 } catch (error) {
19 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
20 return;
21 }
22 const oldNetTransaction = JSON.parse(body.transaction);
23 if (oldNetTransaction.recipientId !== config.ed25519Address) {
24 ctx.helper.err({
25 ctx,
26 err: '接收地址不正确,请使用正确的地址',
27 });
28 return;
29 }
30 // const oldNetTransaction = JSON.stringify(body.transaction);
31 // 旧网转出
32 const oldNetData = await this.service.sdk.pushTransaction(JSON.stringify({ transaction: oldNetTransaction }), config.ed25519Url);
33 console.log('就往转账', oldNetData, typeof oldNetData);
34 if (!oldNetData || !oldNetData.success) {
35 ctx.helper.err({
36 ctx,
37 data: oldNetData,
38 });
39 return;
40 }
41 const result = await ctx.model.Transfer.create({ toAddress: body.receiverAddress, amount: body.amount, transaction: body.transaction, address: body.senderAddress });
42 ctx.helper.success({
43 ctx,
44 data: result,
45 });
46
47 // const transaction = await nodeSdk.transaction.createTransaction(body.receiverAccount, body.amount, body.remark || '网络升级转账', config.naclSecret);
48 // const trs = JSON.stringify({ transaction });
49 // // 上链操作
50 // const data = await this.service.sdk.pushTransaction(trs);
51 // if (data.success) {
52 // ctx.helper.success({
53 // ctx,
54 // data,
55 // });
56 // ctx.model.Transfer.update({ id: result.id }, { finish: true });
57 // } else {
58 // ctx.helper.err({
59 // ctx,
60 // data,
61 // });
62 // ctx.model.Transfer.update({ id: result.id }, { finish: false });
63 // }
64
65 }
66 async getBalance() {
67 console.log(accountData)
68 const { ctx, config } = this;
69 const query = ctx.request.query;
70 const rule = {
71 address: { type: 'string' },
72 };
73 console.log(query);
74 try {
75 ctx.validate(rule, query);
76 } catch (error) {
77 console.log(error);
78 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
79 return;
80 }
81 const data = await this.service.sdk.getAccount(query.address, config.ed25519Url);
82 ctx.helper.success({
83 ctx,
84 data,
85 });
86 }
87 async getTransaction() {
88 const { ctx, config } = this;
89 const query = ctx.request.query;
90 const rule = {
91 address: { type: 'string' },
92 };
93 console.log(query);
94 try {
95 ctx.validate(rule, query);
96 } catch (error) {
97 console.log(error);
98 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
99 return;
100 }
101 const data = await this.service.sdk.getAccount(query.address, config.naclUrl);
102 ctx.helper.success({
103 ctx,
104 data,
105 });
106 }
107 async asyncAccount() {
108 const { ctx, config } = this;
109 const limit = 100;
110 let asyncCount = 0;
111 let errAsyncAccount = 0;
112 let balanceIsZero = 0;
113 const data = await this.service.sdk.getAccountCount(config.ed25519Url);
114 if (data.success) {
115 const length = Math.ceil(data.count / 100);
116 console.log(data.count)
117 for (let index = 0; index < length; index++) {
118 const acountList = await this.service.sdk.getAccountList(index + 1, limit, config.ed25519Url);
119 console.log(acountList)
120 if (acountList.success) {
121 for (let index = 0; index < acountList.accounts.length; index++) {
122 const element = acountList.accounts[index];
123 if (element.balance == 0) {
124 balanceIsZero++
125 continue;
126 }
127 const result = await this.app.model.Accounts.create({ balance: element.balance, address: element.address, publicKey: element.publicKey });
128 if (!result) {
129 errAsyncAccount++;
130 this.ctx.logger.error(`存入账户数据出错 数据:${JSON.stringify(element)},地址:${config.ed25519Url},error: ${JSON.stringify(result)}`);
131 } else {
132 asyncCount++;
133 }
134 }
135 } else {
136 this.ctx.logger.error(`同步账户数据出错:页码:${index + 1},每页条数:${limit},地址:${config.ed25519Url}`);
137 }
138 }
139 } else {
140 this.ctx.logger.error(`同步账户数据请求账户数量出错:${config.ed25519Url}`);
141 }
142 ctx.helper.success({
143 ctx,
144 data: { 出错数量: errAsyncAccount, 同步数量: data.count, 已同步数量: asyncCount, 余额为零数量: balanceIsZero },
145 });
146 }
147 }
148
149 module.exports = TransferController;
1 'use strict';
2
3 const Controller = require('egg').Controller;
4 const nodeSdk = require('@ddn/node-sdk').default;
5 const accountData=require('../../config/mem_accounts.json')
6 class TransferController extends Controller {
7 async postTransaction() {
8 const { ctx, config } = this;
9 const body = ctx.request.body;
10 const rule = {
11 secret: { type: 'string' },
12 amount: { type: 'string' },
13 transaction: { type: 'string' },
14 };
15 console.log(body);
16 try {
17 ctx.validate(rule);
18 } catch (error) {
19 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
20 return;
21 }
22 const oldNetTransaction = JSON.parse(body.transaction);
23 if (oldNetTransaction.recipientId !== config.ed25519Address) {
24 ctx.helper.err({
25 ctx,
26 err: '接收地址不正确,请使用正确的地址',
27 });
28 return;
29 }
30 // const oldNetTransaction = JSON.stringify(body.transaction);
31 // 旧网转出
32 const oldNetData = await this.service.sdk.pushTransaction(JSON.stringify({ transaction: oldNetTransaction }), config.ed25519Url);
33 console.log('就往转账', oldNetData, typeof oldNetData);
34 if (!oldNetData || !oldNetData.success) {
35 ctx.helper.err({
36 ctx,
37 data: oldNetData,
38 });
39 return;
40 }
41 const result = await ctx.model.Transfer.create({ toAddress: body.receiverAddress, amount: body.amount, transaction: body.transaction, address: body.senderAddress });
42 ctx.helper.success({
43 ctx,
44 data: result,
45 });
46
47 // const transaction = await nodeSdk.transaction.createTransaction(body.receiverAccount, body.amount, body.remark || '网络升级转账', config.naclSecret);
48 // const trs = JSON.stringify({ transaction });
49 // // 上链操作
50 // const data = await this.service.sdk.pushTransaction(trs);
51 // if (data.success) {
52 // ctx.helper.success({
53 // ctx,
54 // data,
55 // });
56 // ctx.model.Transfer.update({ id: result.id }, { finish: true });
57 // } else {
58 // ctx.helper.err({
59 // ctx,
60 // data,
61 // });
62 // ctx.model.Transfer.update({ id: result.id }, { finish: false });
63 // }
64
65 }
66 async getBalance() {
67 const { ctx, config } = this;
68 const query = ctx.request.query;
69 const rule = {
70 address: { type: 'string' },
71 };
72 console.log(query);
73 try {
74 ctx.validate(rule, query);
75 } catch (error) {
76 console.log(error);
77 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
78 return;
79 }
80 const data = await this.service.sdk.getAccount(query.address, config.ed25519Url);
81 ctx.helper.success({
82 ctx,
83 data,
84 });
85 }
86 async getTransaction() {
87 const { ctx, config } = this;
88 const query = ctx.request.query;
89 const rule = {
90 address: { type: 'string' },
91 };
92 console.log(query);
93 try {
94 ctx.validate(rule, query);
95 } catch (error) {
96 console.log(error);
97 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
98 return;
99 }
100 const data = await this.service.sdk.getAccount(query.address, config.naclUrl);
101 ctx.helper.success({
102 ctx,
103 data,
104 });
105 }
106 async asyncAccount() {
107 const { ctx, config } = this;
108 const limit = 100;
109 let asyncCount = 0;
110 let errAsyncAccount = 0;
111 let balanceIsZero = 0;
112 const data = await this.service.sdk.getAccountCount(config.ed25519Url);
113 if (data.success) {
114 const length = Math.ceil(data.count / 100);
115 console.log(data.count)
116 for (let index = 0; index < length; index++) {
117 const acountList = await this.service.sdk.getAccountList(index + 1, limit, config.ed25519Url);
118 console.log(acountList)
119 if (acountList.success) {
120 for (let index = 0; index < acountList.accounts.length; index++) {
121 const element = acountList.accounts[index];
122 if (element.balance == 0) {
123 balanceIsZero++
124 continue;
125 }
126 const result = await this.app.model.Accounts.create({ balance: element.balance, address: element.address, publicKey: element.publicKey });
127 if (!result) {
128 errAsyncAccount++;
129 this.ctx.logger.error(`存入账户数据出错 数据:${JSON.stringify(element)},地址:${config.ed25519Url},error: ${JSON.stringify(result)}`);
130 } else {
131 asyncCount++;
132 }
133 }
134 } else {
135 this.ctx.logger.error(`同步账户数据出错:页码:${index + 1},每页条数:${limit},地址:${config.ed25519Url}`);
136 }
137 }
138 } else {
139 this.ctx.logger.error(`同步账户数据请求账户数量出错:${config.ed25519Url}`);
140 }
141 ctx.helper.success({
142 ctx,
143 data: { 出错数量: errAsyncAccount, 同步数量: data.count, 已同步数量: asyncCount, 余额为零数量: balanceIsZero },
144 });
145 }
146 }
147
148 module.exports = TransferController;
1 'use strict';
2
3 const Controller = require('egg').Controller;
4 const nodeSdk = require('@ddn/node-sdk').default;
5 const accountData = require('../../config/mem_accounts.json')
6 class TransferController extends Controller {
7 async postTransaction() {
8 const { ctx, config } = this;
9 const body = ctx.request.body;
10 const rule = {
11 secret: { type: 'string' },
12 amount: { type: 'string' },
13 transaction: { type: 'string' },
14 };
15 console.log(body);
16 try {
17 ctx.validate(rule);
18 } catch (error) {
19 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
20 return;
21 }
22 const oldNetTransaction = JSON.parse(body.transaction);
23 if (oldNetTransaction.recipientId !== config.ed25519Address) {
24 ctx.helper.err({
25 ctx,
26 err: '接收地址不正确,请使用正确的地址',
27 });
28 return;
29 }
30 // const oldNetTransaction = JSON.stringify(body.transaction);
31 // 旧网转出
32 const oldNetData = await this.service.sdk.pushTransaction(JSON.stringify({ transaction: oldNetTransaction }), config.ed25519Url);
33 console.log('就往转账', oldNetData, typeof oldNetData);
34 if (!oldNetData || !oldNetData.success) {
35 ctx.helper.err({
36 ctx,
37 data: oldNetData,
38 });
39 return;
40 }
41 const result = await ctx.model.Transfer.create({ toAddress: body.receiverAddress, amount: body.amount, transaction: body.transaction, address: body.senderAddress });
42 ctx.helper.success({
43 ctx,
44 data: result,
45 });
46
47 // const transaction = await nodeSdk.transaction.createTransaction(body.receiverAccount, body.amount, body.remark || '网络升级转账', config.naclSecret);
48 // const trs = JSON.stringify({ transaction });
49 // // 上链操作
50 // const data = await this.service.sdk.pushTransaction(trs);
51 // if (data.success) {
52 // ctx.helper.success({
53 // ctx,
54 // data,
55 // });
56 // ctx.model.Transfer.update({ id: result.id }, { finish: true });
57 // } else {
58 // ctx.helper.err({
59 // ctx,
60 // data,
61 // });
62 // ctx.model.Transfer.update({ id: result.id }, { finish: false });
63 // }
64
65 }
66 async getBalance() {
67 const { ctx, config } = this;
68 const query = ctx.request.query;
69 const rule = {
70 address: { type: 'string' },
71 };
72 console.log(query);
73 try {
74 ctx.validate(rule, query);
75 } catch (error) {
76 console.log(error);
77 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
78 return;
79 }
80 const data = await this.service.sdk.getAccount(query.address, config.ed25519Url);
81 ctx.helper.success({
82 ctx,
83 data,
84 });
85 }
86 async haveBalance() {
87 const { ctx, config } = this;
88 const query = ctx.request.query;
89 const rule = {
90 address: { type: 'string' },
91 };
92 try {
93 ctx.validate(rule, query);
94 } catch (error) {
95 console.log(error);
96 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
97 return;
98 }
99 let balance
100 const result = accountData.RECORDS.some(item => {
101 if (item.address === query.address && item.balance > 0) {
102 balance = item.balance
103 return true
104 }
105 })
106
107 ctx.helper.success({
108 ctx,
109 data: {
110 balance
111 },
112 });
113 }
114 async getTransaction() {
115 const { ctx, config } = this;
116 const query = ctx.request.query;
117 const rule = {
118 address: { type: 'string' },
119 };
120 console.log(query);
121 try {
122 ctx.validate(rule, query);
123 } catch (error) {
124 console.log(error);
125 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
126 return;
127 }
128 const data = await this.service.sdk.getAccount(query.address, config.naclUrl);
129 ctx.helper.success({
130 ctx,
131 data,
132 });
133 }
134 async asyncAccount() {
135 const { ctx, config } = this;
136 const limit = 100;
137 let asyncCount = 0;
138 let errAsyncAccount = 0;
139 let balanceIsZero = 0;
140 const data = await this.service.sdk.getAccountCount(config.ed25519Url);
141 if (data.success) {
142 const length = Math.ceil(data.count / 100);
143 console.log(data.count)
144 for (let index = 0; index < length; index++) {
145 const acountList = await this.service.sdk.getAccountList(index + 1, limit, config.ed25519Url);
146 console.log(acountList)
147 if (acountList.success) {
148 for (let index = 0; index < acountList.accounts.length; index++) {
149 const element = acountList.accounts[index];
150 if (element.balance == 0) {
151 balanceIsZero++
152 continue;
153 }
154 const result = await this.app.model.Accounts.create({ balance: element.balance, address: element.address, publicKey: element.publicKey });
155 if (!result) {
156 errAsyncAccount++;
157 this.ctx.logger.error(`存入账户数据出错 数据:${JSON.stringify(element)},地址:${config.ed25519Url},error: ${JSON.stringify(result)}`);
158 } else {
159 asyncCount++;
160 }
161 }
162 } else {
163 this.ctx.logger.error(`同步账户数据出错:页码:${index + 1},每页条数:${limit},地址:${config.ed25519Url}`);
164 }
165 }
166 } else {
167 this.ctx.logger.error(`同步账户数据请求账户数量出错:${config.ed25519Url}`);
168 }
169 ctx.helper.success({
170 ctx,
171 data: { 出错数量: errAsyncAccount, 同步数量: data.count, 已同步数量: asyncCount, 余额为零数量: balanceIsZero },
172 });
173 }
174 }
175
176 module.exports = TransferController;
1 'use strict';
2
3 const Controller = require('egg').Controller;
4 const nodeSdk = require('@ddn/node-sdk').default;
5 const accountData = require('../../config/mem_accounts.json')
6 class TransferController extends Controller {
7 async postTransaction() {
8 const { ctx, config } = this;
9 const body = ctx.request.body;
10 const rule = {
11 secret: { type: 'string' },
12 amount: { type: 'string' },
13 transaction: { type: 'string' },
14 };
15 console.log(body);
16 try {
17 ctx.validate(rule);
18 } catch (error) {
19 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
20 return;
21 }
22 const oldNetTransaction = JSON.parse(body.transaction);
23 if (oldNetTransaction.recipientId !== config.ed25519Address) {
24 ctx.helper.err({
25 ctx,
26 err: '接收地址不正确,请使用正确的地址',
27 });
28 return;
29 }
30 // const oldNetTransaction = JSON.stringify(body.transaction);
31 // 旧网转出
32 const oldNetData = await this.service.sdk.pushTransaction(JSON.stringify({ transaction: oldNetTransaction }), config.ed25519Url);
33 console.log('就往转账', oldNetData, typeof oldNetData);
34 if (!oldNetData || !oldNetData.success) {
35 ctx.helper.err({
36 ctx,
37 data: oldNetData,
38 });
39 return;
40 }
41 const result = await ctx.model.Transfer.create({ toAddress: body.receiverAddress, amount: body.amount, transaction: body.transaction, address: body.senderAddress });
42 ctx.helper.success({
43 ctx,
44 data: result,
45 });
46
47 // const transaction = await nodeSdk.transaction.createTransaction(body.receiverAccount, body.amount, body.remark || '网络升级转账', config.naclSecret);
48 // const trs = JSON.stringify({ transaction });
49 // // 上链操作
50 // const data = await this.service.sdk.pushTransaction(trs);
51 // if (data.success) {
52 // ctx.helper.success({
53 // ctx,
54 // data,
55 // });
56 // ctx.model.Transfer.update({ id: result.id }, { finish: true });
57 // } else {
58 // ctx.helper.err({
59 // ctx,
60 // data,
61 // });
62 // ctx.model.Transfer.update({ id: result.id }, { finish: false });
63 // }
64
65 }
66 async getBalance() {
67 const { ctx, config } = this;
68 const query = ctx.request.query;
69 const rule = {
70 address: { type: 'string' },
71 };
72 console.log(query);
73 try {
74 ctx.validate(rule, query);
75 } catch (error) {
76 console.log(error);
77 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
78 return;
79 }
80 const data = await this.service.sdk.getAccount(query.address, config.ed25519Url);
81 ctx.helper.success({
82 ctx,
83 data,
84 });
85 }
86 async haveBalance() {
87 const { ctx, config } = this;
88 const query = ctx.request.query;
89 const rule = {
90 address: { type: 'string' },
91 };
92 try {
93 ctx.validate(rule, query);
94 } catch (error) {
95 console.log(error);
96 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
97 return;
98 }
99 let balance
100 const result = accountData.RECORDS.some(item => {
101 if (item.address === query.address && item.balance > 0) {
102 balance = item.balance
103 return true
104 }
105 })
106 if (result) {
107 ctx.helper.success({
108 ctx,
109 data: {
110 balance
111 },
112 });
113 } else {
114 ctx.helper.err({
115 ctx,
116 data: {
117 balance
118 },
119 });
120 }
121
122 }
123 async getTransaction() {
124 const { ctx, config } = this;
125 const query = ctx.request.query;
126 const rule = {
127 address: { type: 'string' },
128 };
129 console.log(query);
130 try {
131 ctx.validate(rule, query);
132 } catch (error) {
133 console.log(error);
134 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
135 return;
136 }
137 const data = await this.service.sdk.getAccount(query.address, config.naclUrl);
138 ctx.helper.success({
139 ctx,
140 data,
141 });
142 }
143 async asyncAccount() {
144 const { ctx, config } = this;
145 const limit = 100;
146 let asyncCount = 0;
147 let errAsyncAccount = 0;
148 let balanceIsZero = 0;
149 const data = await this.service.sdk.getAccountCount(config.ed25519Url);
150 if (data.success) {
151 const length = Math.ceil(data.count / 100);
152 console.log(data.count)
153 for (let index = 0; index < length; index++) {
154 const acountList = await this.service.sdk.getAccountList(index + 1, limit, config.ed25519Url);
155 console.log(acountList)
156 if (acountList.success) {
157 for (let index = 0; index < acountList.accounts.length; index++) {
158 const element = acountList.accounts[index];
159 if (element.balance == 0) {
160 balanceIsZero++
161 continue;
162 }
163 const result = await this.app.model.Accounts.create({ balance: element.balance, address: element.address, publicKey: element.publicKey });
164 if (!result) {
165 errAsyncAccount++;
166 this.ctx.logger.error(`存入账户数据出错 数据:${JSON.stringify(element)},地址:${config.ed25519Url},error: ${JSON.stringify(result)}`);
167 } else {
168 asyncCount++;
169 }
170 }
171 } else {
172 this.ctx.logger.error(`同步账户数据出错:页码:${index + 1},每页条数:${limit},地址:${config.ed25519Url}`);
173 }
174 }
175 } else {
176 this.ctx.logger.error(`同步账户数据请求账户数量出错:${config.ed25519Url}`);
177 }
178 ctx.helper.success({
179 ctx,
180 data: { 出错数量: errAsyncAccount, 同步数量: data.count, 已同步数量: asyncCount, 余额为零数量: balanceIsZero },
181 });
182 }
183 }
184
185 module.exports = TransferController;
1 'use strict';
2
3 const Controller = require('egg').Controller;
4 const nodeSdk = require('@ddn/node-sdk').default;
5 const accountData = require('../../config/mem_accounts.json')
6 class TransferController extends Controller {
7 async postTransaction() {
8 const { ctx, config } = this;
9 const body = ctx.request.body;
10 const rule = {
11 secret: { type: 'string' },
12 amount: { type: 'string' },
13 transaction: { type: 'string' },
14 };
15 console.log(body);
16 try {
17 ctx.validate(rule);
18 } catch (error) {
19 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
20 return;
21 }
22 const oldNetTransaction = JSON.parse(body.transaction);
23 if (oldNetTransaction.recipientId !== config.ed25519Address) {
24 ctx.helper.err({
25 ctx,
26 err: '接收地址不正确,请使用正确的地址',
27 });
28 return;
29 }
30 // const oldNetTransaction = JSON.stringify(body.transaction);
31 // 旧网转出
32 const oldNetData = await this.service.sdk.pushTransaction(JSON.stringify({ transaction: oldNetTransaction }), config.ed25519Url);
33 console.log('就往转账', oldNetData, typeof oldNetData);
34 if (!oldNetData || !oldNetData.success) {
35 ctx.helper.err({
36 ctx,
37 data: oldNetData,
38 });
39 return;
40 }
41 const result = await ctx.model.Transfer.create({ toAddress: body.receiverAddress, amount: body.amount, transaction: body.transaction, address: body.senderAddress });
42 ctx.helper.success({
43 ctx,
44 data: result,
45 });
46
47 // const transaction = await nodeSdk.transaction.createTransaction(body.receiverAccount, body.amount, body.remark || '网络升级转账', config.naclSecret);
48 // const trs = JSON.stringify({ transaction });
49 // // 上链操作
50 // const data = await this.service.sdk.pushTransaction(trs);
51 // if (data.success) {
52 // ctx.helper.success({
53 // ctx,
54 // data,
55 // });
56 // ctx.model.Transfer.update({ id: result.id }, { finish: true });
57 // } else {
58 // ctx.helper.err({
59 // ctx,
60 // data,
61 // });
62 // ctx.model.Transfer.update({ id: result.id }, { finish: false });
63 // }
64
65 }
66 async getBalance() {
67 const { ctx, config } = this;
68 const query = ctx.request.query;
69 const rule = {
70 address: { type: 'string' },
71 };
72 console.log(query);
73 try {
74 ctx.validate(rule, query);
75 } catch (error) {
76 console.log(error);
77 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
78 return;
79 }
80 const data = await this.service.sdk.getAccount(query.address, config.ed25519Url);
81 ctx.helper.success({
82 ctx,
83 data,
84 });
85 }
86 async haveBalance() {
87 const { ctx, config } = this;
88 const query = ctx.request.query;
89 const rule = {
90 address: { type: 'string' },
91 };
92 try {
93 ctx.validate(rule, query);
94 } catch (error) {
95 console.log(error);
96 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
97 return;
98 }
99 let balance
100 const result = accountData.RECORDS.some(item => {
101 if (item.address === query.address && item.balance > 0) {
102 balance = item.balance
103 return true
104 }
105 })
106 ctx.helper.success({
107 ctx,
108 data: {
109 balance
110 },
111 });
112
113
114 }
115 async getTransaction() {
116 const { ctx, config } = this;
117 const query = ctx.request.query;
118 const rule = {
119 address: { type: 'string' },
120 };
121 console.log(query);
122 try {
123 ctx.validate(rule, query);
124 } catch (error) {
125 console.log(error);
126 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
127 return;
128 }
129 const data = await this.service.sdk.getAccount(query.address, config.naclUrl);
130 ctx.helper.success({
131 ctx,
132 data,
133 });
134 }
135 async asyncAccount() {
136 const { ctx, config } = this;
137 const limit = 100;
138 let asyncCount = 0;
139 let errAsyncAccount = 0;
140 let balanceIsZero = 0;
141 const data = await this.service.sdk.getAccountCount(config.ed25519Url);
142 if (data.success) {
143 const length = Math.ceil(data.count / 100);
144 console.log(data.count)
145 for (let index = 0; index < length; index++) {
146 const acountList = await this.service.sdk.getAccountList(index + 1, limit, config.ed25519Url);
147 console.log(acountList)
148 if (acountList.success) {
149 for (let index = 0; index < acountList.accounts.length; index++) {
150 const element = acountList.accounts[index];
151 if (element.balance == 0) {
152 balanceIsZero++
153 continue;
154 }
155 const result = await this.app.model.Accounts.create({ balance: element.balance, address: element.address, publicKey: element.publicKey });
156 if (!result) {
157 errAsyncAccount++;
158 this.ctx.logger.error(`存入账户数据出错 数据:${JSON.stringify(element)},地址:${config.ed25519Url},error: ${JSON.stringify(result)}`);
159 } else {
160 asyncCount++;
161 }
162 }
163 } else {
164 this.ctx.logger.error(`同步账户数据出错:页码:${index + 1},每页条数:${limit},地址:${config.ed25519Url}`);
165 }
166 }
167 } else {
168 this.ctx.logger.error(`同步账户数据请求账户数量出错:${config.ed25519Url}`);
169 }
170 ctx.helper.success({
171 ctx,
172 data: { 出错数量: errAsyncAccount, 同步数量: data.count, 已同步数量: asyncCount, 余额为零数量: balanceIsZero },
173 });
174 }
175 }
176
177 module.exports = TransferController;
1 'use strict';
2
3 const Controller = require('egg').Controller;
4 const nodeSdk = require('@ddn/node-sdk').default;
5 const accountData = require('../../config/mem_accounts.json')
6 class TransferController extends Controller {
7 async postTransaction() {
8 const { ctx, config } = this;
9 const body = ctx.request.body;
10 const rule = {
11 secret: { type: 'string' },
12 amount: { type: 'string' },
13 transaction: { type: 'string' },
14 };
15 console.log(body);
16 try {
17 ctx.validate(rule);
18 } catch (error) {
19 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
20 return;
21 }
22 const oldNetTransaction = JSON.parse(body.transaction);
23 if (oldNetTransaction.recipientId !== config.ed25519Address) {
24 ctx.helper.err({
25 ctx,
26 err: '接收地址不正确,请使用正确的地址',
27 });
28 return;
29 }
30 // const oldNetTransaction = JSON.stringify(body.transaction);
31 // 旧网转出
32 const oldNetData = await this.service.sdk.pushTransaction(JSON.stringify({ transaction: oldNetTransaction }), config.ed25519Url);
33 console.log('就往转账', oldNetData, typeof oldNetData);
34 if (!oldNetData || !oldNetData.success) {
35 ctx.helper.err({
36 ctx,
37 data: oldNetData,
38 });
39 return;
40 }
41 const result = await ctx.model.Transfer.create({ toAddress: body.receiverAddress, amount: body.amount, transaction: body.transaction, address: body.senderAddress });
42 ctx.helper.success({
43 ctx,
44 data: result,
45 });
46
47 // const transaction = await nodeSdk.transaction.createTransaction(body.receiverAccount, body.amount, body.remark || '网络升级转账', config.naclSecret);
48 // const trs = JSON.stringify({ transaction });
49 // // 上链操作
50 // const data = await this.service.sdk.pushTransaction(trs);
51 // if (data.success) {
52 // ctx.helper.success({
53 // ctx,
54 // data,
55 // });
56 // ctx.model.Transfer.update({ id: result.id }, { finish: true });
57 // } else {
58 // ctx.helper.err({
59 // ctx,
60 // data,
61 // });
62 // ctx.model.Transfer.update({ id: result.id }, { finish: false });
63 // }
64
65 }
66 async haveBalance() {
67 const { ctx, config } = this;
68 const query = ctx.request.query;
69 const rule = {
70 address: { type: 'string' },
71 };
72 console.log(query);
73 try {
74 ctx.validate(rule, query);
75 } catch (error) {
76 console.log(error);
77 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
78 return;
79 }
80 const data = await this.service.sdk.getAccount(query.address, config.ed25519Url);
81 ctx.helper.success({
82 ctx,
83 data,
84 });
85 }
86 async getBalance() {
87 const { ctx, config } = this;
88 const query = ctx.request.query;
89 const rule = {
90 address: { type: 'string' },
91 };
92 try {
93 ctx.validate(rule, query);
94 } catch (error) {
95 console.log(error);
96 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
97 return;
98 }
99 let balance
100 const result = accountData.RECORDS.some(item => {
101 if (item.address === query.address && item.balance > 0) {
102 balance = item.balance
103 return true
104 }
105 })
106 ctx.helper.success({
107 ctx,
108 data: {
109 balance
110 },
111 });
112
113
114 }
115 async getTransaction() {
116 const { ctx, config } = this;
117 const query = ctx.request.query;
118 const rule = {
119 address: { type: 'string' },
120 };
121 console.log(query);
122 try {
123 ctx.validate(rule, query);
124 } catch (error) {
125 console.log(error);
126 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
127 return;
128 }
129 const data = await this.service.sdk.getAccount(query.address, config.naclUrl);
130 ctx.helper.success({
131 ctx,
132 data,
133 });
134 }
135 async asyncAccount() {
136 const { ctx, config } = this;
137 const limit = 100;
138 let asyncCount = 0;
139 let errAsyncAccount = 0;
140 let balanceIsZero = 0;
141 const data = await this.service.sdk.getAccountCount(config.ed25519Url);
142 if (data.success) {
143 const length = Math.ceil(data.count / 100);
144 console.log(data.count)
145 for (let index = 0; index < length; index++) {
146 const acountList = await this.service.sdk.getAccountList(index + 1, limit, config.ed25519Url);
147 console.log(acountList)
148 if (acountList.success) {
149 for (let index = 0; index < acountList.accounts.length; index++) {
150 const element = acountList.accounts[index];
151 if (element.balance == 0) {
152 balanceIsZero++
153 continue;
154 }
155 const result = await this.app.model.Accounts.create({ balance: element.balance, address: element.address, publicKey: element.publicKey });
156 if (!result) {
157 errAsyncAccount++;
158 this.ctx.logger.error(`存入账户数据出错 数据:${JSON.stringify(element)},地址:${config.ed25519Url},error: ${JSON.stringify(result)}`);
159 } else {
160 asyncCount++;
161 }
162 }
163 } else {
164 this.ctx.logger.error(`同步账户数据出错:页码:${index + 1},每页条数:${limit},地址:${config.ed25519Url}`);
165 }
166 }
167 } else {
168 this.ctx.logger.error(`同步账户数据请求账户数量出错:${config.ed25519Url}`);
169 }
170 ctx.helper.success({
171 ctx,
172 data: { 出错数量: errAsyncAccount, 同步数量: data.count, 已同步数量: asyncCount, 余额为零数量: balanceIsZero },
173 });
174 }
175 }
176
177 module.exports = TransferController;
1 'use strict';
2
3 const Controller = require('egg').Controller;
4 const nodeSdk = require('@ddn/node-sdk').default;
5 const accountData = require('../../config/mem_accounts.json')
6 class TransferController extends Controller {
7 async postTransaction() {
8 const { ctx, config } = this;
9 const body = ctx.request.body;
10 const rule = {
11 secret: { type: 'string' },
12 amount: { type: 'string' },
13 transaction: { type: 'string' },
14 };
15 console.log(body);
16 try {
17 ctx.validate(rule);
18 } catch (error) {
19 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
20 return;
21 }
22 const oldNetTransaction = JSON.parse(body.transaction);
23 if (oldNetTransaction.recipientId !== config.ed25519Address) {
24 ctx.helper.err({
25 ctx,
26 err: '接收地址不正确,请使用正确的地址',
27 });
28 return;
29 }
30 // const oldNetTransaction = JSON.stringify(body.transaction);
31 // 旧网转出
32 const oldNetData = await this.service.sdk.pushTransaction(JSON.stringify({ transaction: oldNetTransaction }), config.ed25519Url);
33 console.log('就往转账', oldNetData, typeof oldNetData);
34 if (!oldNetData || !oldNetData.success) {
35 ctx.helper.err({
36 ctx,
37 data: oldNetData,
38 });
39 return;
40 }
41 const result = await ctx.model.Transfer.create({ toAddress: body.receiverAddress, amount: body.amount, transaction: body.transaction, address: body.senderAddress });
42 ctx.helper.success({
43 ctx,
44 data: result,
45 });
46
47 // const transaction = await nodeSdk.transaction.createTransaction(body.receiverAccount, body.amount, body.remark || '网络升级转账', config.naclSecret);
48 // const trs = JSON.stringify({ transaction });
49 // // 上链操作
50 // const data = await this.service.sdk.pushTransaction(trs);
51 // if (data.success) {
52 // ctx.helper.success({
53 // ctx,
54 // data,
55 // });
56 // ctx.model.Transfer.update({ id: result.id }, { finish: true });
57 // } else {
58 // ctx.helper.err({
59 // ctx,
60 // data,
61 // });
62 // ctx.model.Transfer.update({ id: result.id }, { finish: false });
63 // }
64
65 }
66 async haveBalance() {
67 const { ctx, config } = this;
68 const query = ctx.request.query;
69 const rule = {
70 address: { type: 'string' },
71 };
72 console.log(query);
73 try {
74 ctx.validate(rule, query);
75 } catch (error) {
76 console.log(error);
77 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
78 return;
79 }
80 const data = await this.service.sdk.getAccount(query.address, config.ed25519Url);
81 ctx.helper.success({
82 ctx,
83 data,
84 });
85 }
86 async getBalance() {
87 const { ctx, config } = this;
88 const query = ctx.request.query;
89 const rule = {
90 address: { type: 'string' },
91 };
92 try {
93 ctx.validate(rule, query);
94 } catch (error) {
95 console.log(error);
96 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
97 return;
98 }
99 let balance=0
100 const result = accountData.RECORDS.some(item => {
101 if (item.address === query.address && item.balance > 0) {
102 balance = item.balance
103 return true
104 }
105 })
106 ctx.helper.success({
107 ctx,
108 data: {
109 balance
110 },
111 });
112
113
114 }
115 async getTransaction() {
116 const { ctx, config } = this;
117 const query = ctx.request.query;
118 const rule = {
119 address: { type: 'string' },
120 };
121 console.log(query);
122 try {
123 ctx.validate(rule, query);
124 } catch (error) {
125 console.log(error);
126 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
127 return;
128 }
129 const data = await this.service.sdk.getAccount(query.address, config.naclUrl);
130 ctx.helper.success({
131 ctx,
132 data,
133 });
134 }
135 async asyncAccount() {
136 const { ctx, config } = this;
137 const limit = 100;
138 let asyncCount = 0;
139 let errAsyncAccount = 0;
140 let balanceIsZero = 0;
141 const data = await this.service.sdk.getAccountCount(config.ed25519Url);
142 if (data.success) {
143 const length = Math.ceil(data.count / 100);
144 console.log(data.count)
145 for (let index = 0; index < length; index++) {
146 const acountList = await this.service.sdk.getAccountList(index + 1, limit, config.ed25519Url);
147 console.log(acountList)
148 if (acountList.success) {
149 for (let index = 0; index < acountList.accounts.length; index++) {
150 const element = acountList.accounts[index];
151 if (element.balance == 0) {
152 balanceIsZero++
153 continue;
154 }
155 const result = await this.app.model.Accounts.create({ balance: element.balance, address: element.address, publicKey: element.publicKey });
156 if (!result) {
157 errAsyncAccount++;
158 this.ctx.logger.error(`存入账户数据出错 数据:${JSON.stringify(element)},地址:${config.ed25519Url},error: ${JSON.stringify(result)}`);
159 } else {
160 asyncCount++;
161 }
162 }
163 } else {
164 this.ctx.logger.error(`同步账户数据出错:页码:${index + 1},每页条数:${limit},地址:${config.ed25519Url}`);
165 }
166 }
167 } else {
168 this.ctx.logger.error(`同步账户数据请求账户数量出错:${config.ed25519Url}`);
169 }
170 ctx.helper.success({
171 ctx,
172 data: { 出错数量: errAsyncAccount, 同步数量: data.count, 已同步数量: asyncCount, 余额为零数量: balanceIsZero },
173 });
174 }
175 }
176
177 module.exports = TransferController;
1 'use strict';
2
3 const Controller = require('egg').Controller;
4 const nodeSdk = require('@ddn/node-sdk').default;
5 const accountData = require('../../config/mem_accounts.json')
6 class TransferController extends Controller {
7 async postTransaction() {
8 const { ctx, config } = this;
9 const body = ctx.request.body;
10 const rule = {
11 secret: { type: 'string' },
12 amount: { type: 'string' },
13 transaction: { type: 'string' },
14 };
15 console.log(body);
16 try {
17 ctx.validate(rule);
18 } catch (error) {
19 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
20 return;
21 }
22 const oldNetTransaction = JSON.parse(body.transaction);
23 if (oldNetTransaction.recipientId !== config.ed25519Address) {
24 ctx.helper.err({
25 ctx,
26 err: '接收地址不正确,请使用正确的地址',
27 });
28 return;
29 }
30 // const oldNetTransaction = JSON.stringify(body.transaction);
31 // 旧网转出
32 const oldNetData = await this.service.sdk.pushTransaction(JSON.stringify({ transaction: oldNetTransaction }), config.ed25519Url);
33 console.log('就往转账', oldNetData, typeof oldNetData);
34 if (!oldNetData || !oldNetData.success) {
35 ctx.helper.err({
36 ctx,
37 data: oldNetData,
38 });
39 return;
40 }
41 const result = await ctx.model.Transfer.create({ toAddress: body.receiverAddress, amount: body.amount, transaction: body.transaction, address: body.senderAddress });
42 ctx.helper.success({
43 ctx,
44 data: result,
45 });
46
47 // const transaction = await nodeSdk.transaction.createTransaction(body.receiverAccount, body.amount, body.remark || '网络升级转账', config.naclSecret);
48 // const trs = JSON.stringify({ transaction });
49 // // 上链操作
50 // const data = await this.service.sdk.pushTransaction(trs);
51 // if (data.success) {
52 // ctx.helper.success({
53 // ctx,
54 // data,
55 // });
56 // ctx.model.Transfer.update({ id: result.id }, { finish: true });
57 // } else {
58 // ctx.helper.err({
59 // ctx,
60 // data,
61 // });
62 // ctx.model.Transfer.update({ id: result.id }, { finish: false });
63 // }
64
65 }
66 async haveBalance() {
67 const { ctx, config } = this;
68 const query = ctx.request.query;
69 const rule = {
70 address: { type: 'string' },
71 };
72 console.log(query);
73 try {
74 ctx.validate(rule, query);
75 } catch (error) {
76 console.log(error);
77 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
78 return;
79 }
80 const data = await this.service.sdk.getAccount(query.address, config.ed25519Url);
81 ctx.helper.success({
82 ctx,
83 data,
84 });
85 }
86 async getBalance() {
87 const { ctx, config } = this;
88 const query = ctx.request.query;
89 const rule = {
90 address: { type: 'string' },
91 };
92 try {
93 ctx.validate(rule, query);
94 } catch (error) {
95 console.log(error);
96 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
97 return;
98 }
99 let balance = 0
100 const result = accountData.RECORDS.some(item => {
101 if (item.address === query.address && item.balance > 0) {
102 balance = item.balance
103 return true
104 }
105 })
106 ctx.helper.success({
107 ctx,
108 data: {
109 account: { balance }
110 },
111 });
112
113
114 }
115 async getTransaction() {
116 const { ctx, config } = this;
117 const query = ctx.request.query;
118 const rule = {
119 address: { type: 'string' },
120 };
121 console.log(query);
122 try {
123 ctx.validate(rule, query);
124 } catch (error) {
125 console.log(error);
126 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
127 return;
128 }
129 const data = await this.service.sdk.getAccount(query.address, config.naclUrl);
130 ctx.helper.success({
131 ctx,
132 data,
133 });
134 }
135 async asyncAccount() {
136 const { ctx, config } = this;
137 const limit = 100;
138 let asyncCount = 0;
139 let errAsyncAccount = 0;
140 let balanceIsZero = 0;
141 const data = await this.service.sdk.getAccountCount(config.ed25519Url);
142 if (data.success) {
143 const length = Math.ceil(data.count / 100);
144 console.log(data.count)
145 for (let index = 0; index < length; index++) {
146 const acountList = await this.service.sdk.getAccountList(index + 1, limit, config.ed25519Url);
147 console.log(acountList)
148 if (acountList.success) {
149 for (let index = 0; index < acountList.accounts.length; index++) {
150 const element = acountList.accounts[index];
151 if (element.balance == 0) {
152 balanceIsZero++
153 continue;
154 }
155 const result = await this.app.model.Accounts.create({ balance: element.balance, address: element.address, publicKey: element.publicKey });
156 if (!result) {
157 errAsyncAccount++;
158 this.ctx.logger.error(`存入账户数据出错 数据:${JSON.stringify(element)},地址:${config.ed25519Url},error: ${JSON.stringify(result)}`);
159 } else {
160 asyncCount++;
161 }
162 }
163 } else {
164 this.ctx.logger.error(`同步账户数据出错:页码:${index + 1},每页条数:${limit},地址:${config.ed25519Url}`);
165 }
166 }
167 } else {
168 this.ctx.logger.error(`同步账户数据请求账户数量出错:${config.ed25519Url}`);
169 }
170 ctx.helper.success({
171 ctx,
172 data: { 出错数量: errAsyncAccount, 同步数量: data.count, 已同步数量: asyncCount, 余额为零数量: balanceIsZero },
173 });
174 }
175 }
176
177 module.exports = TransferController;
1 'use strict';
2
3 const Controller = require('egg').Controller;
4 const nodeSdk = require('@ddn/node-sdk').default;
5 const accountData = require('../../config/mem_accounts.json')
6 class TransferController extends Controller {
7 async postTransaction() {
8 const { ctx, config } = this;
9 const body = ctx.request.body;
10 const rule = {
11 secret: { type: 'string' },
12 amount: { type: 'string' },
13 transaction: { type: 'string' },
14 };
15 console.log(body);
16 try {
17 ctx.validate(rule);
18 } catch (error) {
19 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
20 return;
21 }
22 const oldNetTransaction = JSON.parse(body.transaction);
23 if (oldNetTransaction.recipientId !== config.ed25519Address) {
24 ctx.helper.err({
25 ctx,
26 err: '接收地址不正确,请使用正确的地址',
27 });
28 return;
29 }
30 // const oldNetTransaction = JSON.stringify(body.transaction);
31 // 旧网转出
32 // const oldNetData = await this.service.sdk.pushTransaction(JSON.stringify({ transaction: oldNetTransaction }), config.ed25519Url);
33 // console.log('就往转账', oldNetData, typeof oldNetData);
34 // if (!oldNetData || !oldNetData.success) {
35 // ctx.helper.err({
36 // ctx,
37 // data: oldNetData,
38 // });
39 // return;
40 // }
41 const result = await ctx.model.Transfer.create({ toAddress: body.receiverAddress, amount: body.amount, transaction: body.transaction, address: body.senderAddress });
42 ctx.helper.success({
43 ctx,
44 data: result,
45 });
46
47 // const transaction = await nodeSdk.transaction.createTransaction(body.receiverAccount, body.amount, body.remark || '网络升级转账', config.naclSecret);
48 // const trs = JSON.stringify({ transaction });
49 // // 上链操作
50 // const data = await this.service.sdk.pushTransaction(trs);
51 // if (data.success) {
52 // ctx.helper.success({
53 // ctx,
54 // data,
55 // });
56 // ctx.model.Transfer.update({ id: result.id }, { finish: true });
57 // } else {
58 // ctx.helper.err({
59 // ctx,
60 // data,
61 // });
62 // ctx.model.Transfer.update({ id: result.id }, { finish: false });
63 // }
64
65 }
66 async haveBalance() {
67 const { ctx, config } = this;
68 const query = ctx.request.query;
69 const rule = {
70 address: { type: 'string' },
71 };
72 console.log(query);
73 try {
74 ctx.validate(rule, query);
75 } catch (error) {
76 console.log(error);
77 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
78 return;
79 }
80 const data = await this.service.sdk.getAccount(query.address, config.ed25519Url);
81 ctx.helper.success({
82 ctx,
83 data,
84 });
85 }
86 async getBalance() {
87 const { ctx, config } = this;
88 const query = ctx.request.query;
89 const rule = {
90 address: { type: 'string' },
91 };
92 try {
93 ctx.validate(rule, query);
94 } catch (error) {
95 console.log(error);
96 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
97 return;
98 }
99 let balance = 0
100 const result = accountData.RECORDS.some(item => {
101 if (item.address === query.address && item.balance > 0) {
102 balance = item.balance
103 return true
104 }
105 })
106 ctx.helper.success({
107 ctx,
108 data: {
109 account: { balance }
110 },
111 });
112
113
114 }
115 async getTransaction() {
116 const { ctx, config } = this;
117 const query = ctx.request.query;
118 const rule = {
119 address: { type: 'string' },
120 };
121 console.log(query);
122 try {
123 ctx.validate(rule, query);
124 } catch (error) {
125 console.log(error);
126 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
127 return;
128 }
129 const data = await this.service.sdk.getAccount(query.address, config.naclUrl);
130 ctx.helper.success({
131 ctx,
132 data,
133 });
134 }
135 async asyncAccount() {
136 const { ctx, config } = this;
137 const limit = 100;
138 let asyncCount = 0;
139 let errAsyncAccount = 0;
140 let balanceIsZero = 0;
141 const data = await this.service.sdk.getAccountCount(config.ed25519Url);
142 if (data.success) {
143 const length = Math.ceil(data.count / 100);
144 console.log(data.count)
145 for (let index = 0; index < length; index++) {
146 const acountList = await this.service.sdk.getAccountList(index + 1, limit, config.ed25519Url);
147 console.log(acountList)
148 if (acountList.success) {
149 for (let index = 0; index < acountList.accounts.length; index++) {
150 const element = acountList.accounts[index];
151 if (element.balance == 0) {
152 balanceIsZero++
153 continue;
154 }
155 const result = await this.app.model.Accounts.create({ balance: element.balance, address: element.address, publicKey: element.publicKey });
156 if (!result) {
157 errAsyncAccount++;
158 this.ctx.logger.error(`存入账户数据出错 数据:${JSON.stringify(element)},地址:${config.ed25519Url},error: ${JSON.stringify(result)}`);
159 } else {
160 asyncCount++;
161 }
162 }
163 } else {
164 this.ctx.logger.error(`同步账户数据出错:页码:${index + 1},每页条数:${limit},地址:${config.ed25519Url}`);
165 }
166 }
167 } else {
168 this.ctx.logger.error(`同步账户数据请求账户数量出错:${config.ed25519Url}`);
169 }
170 ctx.helper.success({
171 ctx,
172 data: { 出错数量: errAsyncAccount, 同步数量: data.count, 已同步数量: asyncCount, 余额为零数量: balanceIsZero },
173 });
174 }
175 }
176
177 module.exports = TransferController;
1 'use strict';
2 const moment = require('moment');
3
4 module.exports = {
5
6 /**
7 * 对象拼接为字符串
8 * @param {object} object 待拼接的hash对象
9 */
10 concatHash(object) {
11 const keys = Object.keys(object).sort();
12 let paramsStr = '';
13 for (let index = 0; index < keys.length; index++) {
14 const key = keys[index];
15 let value = object[key];
16 // object参数类型转换为string
17 if (typeof (value) === 'object') {
18 value = JSON.stringify(value);
19 }
20 paramsStr = paramsStr + `${key}${value}`;
21 }
22 return paramsStr;
23 },
24 err({ ctx, code, err, status }) {
25 ctx.body = {
26 success: false,
27 code: code || 1,
28 err,
29 };
30 ctx.status = status || 200;
31 },
32
33 // 优化错误信息可读性
34 errorReadable(e) {
35 switch (e.code) {
36 case 'invalid_param': { // 参数错误
37 const errorHash = {
38 missing_field: '缺失',
39 };
40 let msg = '';
41 for (let i = 0; i < e.errors.length; i++) {
42 msg += '参数' + e.errors[i].field + ' ' + (errorHash[e.errors[i].code] || e.errors[i].message) + ';';
43 }
44 return msg;
45 }
46 default:
47 console.log('未优化类型错误!');
48 return JSON.stringify(e);
49 }
50 },
51
52 success({ ctx, data, status }) {
53 ctx.body = {
54 success: true,
55 code: 0,
56 data,
57 };
58 ctx.status = status || 200;
59 },
60
61 async verifySign(ctx) {
62 const authorization = ctx.request.headers.authorization;
63 const timestamp = ctx.request.headers.timestamp;
64 const publicKey = ctx.request.headers.publickey;
65 if (!timestamp) {
66 this.err({
67 ctx,
68 err: 'hearder: timestamp不能为空',
69 });
70 return false;
71 }
72 if (!authorization) {
73 this.err({
74 ctx,
75 err: 'hearder: authorization不能为空',
76 });
77 return false;
78 }
79 if (!publicKey) {
80 this.err({
81 ctx,
82 err: 'hearder: publicKey不能为空',
83 });
84 return false;
85 }
86 // 验证时间不大于5秒
87 const time = moment().valueOf();
88 if (Math.abs(Number(timestamp) - time) > (ctx.app.config.timeEquation || 5000)) {
89 ctx.body = {
90 code: 1,
91 err: '签名超时',
92 };
93 return false;
94 }
95
96 // 验证签名
97 // 1. 获取请求者公钥
98 const platformKey = await ctx.model.PlatformKey.findOne({ where: { publicKey } });
99 if (!platformKey) {
100 ctx.body = {
101 code: 1,
102 err: 'publicKey不存在',
103 };
104 return false;
105 }
106 if (!platformKey.usable) {
107 ctx.body = {
108 code: 1,
109 err: '密钥对不可用',
110 };
111 return;
112 }
113
114 // 2. 对签名进行验证
115
116 // 2.1 合并参数并排序组合拼接
117 const allParams = { ...ctx.params, ...ctx.request.query, ...ctx.request.body, timestamp };
118 const paramsStr = this.concatHash(allParams);
119
120 const verify = this.service.sdk.gm.sm2VerifySign(paramsStr, authorization, publicKey);
121 // 3. 比对时间戳
122 if (!verify) {
123 ctx.body = {
124 code: 1,
125 err: '签名无效',
126 };
127 return false;
128 }
129 // 记录当前媒体平台
130 ctx.platformId = platformKey.platformId;
131 return true;
132 },
133
134 };
1 'use strict';
2
3 module.exports = app => {
4 const { STRING, DATE, UUID, UUIDV1, JSON } = app.Sequelize;
5
6 const Account = app.model.define('transfer', {
7 id: {
8 allowNull: false,
9 primaryKey: true,
10 type: UUID,
11 defaultValue: UUIDV1,
12 },
13 address: { type: STRING, comment: '地址' },
14 toAddress: { type: STRING, comment: '接收地址' },
15 amount: { type: STRING, comment: '金额' },
16 transaction: { type: JSON, comment: '交易体' },
17 createdAt: { type: DATE },
18 updatedAt: { type: DATE },
19 });
20 return Account;
21 };
1 'use strict';
2
3 module.exports = app => {
4 const { STRING, DATE, UUID, UUIDV1, JSON, BOOLEAN } = app.Sequelize;
5
6 const Account = app.model.define('transfer', {
7 id: {
8 allowNull: false,
9 primaryKey: true,
10 type: UUID,
11 defaultValue: UUIDV1,
12 },
13 address: { type: STRING, comment: '地址' },
14 toAddress: { type: STRING, comment: '接收地址' },
15 amount: { type: STRING, comment: '金额' },
16 transaction: { type: JSON, comment: '交易体' },
17 finish: { type: BOOLEAN, comment: '是否完成' },
18 createdAt: { type: DATE },
19 updatedAt: { type: DATE },
20 });
21 return Account;
22 };
1 'use strict';
2
3 /**
4 * @param {Egg.Application} app - egg application
5 */
6 module.exports = app => {
7 const { router, controller } = app;
8 router.post('/upgrade/transfer/postTransaction', controller.transfer.postTransaction);
9 router.get('/upgrade/transfer/getBanlance', controller.transfer.getBalance);
10 router.get('/upgrade/transfer/transaction', controller.transfer.getTransaction);
11 router.get('/upgrade/transfer/async', controller.transfer.asyncAccount);
12 };
1 'use strict';
2
3 /**
4 * @param {Egg.Application} app - egg application
5 */
6 module.exports = app => {
7 const { router, controller } = app;
8 router.post('/upgrade/transfer/postTransaction', controller.transfer.postTransaction);
9 router.get('/upgrade/transfer/getBanlance', controller.transfer.getBalance);
10 router.get('/upgrade/transfer/haveBalance', controller.transfer.haveBalance);
11 router.get('/upgrade/transfer/transaction', controller.transfer.getTransaction);
12 router.get('/upgrade/transfer/async', controller.transfer.asyncAccount);
13 };
1 // eslint-disable-next-line strict
2 const Subscription = require('egg').Subscription;
3 const nodeSdk = require('@ddn/node-sdk').default;
4 class Transfer extends Subscription {
5 static get schedule() {
6 return {
7 interval: '10s',
8 type: 'worker',
9 };
10 }
11
12 async subscribe() {
13 const { ctx, config } = this;
14 const trsData = await ctx.model.Transfer.findAll({ where: { finish: false } });
15 console.log(trsData)
16 trsData.map(async item => {
17 if (item.id != "e2475660-9942-11ec-b873-171b64126906") {
18 return
19 }
20 const transaction = await nodeSdk.transaction.createTransaction(item.toAddress, item.amount, item.remark || '网络升级转账', config.naclSecret);
21 const trs = JSON.stringify({ transaction });
22 // 上链操作
23 const data = await this.service.sdk.pushTransaction(trs);
24 if (data.success) {
25 ctx.model.Transfer.update({ finish: true }, { where: { id: item.id } });
26 } else {
27 ctx.model.Transfer.update({ finish: false }, { where: { id: item.id } });
28 }
29 });
30 }
31 }
32
33 module.exports = Transfer;
1 // eslint-disable-next-line strict
2 const Subscription = require('egg').Subscription;
3 const nodeSdk = require('@ddn/node-sdk').default;
4 class Transfer extends Subscription {
5 static get schedule() {
6 return {
7 interval: '10s',
8 type: 'worker',
9 };
10 }
11
12 async subscribe() {
13 const { ctx, config } = this;
14 const trsData = await ctx.model.Transfer.findAll({ where: { finish: false } });
15 console.log(trsData)
16 trsData.map(async item => {
17 // if (item.id != "e2475660-9942-11ec-b873-171b64126906") {
18 // return
19 // }
20 const transaction = await nodeSdk.transaction.createTransaction(item.toAddress, item.amount, item.remark || '网络升级转账', config.naclSecret);
21 const trs = JSON.stringify({ transaction });
22 // 上链操作
23 const data = await this.service.sdk.pushTransaction(trs);
24 if (data.success) {
25 ctx.model.Transfer.update({ finish: true }, { where: { id: item.id } });
26 } else {
27 ctx.model.Transfer.update({ finish: false }, { where: { id: item.id } });
28 }
29 });
30 }
31 }
32
33 module.exports = Transfer;
1 'use strict';
2
3 const Service = require('egg').Service;
4 const request = require('request');
5 class DdnService extends Service {
6
7 // 根据前端传过来的地址查询链上数据
8 async getDDNDataByUrl(url) {
9 const { config } = this;
10 const result = await new Promise((resolve, reject) => {
11 request({
12 url,
13 method: 'GET',
14 headers: {
15 'Content-Type': 'application/json',
16 version: '',
17 nethash: config.nethash,
18 },
19 }, (error, response, body) => {
20 if (error) {
21 reject(error);
22 } else {
23 resolve(JSON.parse(body));
24 }
25 });
26 });
27 return result;
28 }
29 async getPeers() {
30 const { config } = this;
31 const peer_host = await this.getPeerHost();
32 const url = `${peer_host}/api/peers`;
33 const result = await new Promise(resolve => {
34 request({
35 url,
36 method: 'GET',
37 headers: {
38 'Content-Type': 'application/json',
39 version: '',
40 nethash: config.nethash,
41 },
42 }, (error, response, body) => {
43 resolve(JSON.parse(body));
44 });
45 });
46 return result;
47 }
48 // 区块高度
49 async getHeight() {
50 const { config } = this;
51 const peer_host = await this.getPeerHost();
52 const url = `${peer_host}/api/blocks/getHeight`;
53 const result = await new Promise(resolve => {
54 request({
55 url,
56 method: 'GET',
57 headers: {
58 'Content-Type': 'application/json',
59 version: '',
60 nethash: config.nethash,
61 },
62 }, (error, response, body) => {
63 resolve(JSON.parse(body));
64 });
65 });
66 return result;
67 }
68 // 交易详情
69 async getTransaction(id) {
70 const { config } = this;
71 const peer_host = await this.getPeerHost();
72 const url = `${peer_host}/api/transactions/get?id=${id}`;
73 const result = await new Promise(resolve => {
74 request({
75 url,
76 method: 'GET',
77 headers: {
78 'Content-Type': 'application/json',
79 version: '',
80 nethash: config.nethash,
81 },
82 }, (error, response, body) => {
83 resolve(JSON.parse(body));
84 });
85 });
86 return result;
87 }
88 // 交易列表
89 async getTransactions(con, page = 1, per_page = 10) {
90 const { config } = this;
91 const peer_host = await this.getPeerHost();
92 const conditions = [];
93 if (con.sender_id) {
94 conditions.push('senderId=' + con.sender_id);
95 }
96 if (con.recipient_id) {
97 conditions.push('recipientId=' + con.recipient_id);
98 }
99 conditions.push('limit=' + Number(per_page));
100 conditions.push('offset=' + (Number(page) - 1) * Number(per_page));
101 const url = `${peer_host}/api/transactions?${conditions.join('&')}`;
102 const result = await new Promise(resolve => {
103 request({
104 url,
105 method: 'GET',
106 headers: {
107 'Content-Type': 'application/json',
108 version: '',
109 nethash: config.nethash,
110 },
111 }, (error, response, body) => {
112 resolve(JSON.parse(body));
113 });
114 });
115 return result;
116 }
117
118 // 账户信息
119 async getAccount(address) {
120 const { config } = this;
121 const peer_host = await this.getPeerHost();
122 const url = `${peer_host}/api/accounts?address=${address}`;
123 const result = await new Promise((resolve, reject) => {
124 request({
125 url,
126 method: 'GET',
127 headers: {
128 'Content-Type': 'application/json',
129 version: '',
130 nethash: config.nethash,
131 },
132 }, (error, response, body) => {
133 if (error) {
134 reject(error);
135 }
136 resolve(JSON.parse(body));
137 });
138 });
139 return result;
140 }
141
142 // 交易上链
143 async pushTransaction(trs) {
144 const { config } = this;
145 const peer_host = await this.getPeerHost();
146 const url = `${peer_host}/peer/transactions`;
147 let ddn_result = await new Promise(resolve => {
148 request({
149 url,
150 method: 'POST',
151 headers: {
152 'Content-Type': 'application/json',
153 version: '',
154 nethash: config.nethash,
155 },
156 body: trs,
157 }, (error, response, body) => {
158 resolve(body);
159 });
160 });
161 ddn_result = JSON.parse(ddn_result);
162 return ddn_result;
163 }
164
165 // 获取结点地址
166 async getPeerHost() {
167 // const peer_host = await this.service.sdk.redis.get('peer_host');
168 // if (!peer_host) {
169 const res = await this.checkPeerHost();
170 return res;
171 // }
172 // return peer_host.host;
173 }
174
175 // 检查结点并指定可用结点
176 async checkPeerHost() {
177 const { config } = this;
178 // const peers = this.config.peer_list;
179 // const peers = [ 'http://120.24.69.99:8003' ];
180 const peers = [ 'http://localhost:8001' ];
181 const peers_height = {};
182 let max_height = null;
183 let min_height = null;
184 let best_peer = null;
185 for (let i = 0; i < peers.length; i++) {
186 const url = `${peers[i]}/api/blocks/getstatus`;
187 const result = await new Promise(resolve => {
188 request({
189 url,
190 method: 'GET',
191 headers: {
192 'Content-Type': 'application/json',
193 version: '',
194 nethash: config.nethash,
195 },
196 }, (error, response, body) => {
197 if (error) {
198 resolve({ success: false });
199 } else {
200 resolve(JSON.parse(body));
201 }
202 });
203 });
204 if (result.success) {
205 peers_height[peers[i]] = result.height;
206 if (!max_height || Number(result.height) > Number(max_height)) {
207 max_height = result.height;
208 }
209 if (!min_height || Number(result.height) < Number(min_height)) {
210 min_height = result.height;
211 }
212 if (!best_peer || Number(result.height) === Number(max_height)) {
213 best_peer = peers[i];
214 }
215 } else {
216 // 节点不可达,报警
217
218 // has_error = true;
219 // this.service.sdk.alisms.peerWarning(peers[i].split(/\.|\:/)[4] + 'lost', config.admin_phone);
220 }
221 }
222 // // 高度相差3个,就报警
223 // if (max_height - min_height > 3) {
224 // console.log('高度相差3个,报警');
225 // has_error = true;
226 // this.service.sdk.alisms.peerWarning('HeighGt3', config.admin_phone);
227 // }
228 // if (!best_peer) {
229 // has_error = true;
230 // this.service.sdk.alisms.peerWarning('noPeer', config.admin_phone);
231 // }
232 // // 如果没有异常了,就再次打开监控通知,开始监控和通知
233 // if (!has_error) {
234 // await this.service.sdk.redis.set('peer_warning', { status: 'on' });
235 // }
236 // await this.service.sdk.redis.set('peer_host', { host: best_peer });
237 // await this.service.sdk.redis.set('peers_height', peers_height);
238 return best_peer;
239 }
240
241
242 }
243
244 module.exports = DdnService;
1 'use strict';
2
3 const Service = require('egg').Service;
4 const request = require('request');
5 class DdnService extends Service {
6
7 // 根据前端传过来的地址查询链上数据
8 async getDDNDataByUrl(url) {
9 const { config } = this;
10 const result = await new Promise((resolve, reject) => {
11 request({
12 url,
13 method: 'GET',
14 headers: {
15 'Content-Type': 'application/json',
16 version: '',
17 nethash: config.nethash,
18 },
19 }, (error, response, body) => {
20 if (error) {
21 reject(error);
22 } else {
23 resolve(JSON.parse(body));
24 }
25 });
26 });
27 return result;
28 }
29 async getPeers() {
30 const { config } = this;
31 const peer_host = await this.getPeerHost();
32 const url = `${peer_host}/api/peers`;
33 const result = await new Promise(resolve => {
34 request({
35 url,
36 method: 'GET',
37 headers: {
38 'Content-Type': 'application/json',
39 version: '',
40 nethash: config.nethash,
41 },
42 }, (error, response, body) => {
43 resolve(JSON.parse(body));
44 });
45 });
46 return result;
47 }
48 // 区块高度
49 async getHeight() {
50 const { config } = this;
51 const peer_host = await this.getPeerHost();
52 const url = `${peer_host}/api/blocks/getHeight`;
53 const result = await new Promise(resolve => {
54 request({
55 url,
56 method: 'GET',
57 headers: {
58 'Content-Type': 'application/json',
59 version: '',
60 nethash: config.nethash,
61 },
62 }, (error, response, body) => {
63 resolve(JSON.parse(body));
64 });
65 });
66 return result;
67 }
68 // 交易详情
69 async getTransaction(id) {
70 const { config } = this;
71 const peer_host = await this.getPeerHost();
72 const url = `${peer_host}/api/transactions/get?id=${id}`;
73 const result = await new Promise(resolve => {
74 request({
75 url,
76 method: 'GET',
77 headers: {
78 'Content-Type': 'application/json',
79 version: '',
80 nethash: config.nethash,
81 },
82 }, (error, response, body) => {
83 resolve(JSON.parse(body));
84 });
85 });
86 return result;
87 }
88 // 交易列表
89 async getTransactions(con, page = 1, per_page = 10) {
90 const { config } = this;
91 const peer_host = await this.getPeerHost();
92 const conditions = [];
93 if (con.sender_id) {
94 conditions.push('senderId=' + con.sender_id);
95 }
96 if (con.recipient_id) {
97 conditions.push('recipientId=' + con.recipient_id);
98 }
99 conditions.push('limit=' + Number(per_page));
100 conditions.push('offset=' + (Number(page) - 1) * Number(per_page));
101 const url = `${peer_host}/api/transactions?${conditions.join('&')}`;
102 const result = await new Promise(resolve => {
103 request({
104 url,
105 method: 'GET',
106 headers: {
107 'Content-Type': 'application/json',
108 version: '',
109 nethash: config.nethash,
110 },
111 }, (error, response, body) => {
112 resolve(JSON.parse(body));
113 });
114 });
115 return result;
116 }
117
118 // 账户信息
119 async getAccount(address) {
120 const { config } = this;
121 const peer_host = await this.getPeerHost();
122 const url = `${peer_host}/api/accounts?address=${address}`;
123 const result = await new Promise((resolve, reject) => {
124 request({
125 url,
126 method: 'GET',
127 headers: {
128 'Content-Type': 'application/json',
129 version: '',
130 nethash: config.nethash,
131 },
132 }, (error, response, body) => {
133 if (error) {
134 reject(error);
135 }
136 resolve(JSON.parse(body));
137 });
138 });
139 return result;
140 }
141
142 // 交易上链
143 async pushTransaction(trs) {
144 const { config } = this;
145 const peer_host = await this.getPeerHost();
146 const url = `${peer_host}/peer/transactions`;
147 let ddn_result = await new Promise(resolve => {
148 request({
149 url,
150 method: 'POST',
151 headers: {
152 'Content-Type': 'application/json',
153 version: '',
154 nethash: config.nethash,
155 },
156 body: trs,
157 }, (error, response, body) => {
158 resolve(body);
159 });
160 });
161 ddn_result = JSON.parse(ddn_result);
162 return ddn_result;
163 }
164
165 // 获取结点地址
166 async getPeerHost() {
167 // const peer_host = await this.service.sdk.redis.get('peer_host');
168 // if (!peer_host) {
169 const res = await this.checkPeerHost();
170 return res;
171 // }
172 // return peer_host.host;
173 }
174
175 // 检查结点并指定可用结点
176 async checkPeerHost() {
177 const { config } = this;
178 // const peers = this.config.peer_list;
179 // const peers = [ 'http://120.24.69.99:8003' ];
180 const peers = ['http://localhost:8001'];
181 const peers_height = {};
182 let max_height = null;
183 let min_height = null;
184 let best_peer = null;
185 for (let i = 0; i < peers.length; i++) {
186 const url = `${peers[i]}/api/blocks/getstatus`;
187 const result = await new Promise(resolve => {
188 request({
189 url,
190 method: 'GET',
191 headers: {
192 'Content-Type': 'application/json',
193 version: '',
194 nethash: config.nethash,
195 },
196 }, (error, response, body) => {
197 if (error) {
198 resolve({ success: false });
199 } else {
200 resolve(JSON.parse(body));
201 }
202 });
203 });
204 if (result.success) {
205 peers_height[peers[i]] = result.height;
206 if (!max_height || Number(result.height) > Number(max_height)) {
207 max_height = result.height;
208 }
209 if (!min_height || Number(result.height) < Number(min_height)) {
210 min_height = result.height;
211 }
212 if (!best_peer || Number(result.height) === Number(max_height)) {
213 best_peer = peers[i];
214 }
215 } else {
216 }
217 }
218
219 return best_peer;
220 }
221
222
223 }
224
225 module.exports = DdnService;
1 'use strict';
2
3 const Service = require('egg').Service;
4 const request = require('request');
5 class DdnService extends Service {
6
7 async getPeers() {
8 const { config } = this;
9 const peer_host = await this.getPeerHost();
10 const url = `${peer_host}/api/peers`;
11 const result = await new Promise(resolve => {
12 request({
13 url,
14 method: 'GET',
15 headers: {
16 'Content-Type': 'application/json',
17 version: '',
18 nethash: config.nethash,
19 },
20 }, (error, response, body) => {
21 resolve(JSON.parse(body));
22 });
23 });
24 return result;
25 }
26 // 区块高度
27 async getHeight() {
28 const { config } = this;
29 const peer_host = await this.getPeerHost();
30 const url = `${peer_host}/api/blocks/getHeight`;
31 const result = await new Promise(resolve => {
32 request({
33 url,
34 method: 'GET',
35 headers: {
36 'Content-Type': 'application/json',
37 version: '',
38 nethash: config.nethash,
39 },
40 }, (error, response, body) => {
41 resolve(JSON.parse(body));
42 });
43 });
44 return result;
45 }
46 // 交易详情
47 async getTransaction(id) {
48 const { config } = this;
49 const peer_host = await this.getPeerHost();
50 const url = `${peer_host}/api/transactions/get?id=${id}`;
51 const result = await new Promise(resolve => {
52 request({
53 url,
54 method: 'GET',
55 headers: {
56 'Content-Type': 'application/json',
57 version: '',
58 nethash: config.nethash,
59 },
60 }, (error, response, body) => {
61 resolve(JSON.parse(body));
62 });
63 });
64 return result;
65 }
66 // 交易列表
67 async getTransactions(con, page = 1, per_page = 10) {
68 const { config } = this;
69 const peer_host = await this.getPeerHost();
70 const conditions = [];
71 if (con.sender_id) {
72 conditions.push('senderId=' + con.sender_id);
73 }
74 if (con.recipient_id) {
75 conditions.push('recipientId=' + con.recipient_id);
76 }
77 conditions.push('limit=' + Number(per_page));
78 conditions.push('offset=' + (Number(page) - 1) * Number(per_page));
79 const url = `${peer_host}/api/transactions?${conditions.join('&')}`;
80 const result = await new Promise(resolve => {
81 request({
82 url,
83 method: 'GET',
84 headers: {
85 'Content-Type': 'application/json',
86 version: '',
87 nethash: config.nethash,
88 },
89 }, (error, response, body) => {
90 resolve(JSON.parse(body));
91 });
92 });
93 return result;
94 }
95
96 // 账户信息
97 async getAccount(address) {
98 const { config } = this;
99 const peer_host = await this.getPeerHost();
100 const url = `${peer_host}/api/accounts?address=${address}`;
101 const result = await new Promise((resolve, reject) => {
102 request({
103 url,
104 method: 'GET',
105 headers: {
106 'Content-Type': 'application/json',
107 version: '',
108 nethash: config.nethash,
109 },
110 }, (error, response, body) => {
111 if (error) {
112 reject(error);
113 }
114 resolve(JSON.parse(body));
115 });
116 });
117 return result;
118 }
119
120 // 交易上链
121 async pushTransaction(trs) {
122 const { config } = this;
123 const peer_host = await this.getPeerHost();
124 const url = `${peer_host}/peer/transactions`;
125 let ddn_result = await new Promise(resolve => {
126 request({
127 url,
128 method: 'POST',
129 headers: {
130 'Content-Type': 'application/json',
131 version: '',
132 nethash: config.nethash,
133 },
134 body: trs,
135 }, (error, response, body) => {
136 resolve(body);
137 });
138 });
139 ddn_result = JSON.parse(ddn_result);
140 return ddn_result;
141 }
142
143 // 获取结点地址
144 async getPeerHost() {
145 // const peer_host = await this.service.sdk.redis.get('peer_host');
146 // if (!peer_host) {
147 const res = await this.checkPeerHost();
148 return res;
149 // }
150 // return peer_host.host;
151 }
152
153 // 检查结点并指定可用结点
154 async checkPeerHost() {
155 const { config } = this;
156 // const peers = this.config.peer_list;
157 // const peers = [ 'http://120.24.69.99:8003' ];
158 const peers = ['http://localhost:8001'];
159 const peers_height = {};
160 let max_height = null;
161 let min_height = null;
162 let best_peer = null;
163 for (let i = 0; i < peers.length; i++) {
164 const url = `${peers[i]}/api/blocks/getstatus`;
165 const result = await new Promise(resolve => {
166 request({
167 url,
168 method: 'GET',
169 headers: {
170 'Content-Type': 'application/json',
171 version: '',
172 nethash: config.nethash,
173 },
174 }, (error, response, body) => {
175 if (error) {
176 resolve({ success: false });
177 } else {
178 resolve(JSON.parse(body));
179 }
180 });
181 });
182 if (result.success) {
183 peers_height[peers[i]] = result.height;
184 if (!max_height || Number(result.height) > Number(max_height)) {
185 max_height = result.height;
186 }
187 if (!min_height || Number(result.height) < Number(min_height)) {
188 min_height = result.height;
189 }
190 if (!best_peer || Number(result.height) === Number(max_height)) {
191 best_peer = peers[i];
192 }
193 } else {
194 }
195 }
196
197 return best_peer;
198 }
199
200
201 }
202
203 module.exports = DdnService;
1 /* eslint valid-jsdoc: "off" */
2
3 'use strict';
4
5 /**
6 * @param {Egg.EggAppInfo} appInfo app info
7 */
8 module.exports = appInfo => {
9 /**
10 * built-in config
11 * @type {Egg.EggAppConfig}
12 **/
13 const config = exports = {};
14
15 // use for cookie sign key, should change to your own and keep security
16 config.keys = appInfo.name + '_1644800905769_2309';
17
18 // add your middleware config here
19 config.middleware = [];
20
21 // add your user config here
22 const userConfig = {
23 // myAppName: 'egg',
24 };
25
26 return {
27 ...config,
28 ...userConfig,
29 };
30 };
1 /* eslint valid-jsdoc: "off" */
2
3 'use strict';
4
5 /**
6 * @param {Egg.EggAppInfo} appInfo app info
7 */
8 module.exports = appInfo => {
9 /**
10 * built-in config
11 * @type {Egg.EggAppConfig}
12 **/
13 const config = exports = {};
14
15 // use for cookie sign key, should change to your own and keep security
16 config.keys = appInfo.name + '_1644800905769_2309';
17
18 // add your middleware config here
19 config.middleware = [];
20
21 config.sequelize = {
22 host: 'localhost',
23 port: '5432',
24 username: 'postgres',
25 password: 'wawjr1314',
26 database: 'jh',
27 dialect: 'postgres',
28 pool: {
29 max: 5,
30 min: 0,
31 idle: 10000,
32 },
33 logging: true,
34 };
35
36 // add your user config here
37 const userConfig = {
38 // myAppName: 'egg',
39 };
40
41 return {
42 ...config,
43 ...userConfig,
44 };
45 };
1 /* eslint valid-jsdoc: "off" */
2
3 'use strict';
4
5 /**
6 * @param {Egg.EggAppInfo} appInfo app info
7 */
8 module.exports = appInfo => {
9 /**
10 * built-in config
11 * @type {Egg.EggAppConfig}
12 **/
13 const config = exports = {};
14
15 // use for cookie sign key, should change to your own and keep security
16 config.keys = appInfo.name + '_1644800905769_2309';
17
18 // add your middleware config here
19 config.middleware = [];
20
21 config.sequelize = {
22 host: 'localhost',
23 port: '5432',
24 username: 'postgres',
25 password: 'wawjr1314',
26 database: 'ddn',
27 dialect: 'postgres',
28 pool: {
29 max: 5,
30 min: 0,
31 idle: 10000,
32 },
33 logging: true,
34 };
35
36 // add your user config here
37 const userConfig = {
38 // myAppName: 'egg',
39 };
40
41 return {
42 ...config,
43 ...userConfig,
44 };
45 };
1 /* eslint valid-jsdoc: "off" */
2
3 'use strict';
4
5 /**
6 * @param {Egg.EggAppInfo} appInfo app info
7 */
8 module.exports = appInfo => {
9 /**
10 * built-in config
11 * @type {Egg.EggAppConfig}
12 **/
13 const config = exports = {};
14
15 // use for cookie sign key, should change to your own and keep security
16 config.keys = appInfo.name + '_1644800905769_2309';
17
18 // add your middleware config here
19 config.middleware = [];
20
21 config.sequelize = {
22 host: 'localhost',
23 port: '5432',
24 username: 'postgres',
25 password: 'wawjr1314',
26 database: 'ddn',
27 dialect: 'postgres',
28 pool: {
29 max: 5,
30 min: 0,
31 idle: 10000,
32 },
33 logging: true,
34 };
35 config.ed25519Address = '',
36 config.naclAddress = '',
37 // add your user config here
38 const userConfig = {
39 // myAppName: 'egg',
40 };
41
42 return {
43 ...config,
44 ...userConfig,
45 };
46 };
1 /* eslint valid-jsdoc: "off" */
2
3 'use strict';
4
5 /**
6 * @param {Egg.EggAppInfo} appInfo app info
7 */
8 module.exports = appInfo => {
9 /**
10 * built-in config
11 * @type {Egg.EggAppConfig}
12 **/
13 const config = exports = {};
14
15 // use for cookie sign key, should change to your own and keep security
16 config.keys = appInfo.name + '_1644800905769_2309';
17
18 // add your middleware config here
19 config.middleware = [];
20
21 config.sequelize = {
22 host: 'localhost',
23 port: '5432',
24 username: 'postgres',
25 password: 'wawjr1314',
26 database: 'ddn',
27 dialect: 'postgres',
28 pool: {
29 max: 5,
30 min: 0,
31 idle: 10000,
32 },
33 logging: true,
34 };
35 config.ed25519Address = '';
36 config.naclAddress = '';
37 // add your user config here
38 const userConfig = {
39 // myAppName: 'egg',
40 };
41
42 return {
43 ...config,
44 ...userConfig,
45 };
46 };
1 /* eslint valid-jsdoc: "off" */
2
3 'use strict';
4
5 /**
6 * @param {Egg.EggAppInfo} appInfo app info
7 */
8 module.exports = appInfo => {
9 /**
10 * built-in config
11 * @type {Egg.EggAppConfig}
12 **/
13 const config = exports = {};
14
15 // use for cookie sign key, should change to your own and keep security
16 config.keys = appInfo.name + '_1644800905769_2309';
17
18 // add your middleware config here
19 config.middleware = [];
20
21 config.sequelize = {
22 host: 'localhost',
23 port: '5432',
24 username: 'postgres',
25 password: 'wawjr1314',
26 database: 'ddn',
27 dialect: 'postgres',
28 pool: {
29 max: 5,
30 min: 0,
31 idle: 10000,
32 },
33 logging: true,
34 };
35 config.ed25519Address = '';
36 config.naclAddress = '';
37 config.naclSecret = '';
38 // add your user config here
39 const userConfig = {
40 // myAppName: 'egg',
41 };
42
43 return {
44 ...config,
45 ...userConfig,
46 };
47 };
1 /* eslint valid-jsdoc: "off" */
2
3 'use strict';
4
5 /**
6 * @param {Egg.EggAppInfo} appInfo app info
7 */
8 module.exports = appInfo => {
9 const config = exports = {};
10 config.ed25519Address = 'DFy6P2sN1KLDNtppnqLBPWcnH8GTJRouGj'; // 旧网接收钱包地址
11 // config.naclAddress = 'D3EFYncByWwzsSQvRsVbufBfmVstuf11QW'; // 新网转账钱包地址
12 config.naclSecret = 'predict upon can egg glare desk draw arm hub auction language upper'; // 新网转账钱包秘钥
13 config.ed25519Url = 'http://120.24.69.99:8001'; // 旧网节点地址
14 // config.naclUrl = 'http://8.142.20.158:9000'; // 新网节点地址
15 config.naclUrl = 'http://localhost:8001'; // 测试地址
16 config.ed25519NetHash = 'b11fa2f2';// 旧网nethash
17 config.naclNetHash = '0ab796cd'; // 新网nethash
18 // add your user config here
19 const userConfig = {
20 // myAppName: 'egg',
21 };
22
23 return {
24 ...config,
25 ...userConfig,
26 };
27 };
1 /* eslint valid-jsdoc: "off" */
2
3 'use strict';
4
5 /**
6 * @param {Egg.EggAppInfo} appInfo app info
7 */
8 module.exports = appInfo => {
9 const config = exports = {};
10 config.ed25519Address = 'DFy6P2sN1KLDNtppnqLBPWcnH8GTJRouGj'; // 旧网接收钱包地址
11 // config.naclAddress = 'D3EFYncByWwzsSQvRsVbufBfmVstuf11QW'; // 新网转账钱包地址
12 config.naclSecret = 'predict upon can egg glare desk draw arm hub auction language upper'; // 新网转账钱包秘钥
13 config.ed25519Url = 'http://120.24.69.99:8001'; // 旧网节点地址
14 config.naclUrl = 'http://8.142.20.158:8001'; // 新网节点地址
15 // config.naclUrl = 'http://localhost:8001'; // 测试地址
16 config.ed25519NetHash = 'b11fa2f2';// 旧网nethash
17 config.naclNetHash = '0ab796cd'; // 新网nethash
18 // add your user config here
19 const userConfig = {
20 // myAppName: 'egg',
21 };
22
23 return {
24 ...config,
25 ...userConfig,
26 };
27 };
1 'use strict';
2
3 /** @type Egg.EggPlugin */
4 module.exports = {
5 // had enabled by egg
6 // static: {
7 // enable: true,
8 // }
9 };
1 'use strict';
2
3 /** @type Egg.EggPlugin */
4 module.exports = {
5 // had enabled by egg
6 // static: {
7 // enable: true,
8 // }
9 cors: {
10 enable: true,
11 package: 'egg-cors',
12 },
13 sequelize: {
14 enable: true,
15 package: 'egg-sequelize',
16 },
17 validate: {
18 enable: true,
19 package: 'egg-validate',
20 },
21 };
1 'use strict';
2
3 /** @type Egg.EggPlugin */
4 module.exports = {
5 // had enabled by egg
6 // static: {
7 // enable: true,
8 // }
9 // cors: {
10 // enable: true,
11 // package: 'egg-cors',
12 // },
13 sequelize: {
14 enable: true,
15 package: 'egg-sequelize',
16 },
17 validate: {
18 enable: true,
19 package: 'egg-validate',
20 },
21 };
1 {
2 "name": "DDNTrs",
3 "version": "1.0.0",
4 "description": "DDN",
5 "private": true,
6 "egg": {
7 "declarations": true
8 },
9 "dependencies": {
10 "egg": "^2.15.1",
11 "egg-scripts": "^2.11.0"
12 },
13 "devDependencies": {
14 "autod": "^3.0.1",
15 "autod-egg": "^1.1.0",
16 "egg-bin": "^4.11.0",
17 "egg-ci": "^1.11.0",
18 "egg-mock": "^3.21.0",
19 "eslint": "^5.13.0",
20 "eslint-config-egg": "^7.1.0"
21 },
22 "engines": {
23 "node": ">=10.0.0"
24 },
25 "scripts": {
26 "start": "egg-scripts start --daemon --title=egg-server-DDNTrs",
27 "stop": "egg-scripts stop --title=egg-server-DDNTrs",
28 "dev": "egg-bin dev",
29 "debug": "egg-bin debug",
30 "test": "npm run lint -- --fix && npm run test-local",
31 "test-local": "egg-bin test",
32 "cov": "egg-bin cov",
33 "lint": "eslint .",
34 "ci": "npm run lint && npm run cov",
35 "autod": "autod"
36 },
37 "ci": {
38 "version": "10"
39 },
40 "repository": {
41 "type": "git",
42 "url": ""
43 },
44 "author": "creazy",
45 "license": "MIT"
46 }
1 {
2 "name": "DDNTrs",
3 "version": "1.0.0",
4 "description": "DDN upgrade server",
5 "private": true,
6 "egg": {
7 "declarations": true
8 },
9 "dependencies": {
10 "egg": "^2.15.1",
11 "egg-scripts": "^2.11.0"
12 },
13 "devDependencies": {
14 "autod": "^3.0.1",
15 "autod-egg": "^1.1.0",
16 "egg-bin": "^4.11.0",
17 "egg-ci": "^1.11.0",
18 "egg-mock": "^3.21.0",
19 "eslint": "^5.13.0",
20 "eslint-config-egg": "^7.1.0"
21 },
22 "engines": {
23 "node": ">=10.0.0"
24 },
25 "scripts": {
26 "start": "egg-scripts start --daemon --title=egg-server-DDNTrs",
27 "stop": "egg-scripts stop --title=egg-server-DDNTrs",
28 "dev": "egg-bin dev",
29 "debug": "egg-bin debug",
30 "test": "npm run lint -- --fix && npm run test-local",
31 "test-local": "egg-bin test",
32 "cov": "egg-bin cov",
33 "lint": "eslint .",
34 "ci": "npm run lint && npm run cov",
35 "autod": "autod"
36 },
37 "ci": {
38 "version": "10"
39 },
40 "repository": {
41 "type": "git",
42 "url": ""
43 },
44 "author": "creazy",
45 "license": "MIT"
46 }
1 {
2 "name": "DDNTrs",
3 "version": "1.0.0",
4 "description": "DDN upgrade server",
5 "private": true,
6 "egg": {
7 "declarations": true
8 },
9 "dependencies": {
10 "@ddn/node-sdk": "^2.1.2",
11 "egg": "^2.15.1",
12 "egg-scripts": "^2.11.0",
13 "egg-sequelize": "^6.0.0",
14 },
15 "devDependencies": {
16 "autod": "^3.0.1",
17 "autod-egg": "^1.1.0",
18 "egg-bin": "^4.11.0",
19 "egg-ci": "^1.11.0",
20 "egg-mock": "^3.21.0",
21 "eslint": "^5.13.0",
22 "eslint-config-egg": "^7.1.0"
23 },
24 "engines": {
25 "node": ">=10.0.0"
26 },
27 "scripts": {
28 "start": "egg-scripts start --daemon --title=egg-server-DDNTrs",
29 "stop": "egg-scripts stop --title=egg-server-DDNTrs",
30 "dev": "egg-bin dev",
31 "debug": "egg-bin debug",
32 "test": "npm run lint -- --fix && npm run test-local",
33 "test-local": "egg-bin test",
34 "cov": "egg-bin cov",
35 "lint": "eslint .",
36 "ci": "npm run lint && npm run cov",
37 "autod": "autod"
38 },
39 "ci": {
40 "version": "10"
41 },
42 "repository": {
43 "type": "git",
44 "url": ""
45 },
46 "author": "creazy",
47 "license": "MIT"
48 }
1 {
2 "name": "DDNTrs",
3 "version": "1.0.0",
4 "description": "DDN upgrade server",
5 "private": true,
6 "egg": {
7 "declarations": true
8 },
9 "dependencies": {
10 "@ddn/node-sdk": "^2.1.2",
11 "egg": "^2.15.1",
12 "egg-scripts": "^2.11.0",
13 "egg-sequelize": "^6.0.0"
14 },
15 "devDependencies": {
16 "autod": "^3.0.1",
17 "autod-egg": "^1.1.0",
18 "egg-bin": "^4.11.0",
19 "egg-ci": "^1.11.0",
20 "egg-mock": "^3.21.0",
21 "eslint": "^5.13.0",
22 "eslint-config-egg": "^7.1.0"
23 },
24 "engines": {
25 "node": ">=10.0.0"
26 },
27 "scripts": {
28 "start": "egg-scripts start --daemon --title=egg-server-DDNTrs",
29 "stop": "egg-scripts stop --title=egg-server-DDNTrs",
30 "dev": "egg-bin dev",
31 "debug": "egg-bin debug",
32 "test": "npm run lint -- --fix && npm run test-local",
33 "test-local": "egg-bin test",
34 "cov": "egg-bin cov",
35 "lint": "eslint .",
36 "ci": "npm run lint && npm run cov",
37 "autod": "autod"
38 },
39 "ci": {
40 "version": "10"
41 },
42 "repository": {
43 "type": "git",
44 "url": ""
45 },
46 "author": "creazy",
47 "license": "MIT"
48 }
1 'use strict';
2
3 const path = require('path');
4
5 module.exports = {
6 config: path.join(__dirname, 'database/config.json'),
7 'migrations-path': path.join(__dirname, 'database/migrations'),
8 'seeders-path': path.join(__dirname, 'database/seeders'),
9 'models-path': path.join(__dirname, 'app/model'),
10 };
...\ No newline at end of file ...\ No newline at end of file
1
2 language: node_js
3 node_js:
4 - '10'
5 before_install:
6 - npm i npminstall -g
7 install:
8 - npminstall
9 script:
10 - npm run ci
11 after_script:
12 - npminstall codecov && codecov
1 # DDNTrs
2
3 DDN
4
5 ## QuickStart
6
7 <!-- add docs here for user -->
8
9 see [egg docs][egg] for more detail.
10
11 ### Development
12
13 ```bash
14 $ npm i
15 $ npm run dev
16 $ open http://localhost:7001/
17 ```
18
19 ### Deploy
20
21 ```bash
22 $ npm start
23 $ npm stop
24 ```
25
26 ### npm scripts
27
28 - Use `npm run lint` to check code style.
29 - Use `npm test` to run unit test.
30 - Use `npm run autod` to auto detect dependencies upgrade, see [autod](https://www.npmjs.com/package/autod) for more detail.
31
32
33 [egg]: https://eggjs.org
...\ No newline at end of file ...\ No newline at end of file
1 'use strict';
2
3 const Controller = require('egg').Controller;
4
5 class HomeController extends Controller {
6 async index() {
7 const { ctx } = this;
8 ctx.body = 'hi, egg';
9 }
10 }
11
12 module.exports = HomeController;
1 'use strict';
2
3 const Controller = require('egg').Controller;
4 const nodeSdk = require('@ddn/node-sdk').default;
5 const accountData = require('../../config/mem_accounts.json')
6 class TransferController extends Controller {
7 async postTransaction() {
8 const { ctx, config } = this;
9 const body = ctx.request.body;
10 const rule = {
11 secret: { type: 'string' },
12 amount: { type: 'string' },
13 transaction: { type: 'string' },
14 };
15 console.log(body);
16 try {
17 ctx.validate(rule);
18 } catch (error) {
19 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
20 return;
21 }
22 const oldNetTransaction = JSON.parse(body.transaction);
23 if (oldNetTransaction.recipientId !== config.ed25519Address) {
24 ctx.helper.err({
25 ctx,
26 err: '接收地址不正确,请使用正确的地址',
27 });
28 return;
29 }
30 // const oldNetTransaction = JSON.stringify(body.transaction);
31 // 旧网转出
32 // const oldNetData = await this.service.sdk.pushTransaction(JSON.stringify({ transaction: oldNetTransaction }), config.ed25519Url);
33 // console.log('就往转账', oldNetData, typeof oldNetData);
34 // if (!oldNetData || !oldNetData.success) {
35 // ctx.helper.err({
36 // ctx,
37 // data: oldNetData,
38 // });
39 // return;
40 // }
41 const some = accountData.RECORDS.some(item => {
42 if (item.address === body.senderAddress) {
43 return body.amount * 10 ** 8 == item.balance;
44 }
45 });
46 // 加上手续费
47 body.amount = (body.amount + 0.1) * 10 ** 8;
48 if (!some) {
49 ctx.helper.err({
50 ctx,
51 err: '金额不正确,请联系社区工作人员',
52 });
53 return;
54 }
55 const result = await ctx.model.Transfer.create({ toAddress: body.receiverAddress, amount: body.amount, transaction: body.transaction, address: body.senderAddress });
56 ctx.helper.success({
57 ctx,
58 data: result,
59 });
60
61 // const transaction = await nodeSdk.transaction.createTransaction(body.receiverAccount, body.amount, body.remark || '网络升级转账', config.naclSecret);
62 // const trs = JSON.stringify({ transaction });
63 // // 上链操作
64 // const data = await this.service.sdk.pushTransaction(trs);
65 // if (data.success) {
66 // ctx.helper.success({
67 // ctx,
68 // data,
69 // });
70 // ctx.model.Transfer.update({ id: result.id }, { finish: true });
71 // } else {
72 // ctx.helper.err({
73 // ctx,
74 // data,
75 // });
76 // ctx.model.Transfer.update({ id: result.id }, { finish: false });
77 // }
78
79 }
80 // async haveBalance() {
81 // const { ctx, config } = this;
82 // const query = ctx.request.query;
83 // const rule = {
84 // address: { type: 'string' },
85 // };
86 // console.log(query);
87 // try {
88 // ctx.validate(rule, query);
89 // } catch (error) {
90 // console.log(error);
91 // ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
92 // return;
93 // }
94 // const data = await this.service.sdk.getAccount(query.address, config.ed25519Url);
95 // ctx.helper.success({
96 // ctx,
97 // data,
98 // });
99 // }
100 async getBalance() {
101 const { ctx, config } = this;
102 const query = ctx.request.query;
103 let balance = 0;
104 let total = 0
105 const rule = {
106 address: { type: 'string' },
107 };
108 try {
109 ctx.validate(rule, query);
110 } catch (error) {
111 console.log(error);
112 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
113 return;
114 }
115 const transferCount = await this.ctx.model.Transfer.count({ address: query.address });
116 if (transferCount === 0) {
117 balance = 0;
118 } else {
119 accountData.RECORDS.some(item => {
120 if (item.address === query.address && item.balance > 0) {
121 balance = item.balance / (10 ** 8);
122 return true;
123 }
124 if (item.balance > 0) {
125 total += Number(item.balance)
126 }
127 });
128 }
129 console.log(total / (10 ** 8))
130 ctx.helper.success({
131 ctx,
132 data: {
133 account: { balance }
134 },
135 });
136
137
138 }
139 async getTransaction() {
140 const { ctx, config } = this;
141 const query = ctx.request.query;
142 const rule = {
143 address: { type: 'string' },
144 };
145 console.log(query);
146 try {
147 ctx.validate(rule, query);
148 } catch (error) {
149 console.log(error);
150 ctx.helper.err({ ctx, err: ctx.helper.errorReadable(error) });
151 return;
152 }
153 const data = await this.service.sdk.getAccount(query.address, config.naclUrl);
154 ctx.helper.success({
155 ctx,
156 data,
157 });
158 }
159 // async asyncAccount() {
160 // const { ctx, config } = this;
161 // const limit = 100;
162 // let asyncCount = 0;
163 // let errAsyncAccount = 0;
164 // let balanceIsZero = 0;
165 // const data = await this.service.sdk.getAccountCount(config.ed25519Url);
166 // if (data.success) {
167 // const length = Math.ceil(data.count / 100);
168 // console.log(data.count)
169 // for (let index = 0; index < length; index++) {
170 // const acountList = await this.service.sdk.getAccountList(index + 1, limit, config.ed25519Url);
171 // console.log(acountList)
172 // if (acountList.success) {
173 // for (let index = 0; index < acountList.accounts.length; index++) {
174 // const element = acountList.accounts[index];
175 // if (element.balance == 0) {
176 // balanceIsZero++
177 // continue;
178 // }
179 // const result = await this.app.model.Accounts.create({ balance: element.balance, address: element.address, publicKey: element.publicKey });
180 // if (!result) {
181 // errAsyncAccount++;
182 // this.ctx.logger.error(`存入账户数据出错 数据:${JSON.stringify(element)},地址:${config.ed25519Url},error: ${JSON.stringify(result)}`);
183 // } else {
184 // asyncCount++;
185 // }
186 // }
187 // } else {
188 // this.ctx.logger.error(`同步账户数据出错:页码:${index + 1},每页条数:${limit},地址:${config.ed25519Url}`);
189 // }
190 // }
191 // } else {
192 // this.ctx.logger.error(`同步账户数据请求账户数量出错:${config.ed25519Url}`);
193 // }
194 // ctx.helper.success({
195 // ctx,
196 // data: { 出错数量: errAsyncAccount, 同步数量: data.count, 已同步数量: asyncCount, 余额为零数量: balanceIsZero },
197 // });
198 // }
199 }
200
201 module.exports = TransferController;
1 'use strict';
2 const moment = require('moment');
3
4 module.exports = {
5
6 /**
7 * 对象拼接为字符串
8 * @param {object} object 待拼接的hash对象
9 */
10 concatHash(object) {
11 const keys = Object.keys(object).sort();
12 let paramsStr = '';
13 for (let index = 0; index < keys.length; index++) {
14 const key = keys[index];
15 let value = object[key];
16 // object参数类型转换为string
17 if (typeof (value) === 'object') {
18 value = JSON.stringify(value);
19 }
20 paramsStr = paramsStr + `${key}${value}`;
21 }
22 return paramsStr;
23 },
24 err({ ctx, code, err, status }) {
25 ctx.body = {
26 success: false,
27 code: code || 1,
28 err,
29 };
30 ctx.status = status || 200;
31 },
32
33 // 优化错误信息可读性
34 errorReadable(e) {
35 switch (e.code) {
36 case 'invalid_param': { // 参数错误
37 const errorHash = {
38 missing_field: '缺失',
39 };
40 let msg = '';
41 for (let i = 0; i < e.errors.length; i++) {
42 msg += '参数' + e.errors[i].field + ' ' + (errorHash[e.errors[i].code] || e.errors[i].message) + ';';
43 }
44 return msg;
45 }
46 default:
47 console.log('未优化类型错误!');
48 return JSON.stringify(e);
49 }
50 },
51
52 success({ ctx, data, status }) {
53 ctx.body = {
54 success: true,
55 code: 0,
56 data,
57 };
58 ctx.status = status || 200;
59 },
60
61 async verifySign(ctx) {
62 const authorization = ctx.request.headers.authorization;
63 const timestamp = ctx.request.headers.timestamp;
64 const publicKey = ctx.request.headers.publickey;
65 if (!timestamp) {
66 this.err({
67 ctx,
68 err: 'hearder: timestamp不能为空',
69 });
70 return false;
71 }
72 if (!authorization) {
73 this.err({
74 ctx,
75 err: 'hearder: authorization不能为空',
76 });
77 return false;
78 }
79 if (!publicKey) {
80 this.err({
81 ctx,
82 err: 'hearder: publicKey不能为空',
83 });
84 return false;
85 }
86 // 验证时间不大于5秒
87 const time = moment().valueOf();
88 if (Math.abs(Number(timestamp) - time) > (ctx.app.config.timeEquation || 5000)) {
89 ctx.body = {
90 code: 1,
91 err: '签名超时',
92 };
93 return false;
94 }
95
96 // 验证签名
97 // 1. 获取请求者公钥
98 const platformKey = await ctx.model.PlatformKey.findOne({ where: { publicKey } });
99 if (!platformKey) {
100 ctx.body = {
101 code: 1,
102 err: 'publicKey不存在',
103 };
104 return false;
105 }
106 if (!platformKey.usable) {
107 ctx.body = {
108 code: 1,
109 err: '密钥对不可用',
110 };
111 return;
112 }
113
114 // 2. 对签名进行验证
115
116 // 2.1 合并参数并排序组合拼接
117 const allParams = { ...ctx.params, ...ctx.request.query, ...ctx.request.body, timestamp };
118 const paramsStr = this.concatHash(allParams);
119
120 const verify = this.service.sdk.gm.sm2VerifySign(paramsStr, authorization, publicKey);
121 // 3. 比对时间戳
122 if (!verify) {
123 ctx.body = {
124 code: 1,
125 err: '签名无效',
126 };
127 return false;
128 }
129 // 记录当前媒体平台
130 ctx.platformId = platformKey.platformId;
131 return true;
132 },
133
134 };
1 'use strict';
2
3 module.exports = app => {
4 const { STRING, DATE, UUID, UUIDV1, TEXT, BOOLEAN } = app.Sequelize;
5
6 const Account = app.model.define('account', {
7 id: {
8 allowNull: false,
9 primaryKey: true,
10 type: UUID,
11 defaultValue: UUIDV1,
12 },
13 address: { type: STRING, unique: true },
14 balance: { type: STRING },
15 publicKey: { type: STRING, unique: true },
16 created_at: { type: DATE },
17 updated_at: { type: DATE },
18 });
19 return Account;
20 };
1 'use strict';
2
3 module.exports = app => {
4 const { STRING, DATE, UUID, UUIDV1, TEXT, BOOLEAN } = app.Sequelize;
5
6 const Transfer = app.model.define('transfer', {
7 id: {
8 allowNull: false,
9 primaryKey: true,
10 type: UUID,
11 defaultValue: UUIDV1,
12 },
13 address: { type: STRING, comment: '地址' },
14 // sendTrsId: { type: STRING, comment: '发送交易成功的id' },
15 // toTrsId: { type: STRING, comment: '接收成功的交易id' },
16 toAddress: { type: STRING, comment: '接收地址' },
17 amount: { type: STRING, comment: '金额' },
18 transaction: { type: TEXT, comment: '交易体' },
19 finish: { type: BOOLEAN, comment: '是否完成', defaultValue: false },
20 createdAt: { type: DATE },
21 updatedAt: { type: DATE },
22 });
23 return Transfer;
24 };
1 'use strict';
2
3 /**
4 * @param {Egg.Application} app - egg application
5 */
6 module.exports = app => {
7 const { router, controller } = app;
8 router.post('/upgrade/transfer/postTransaction', controller.transfer.postTransaction);
9 router.get('/upgrade/transfer/getBanlance', controller.transfer.getBalance);
10 // router.get('/upgrade/transfer/haveBalance', controller.transfer.haveBalance);
11 router.get('/upgrade/transfer/transaction', controller.transfer.getTransaction);
12 // router.get('/upgrade/transfer/async', controller.transfer.asyncAccount);
13 };
1 // eslint-disable-next-line strict
2 const Subscription = require('egg').Subscription;
3 const nodeSdk = require('@ddn/node-sdk').default;
4 class Transfer extends Subscription {
5 static get schedule() {
6 return {
7 interval: '10s',
8 type: 'worker',
9 };
10 }
11
12 async subscribe() {
13 const { ctx, config } = this;
14 const trsData = await ctx.model.Transfer.findAll({ where: { finish: false } });
15 console.log(trsData)
16 trsData.map(async item => {
17 // if (item.id != "e2475660-9942-11ec-b873-171b64126906") {
18 // return
19 // }
20 const transaction = await nodeSdk.transaction.createTransaction(item.toAddress, item.amount, item.remark || '网络升级转账', config.naclSecret);
21 const trs = JSON.stringify({ transaction });
22 // 上链操作
23 const data = await this.service.sdk.pushTransaction(trs);
24 console.log('向新网地址转账结果',data)
25 if (data.success) {
26 ctx.model.Transfer.update({ finish: true }, { where: { id: item.id } });
27 } else {
28 ctx.model.Transfer.update({ finish: false }, { where: { id: item.id } });
29 }
30 });
31 }
32 }
33
34 module.exports = Transfer;
1 'use strict';
2
3 const Service = require('egg').Service;
4 const request = require('request');
5 const net = {
6 oldNet: '',
7 newNet: ''
8 }
9 class DdnService extends Service {
10
11 async getPeers() {
12 const { config } = this;
13 const peer_host = await this.getPeerHost();
14 const url = `${peer_host}/api/peers`;
15 const result = await new Promise(resolve => {
16 request({
17 url,
18 method: 'GET',
19 headers: {
20 'Content-Type': 'application/json',
21 version: '',
22 nethash: config.nethash,
23 },
24 }, (error, response, body) => {
25 resolve(JSON.parse(body));
26 });
27 });
28 return result;
29 }
30 // 区块高度
31 async getHeight() {
32 const { config } = this;
33 const peer_host = await this.getPeerHost();
34 const url = `${peer_host}/api/blocks/getHeight`;
35 const result = await new Promise(resolve => {
36 request({
37 url,
38 method: 'GET',
39 headers: {
40 'Content-Type': 'application/json',
41 version: '',
42 nethash: config.nethash,
43 },
44 }, (error, response, body) => {
45 resolve(JSON.parse(body));
46 });
47 });
48 return result;
49 }
50 // 交易详情
51 async getTransaction(id) {
52 const { config } = this;
53 const peer_host = await this.getPeerHost();
54 const url = `${peer_host}/api/transactions/get?id=${id}`;
55 const result = await new Promise(resolve => {
56 request({
57 url,
58 method: 'GET',
59 headers: {
60 'Content-Type': 'application/json',
61 version: '',
62 nethash: config.nethash,
63 },
64 }, (error, response, body) => {
65 resolve(JSON.parse(body));
66 });
67 });
68 return result;
69 }
70 // 交易列表
71 async getTransactions(con, page = 1, per_page = 10) {
72 const { config } = this;
73 const peer_host = await this.getPeerHost();
74 const conditions = [];
75 if (con.sender_id) {
76 conditions.push('senderId=' + con.sender_id);
77 }
78 if (con.recipient_id) {
79 conditions.push('recipientId=' + con.recipient_id);
80 }
81 conditions.push('limit=' + Number(per_page));
82 conditions.push('offset=' + (Number(page) - 1) * Number(per_page));
83 const url = `${peer_host}/api/transactions?${conditions.join('&')}`;
84 const result = await new Promise(resolve => {
85 request({
86 url,
87 method: 'GET',
88 headers: {
89 'Content-Type': 'application/json',
90 version: '',
91 nethash: config.nethash,
92 },
93 }, (error, response, body) => {
94 resolve(JSON.parse(body));
95 });
96 });
97 return result;
98 }
99
100 // 账户信息
101 async getAccount(address, peer_url) {
102 const { config } = this;
103 const peer_host = await this.getPeerHost();
104 const url = peer_url ? `${peer_url}/api/accounts?address=${address}` : `${peer_host}/api/accounts?address=${address}`;
105 const result = await new Promise((resolve, reject) => {
106 request({
107 url,
108 method: 'GET',
109 headers: {
110 'Content-Type': 'application/json',
111 version: '',
112 nethash: config.nethash,
113 },
114 }, (error, response, body) => {
115 if (error) {
116 reject(error);
117 }
118 console.log(url, body)
119 resolve(JSON.parse(body));
120 });
121 });
122 return result;
123 }
124
125 // 账户信息列表
126 async getAccountList(page, per_page, peer_url) {
127 const { config } = this;
128 const peer_host = await this.getPeerHost();
129 const offect = per_page * page
130 const url = peer_url ? `${peer_url}/api/accounts/top?offect=${offect}&limit=${per_page}` : `${peer_host}/api/accounts/top?page=${page}&per_page=${per_page}`;
131 const result = await new Promise((resolve, reject) => {
132 request({
133 url,
134 method: 'GET',
135 headers: {
136 'Content-Type': 'application/json',
137 version: '',
138 nethash: config.nethash,
139 },
140 }, (error, response, body) => {
141 if (error) {
142 reject(error);
143 }
144 resolve(JSON.parse(body));
145 });
146 });
147 return result;
148 }
149 // 账户信息列表
150 async getAccountCount(peer_url) {
151 const { config } = this;
152 const peer_host = await this.getPeerHost();
153 const url = peer_url ? `${peer_url}/api/accounts/count` : `${peer_host}/api/accounts/count`;
154 const result = await new Promise((resolve, reject) => {
155 request({
156 url,
157 method: 'GET',
158 headers: {
159 'Content-Type': 'application/json',
160 version: '',
161 nethash: config.nethash,
162 },
163 }, (error, response, body) => {
164 if (error) {
165 reject(error);
166 }
167 resolve(JSON.parse(body));
168 });
169 });
170 return result;
171 }
172
173 // 交易上链
174 async pushTransaction(trs, peer_url) {
175 console.log(trs)
176 const { config } = this;
177 const peer_host = await this.getPeerHost();
178 const url = peer_url ? `${peer_url}/peer/transactions` : `${peer_host}/api/transactions`;
179 let ddn_result = await new Promise(resolve => {
180 request({
181 url,
182 method: 'POST',
183 headers: {
184 'Content-Type': 'application/json',
185 version: '',
186 nethash: config.ed25519NetHash,
187 },
188 body: trs,
189 }, (error, response, body) => {
190 console.log(error, 'body', body)
191 resolve(JSON.parse(body));
192 });
193 });
194 console.log('ddn_result', ddn_result)
195 // ddn_result = JSON.parse(ddn_result);
196 return ddn_result;// ddn_result;
197 }
198
199 // // 获取结点地址
200 async getPeerHost() {
201 // const peer_host = await this.service.sdk.redis.get('peer_host');
202 // if (!peer_host) {
203 const res = await this.checkPeerHost();
204 return res;
205 // }
206 // return peer_host.host;
207 }
208
209 // 检查结点并指定可用结点
210 async checkPeerHost() {
211 const { config } = this;
212 // const peers = this.config.peer_list;
213 // const peers = [ 'http://120.24.69.99:8003' ];
214 const peers = ['http://localhost:8001'];
215 const peers_height = {};
216 let max_height = null;
217 let min_height = null;
218 let best_peer = null;
219 for (let i = 0; i < peers.length; i++) {
220 const url = `${peers[i]}/api/blocks/getstatus`;
221 const result = await new Promise(resolve => {
222 request({
223 url,
224 method: 'GET',
225 headers: {
226 'Content-Type': 'application/json',
227 version: '',
228 nethash: config.nethash,
229 },
230 }, (error, response, body) => {
231 if (error) {
232 resolve({ success: false });
233 } else {
234 resolve(JSON.parse(body));
235 }
236 });
237 });
238 if (result.success) {
239 peers_height[peers[i]] = result.height;
240 if (!max_height || Number(result.height) > Number(max_height)) {
241 max_height = result.height;
242 }
243 if (!min_height || Number(result.height) < Number(min_height)) {
244 min_height = result.height;
245 }
246 if (!best_peer || Number(result.height) === Number(max_height)) {
247 best_peer = peers[i];
248 }
249 } else {
250 }
251 }
252
253 return best_peer;
254 }
255
256
257 }
258
259 module.exports = DdnService;
1 environment:
2 matrix:
3 - nodejs_version: '10'
4
5 install:
6 - ps: Install-Product node $env:nodejs_version
7 - npm i npminstall && node_modules\.bin\npminstall
8
9 test_script:
10 - node --version
11 - npm --version
12 - npm run test
13
14 build: off
1 /* eslint valid-jsdoc: "off" */
2
3 'use strict';
4
5 /**
6 * @param {Egg.EggAppInfo} appInfo app info
7 */
8 module.exports = appInfo => {
9 /**
10 * built-in config
11 * @type {Egg.EggAppConfig}
12 **/
13 const config = exports = {};
14
15 config.cluster = {
16 listen: {
17 port: 7009,
18 },
19 };
20
21 // use for cookie sign key, should change to your own and keep security
22 config.keys = appInfo.name + '_1644800905769_2309';
23
24 // add your middleware config here
25 config.middleware = [];
26
27 config.security = {
28 csrf: {
29 enable: false,
30 ignoreJSON: true,
31 },
32 };
33
34 config.cors = {
35 origin: '*',
36 allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH',
37 };
38
39 config.sequelize = {
40 host: 'localhost',
41 port: '5432',
42 username: 'postgres',
43 password: 'wawjr1314',
44 database: 'ddn',
45 dialect: 'postgres',
46 pool: {
47 max: 5,
48 min: 0,
49 idle: 10000,
50 },
51 logging: false,
52 };
53 // config.ed25519Address = 'DFy6P2sN1KLDNtppnqLBPWcnH8GTJRouGj'; // 旧网接收钱包地址 弃用
54 config.naclSecret = 'puzzle elite rescue gun blush top floor surge injury popular pole inquiry'; // 新网转账钱包秘钥
55 // config.ed25519Url = 'http://120.24.69.99:8001'; // 旧网节点地址 弃用
56 config.naclUrl = 'http://8.142.20.158:8001'; // 新网节点地址
57 // config.naclUrl = 'http://localhost:8001'; // 测试地址
58 config.ed25519NetHash = 'b11fa2f2';// 旧网nethash
59 config.naclNetHash = '0ab796cd'; // 新网nethash
60 // add your user config here
61 const userConfig = {
62 // myAppName: 'egg',
63 };
64
65 return {
66 ...config,
67 ...userConfig,
68 };
69 };
1 /* eslint valid-jsdoc: "off" */
2
3 'use strict';
4
5 /**
6 * @param {Egg.EggAppInfo} appInfo app info
7 */
8 module.exports = appInfo => {
9 const config = exports = {};
10 // config.ed25519Address = 'DFy6P2sN1KLDNtppnqLBPWcnH8GTJRouGj'; // 旧网接收钱包地址
11 // config.naclAddress = 'D3EFYncByWwzsSQvRsVbufBfmVstuf11QW'; // 新网转账钱包地址
12 config.naclSecret = 'puzzle elite rescue gun blush top floor surge injury popular pole inquiry'; // 新网转账钱包秘钥
13 // config.ed25519Url = 'http://120.24.69.99:8001'; // 旧网节点地址
14 config.naclUrl = 'http://8.142.20.158:8001'; // 新网节点地址
15 // config.naclUrl = 'http://localhost:8001'; // 测试地址
16 config.ed25519NetHash = 'b11fa2f2';// 旧网nethash
17 config.naclNetHash = '0ab796cd'; // 新网nethash
18 // add your user config here
19 const userConfig = {
20 // myAppName: 'egg',
21 };
22
23 return {
24 ...config,
25 ...userConfig,
26 };
27 };
1 /**
2 * Please use yourself constants file
3 * Note: Once the mainnet is online, this file can no longer be changed.
4 */
5 module.exports = {
6 nethash: '0ab796cd', // 标定该链的版本
7 };
This diff could not be displayed because it is too large.
1 'use strict';
2
3 /** @type Egg.EggPlugin */
4 module.exports = {
5 // had enabled by egg
6 // static: {
7 // enable: true,
8 // }
9 cors: {
10 enable: true,
11 package: 'egg-cors',
12 },
13 sequelize: {
14 enable: true,
15 package: 'egg-sequelize',
16 },
17 validate: {
18 enable: true,
19 package: 'egg-validate',
20 },
21 };
1 {
2 "development": {
3 "username": "postgres",
4 "password": "wawjr1314",
5 "database": "ddn",
6 "host": "localhost",
7 "dialect": "postgres"
8
9 },
10 "test": {
11 "username": "root",
12 "password": null,
13 "database": "database_test",
14 "host": "127.0.0.1",
15 "dialect": "mysql"
16 },
17 "production": {
18 "username": "root",
19 "password": null,
20 "database": "database_production",
21 "host": "127.0.0.1",
22 "dialect": "mysql"
23 }
24 }
1 'use strict';
2
3 module.exports = {
4 async up(queryInterface, Sequelize) {
5 const { UUID, DATE, STRING, TEXT, BOOLEAN, UUIDV1 } = Sequelize;
6 await queryInterface.createTable('transfers', {
7 id: {
8 allowNull: false,
9 primaryKey: true,
10 type: UUID,
11 defaultValue: UUIDV1,
12 },
13 address: { type: STRING },
14 to_address: { type: STRING },
15 amount: { type: STRING },
16 transaction: { type: TEXT },
17 finish: { type: BOOLEAN },
18 created_at: { type: DATE },
19 updated_at: { type: DATE },
20 });
21 },
22
23 async down(queryInterface, Sequelize) {
24 /**
25 * Add reverting commands here.
26 *
27 * Example:
28 * await queryInterface.dropTable('users');
29 */
30 await queryInterface.dropTable('transfer');
31 }
32 };
1 'use strict';
2
3 module.exports = {
4 async up (queryInterface, Sequelize) {
5 const { UUID, DATE, STRING, TEXT, BOOLEAN, UUIDV1 } = Sequelize;
6 await queryInterface.createTable('accounts', {
7 id: {
8 allowNull: false,
9 primaryKey: true,
10 type: UUID,
11 defaultValue: UUIDV1,
12 },
13 address: { type: STRING, unique: true },
14 balance: { type: STRING },
15 publicKey: { type: STRING, unique: true },
16 created_at: { type: DATE },
17 updated_at: { type: DATE },
18 });
19 },
20
21 async down (queryInterface, Sequelize) {
22 /**
23 * Add reverting commands here.
24 *
25 * Example:
26 * await queryInterface.dropTable('users');
27 */
28 await queryInterface.dropTable('accounts');
29 }
30 };
No preview for this file type
1 {
2 "include": [
3 "**/*"
4 ]
5 }
...\ No newline at end of file ...\ No newline at end of file
1 {
2 "name": "DDNTrs",
3 "version": "1.0.0",
4 "description": "DDN upgrade server",
5 "private": true,
6 "egg": {
7 "declarations": true
8 },
9 "dependencies": {
10 "@ddn/node-sdk": "^2.2.0",
11 "egg": "^2.15.1",
12 "egg-cors": "^2.2.3",
13 "egg-scripts": "^2.11.0",
14 "egg-sequelize": "^6.0.0",
15 "egg-validate": "^2.0.2",
16 "pg": "^8.7.3"
17 },
18 "devDependencies": {
19 "autod": "^3.0.1",
20 "autod-egg": "^1.1.0",
21 "egg-bin": "^4.11.0",
22 "egg-ci": "^1.11.0",
23 "egg-mock": "^3.21.0",
24 "eslint": "^5.13.0",
25 "eslint-config-egg": "^7.1.0",
26 "sequelize-cli": "^6.4.1"
27 },
28 "engines": {
29 "node": ">=10.0.0"
30 },
31 "scripts": {
32 "start": "egg-scripts start --daemon --title=egg-server-DDNTrs",
33 "stop": "egg-scripts stop --title=egg-server-DDNTrs",
34 "dev": "egg-bin dev",
35 "debug": "egg-bin debug",
36 "test": "npm run lint -- --fix && npm run test-local",
37 "test-local": "egg-bin test",
38 "cov": "egg-bin cov",
39 "lint": "eslint .",
40 "ci": "npm run lint && npm run cov",
41 "autod": "autod",
42 "init":"npx sequelize db:migrate"
43 },
44 "ci": {
45 "version": "10"
46 },
47 "repository": {
48 "type": "git",
49 "url": ""
50 },
51 "author": "creazy",
52 "license": "MIT"
53 }
1 'use strict';
2
3 const { app, assert } = require('egg-mock/bootstrap');
4
5 describe('test/app/controller/home.test.js', () => {
6 it('should assert', () => {
7 const pkg = require('../../../package.json');
8 assert(app.config.keys.startsWith(pkg.name));
9
10 // const ctx = app.mockContext({});
11 // yield ctx.service.xx();
12 });
13
14 it('should GET /', () => {
15 return app.httpRequest()
16 .get('/')
17 .expect('hi, egg')
18 .expect(200);
19 });
20 });