It is a project for Bytom Chrome extension JS SDK https://bytom.github.io/Bytom-JS-SDK
Revision | 1bd6b83f4e2ada5ba48a2efef8f77def8f3ada1b (tree) |
---|---|
Zeit | 2020-06-15 14:42:01 |
Autor | Zhiting Lin <zlin035@uott...> |
Commiter | Zhiting Lin |
update to v3
@@ -17,7 +17,7 @@ function accountsApi(http) { | ||
17 | 17 | * @returns {Promise} Guid, address, label |
18 | 18 | */ |
19 | 19 | accountsApi.prototype.create = function(params) { |
20 | - return this.http.request('account/create-account', params); | |
20 | + return this.http.request('account/create', params); | |
21 | 21 | }; |
22 | 22 | |
23 | 23 | /** |
@@ -1,5 +1,6 @@ | ||
1 | 1 | import axios from 'axios'; |
2 | 2 | import {handleApiError, handleAxiosError} from './utils/http'; |
3 | +import { camelize } from "./utils/utils"; | |
3 | 4 | |
4 | 5 | // const basePath = 'api/v1/btm'; |
5 | 6 |
@@ -22,7 +23,7 @@ export function serverHttp(host) { | ||
22 | 23 | if (resp.status !== 200 || resp.data.code !== 200) { |
23 | 24 | throw handleApiError(resp); |
24 | 25 | } else if (resp.data.code === 200) { |
25 | - return resp.data.result.data; | |
26 | + return camelize(resp.data.data); | |
26 | 27 | } |
27 | 28 | return resp.data; |
28 | 29 | }).catch(error=>{ |
@@ -50,7 +51,7 @@ export function http(baseUrl) { | ||
50 | 51 | if (resp.status !== 200 || resp.data.code !== 200) { |
51 | 52 | throw handleApiError(resp); |
52 | 53 | } else if (resp.data.code === 200) { |
53 | - return resp.data.result.data; | |
54 | + return camelize(resp.data.data); | |
54 | 55 | } |
55 | 56 | return resp.data; |
56 | 57 | }).catch(error=>{ |
@@ -50,9 +50,9 @@ accountsSDK.prototype.listAccountUseServer = function() { | ||
50 | 50 | * @param {String} guid |
51 | 51 | * @returns {Promise} list of all addresses |
52 | 52 | */ |
53 | -accountsSDK.prototype.listAddressUseServer = function(guid) { | |
53 | +accountsSDK.prototype.listAddressUseServer = function(address) { | |
54 | 54 | let net = this.bytom.net; |
55 | - return this.http.request('account/list-addresses', {guid:guid}, net); | |
55 | + return this.http.request(`account/address?address=${address}`, '', net, 'GET'); | |
56 | 56 | }; |
57 | 57 | /** |
58 | 58 | * Create a new address for a wallet. |
@@ -220,7 +220,7 @@ accountsSDK.prototype.listVaporAccountUseServer = function(guid) { | ||
220 | 220 | let net = this.bytom.net; |
221 | 221 | let that = this; |
222 | 222 | let retPromise = new Promise((resolve, reject) => { |
223 | - that.http.request('account/list-addresses', {guid:guid}, net).then(resp => { | |
223 | + that.http.request('account/addresses', {address:address}, net).then(resp => { | |
224 | 224 | getDB().then(db => { |
225 | 225 | let objectStore = db.transaction(['accounts-server'], 'readwrite').objectStore('accounts-server'); |
226 | 226 | let index = objectStore.index('guid'); |
@@ -198,7 +198,7 @@ keysSDK.prototype.signMessage = function(message, password, address) { | ||
198 | 198 | .getAll() |
199 | 199 | |
200 | 200 | getRequest.onsuccess = function (e) { |
201 | - const result = getRequest.result.filter(obj => obj.address === address) | |
201 | + const result = getRequest.result.filter(obj => (obj.address === address || obj.vpAddress === address)) | |
202 | 202 | if (result.length === 0) { |
203 | 203 | reject(new Error('not found address')); |
204 | 204 | return; |
@@ -34,7 +34,7 @@ querySDK.prototype.listUtxo = function(object) { | ||
34 | 34 | */ |
35 | 35 | querySDK.prototype.getVoteStatus = function() { |
36 | 36 | let net = this.bytom.net; |
37 | - return this.http.request('q/get-vote-status',null, net, 'GET'); | |
37 | + return this.http.request('q/vote-status',null, net, 'GET'); | |
38 | 38 | }; |
39 | 39 | |
40 | 40 | export default querySDK; |
\ No newline at end of file |
@@ -19,9 +19,9 @@ function transactionSDK(bytom) { | ||
19 | 19 | * @param {Number} limit page limit |
20 | 20 | * @returns {Promise} |
21 | 21 | */ |
22 | -transactionSDK.prototype.list = function(guid, filter,sort, start, limit) { | |
22 | +transactionSDK.prototype.list = function(address, filter,sort, start, limit) { | |
23 | 23 | let net = this.bytom.net; |
24 | - let pm = {guid: guid}; | |
24 | + let pm = {} | |
25 | 25 | |
26 | 26 | if (filter) { |
27 | 27 | pm.filter = filter; |
@@ -31,7 +31,7 @@ transactionSDK.prototype.list = function(guid, filter,sort, start, limit) { | ||
31 | 31 | pm.sort = sort; |
32 | 32 | } |
33 | 33 | |
34 | - let url = 'merchant/list-transactions'; | |
34 | + let url = 'merchant/transactions'; | |
35 | 35 | let args = new URLSearchParams(); |
36 | 36 | if (typeof start !== 'undefined') { |
37 | 37 | args.append('start', start); |
@@ -39,6 +39,9 @@ transactionSDK.prototype.list = function(guid, filter,sort, start, limit) { | ||
39 | 39 | if (limit) { |
40 | 40 | args.append('limit', limit); |
41 | 41 | } |
42 | + if (address) { | |
43 | + args.append('address', address); | |
44 | + } | |
42 | 45 | url = url + '?' + args.toString(); |
43 | 46 | return this.http.request(url, pm, net); |
44 | 47 | }; |
@@ -51,10 +54,10 @@ transactionSDK.prototype.list = function(guid, filter,sort, start, limit) { | ||
51 | 54 | * @param {String} raw_transaction raw transaction bytes encoded to string |
52 | 55 | * @param {Array} signatures signed data of each signing instruction |
53 | 56 | */ |
54 | -transactionSDK.prototype.submitPayment = function(guid, raw_transaction, signatures) { | |
57 | +transactionSDK.prototype.submitPayment = function(address, raw_transaction, signatures) { | |
55 | 58 | let net = this.bytom.net; |
56 | - let pm = {guid: guid, raw_transaction: raw_transaction, signatures: signatures}; | |
57 | - return this.http.request('merchant/submit-payment', pm, net); | |
59 | + let pm = {raw_transaction: raw_transaction, signatures: signatures}; | |
60 | + return this.http.request(`merchant/submit-payment?address=${address}`, pm, net); | |
58 | 61 | }; |
59 | 62 | |
60 | 63 | /** |
@@ -70,10 +73,9 @@ transactionSDK.prototype.submitPayment = function(guid, raw_transaction, signatu | ||
70 | 73 | * @param {Number} confirmations - transaction confirmations |
71 | 74 | * @returns {Promise} |
72 | 75 | */ |
73 | -transactionSDK.prototype.buildPayment = function(guid, to, asset, amount, fee, confirmations, memo, forbidChainTx) { | |
76 | +transactionSDK.prototype.buildPayment = function(address, to, asset, amount, fee, confirmations, memo, forbidChainTx) { | |
74 | 77 | let net = this.bytom.net; |
75 | 78 | let pm = { |
76 | - guid: guid, | |
77 | 79 | asset: asset, |
78 | 80 | recipients: {}, |
79 | 81 | forbid_chain_tx: false |
@@ -93,7 +95,7 @@ transactionSDK.prototype.buildPayment = function(guid, to, asset, amount, fee, c | ||
93 | 95 | if (confirmations) { |
94 | 96 | pm.confirmations = confirmations; |
95 | 97 | } |
96 | - return this.http.request('merchant/build-payment', pm, net); | |
98 | + return this.http.request(`merchant/build-payment?address=${address}`, pm, net); | |
97 | 99 | }; |
98 | 100 | |
99 | 101 | /** |
@@ -106,11 +108,10 @@ transactionSDK.prototype.buildPayment = function(guid, to, asset, amount, fee, c | ||
106 | 108 | * @param {Number} confirmations - transaction confirmations |
107 | 109 | * @returns {Promise} |
108 | 110 | */ |
109 | -transactionSDK.prototype.buildCrossChain = function(guid, to, asset, amount, confirmations, forbidChainTx) { | |
111 | +transactionSDK.prototype.buildCrossChain = function(address, to, asset, amount, confirmations, forbidChainTx) { | |
110 | 112 | let net = this.bytom.net; |
111 | 113 | |
112 | 114 | let pm = { |
113 | - guid: guid, | |
114 | 115 | asset: asset, |
115 | 116 | recipients: {}, |
116 | 117 | forbid_chain_tx: false |
@@ -122,7 +123,7 @@ transactionSDK.prototype.buildCrossChain = function(guid, to, asset, amount, con | ||
122 | 123 | pm.forbid_chain_tx = forbidChainTx; |
123 | 124 | } |
124 | 125 | |
125 | - return this.http.request('merchant/build-crosschain', pm, net); | |
126 | + return this.http.request(`merchant/build-crosschain?address=${address}`, pm, net); | |
126 | 127 | }; |
127 | 128 | |
128 | 129 | /** |
@@ -135,9 +136,9 @@ transactionSDK.prototype.buildCrossChain = function(guid, to, asset, amount, con | ||
135 | 136 | * @param {Number} confirmations - transaction confirmations |
136 | 137 | * @returns {Promise} |
137 | 138 | */ |
138 | -transactionSDK.prototype.buildVote = function(guid, vote, amount, confirmations, memo, forbidChainTx) { | |
139 | +transactionSDK.prototype.buildVote = function(address, vote, amount, confirmations, memo, forbidChainTx) { | |
139 | 140 | let net = this.bytom.net; |
140 | - let pm = {guid, vote, amount: amount, forbid_chain_tx: false}; | |
141 | + let pm = {vote, amount: amount, forbid_chain_tx: false}; | |
141 | 142 | if (confirmations) { |
142 | 143 | pm.confirmations = confirmations; |
143 | 144 | } |
@@ -148,7 +149,7 @@ transactionSDK.prototype.buildVote = function(guid, vote, amount, confirmations, | ||
148 | 149 | pm.forbid_chain_tx = forbidChainTx; |
149 | 150 | } |
150 | 151 | |
151 | - return this.http.request('merchant/build-vote', pm, net); | |
152 | + return this.http.request(`merchant/build-vote?address=${address}`, pm, net); | |
152 | 153 | }; |
153 | 154 | |
154 | 155 | /** |
@@ -161,9 +162,9 @@ transactionSDK.prototype.buildVote = function(guid, vote, amount, confirmations, | ||
161 | 162 | * @param {Number} confirmations - transaction confirmations |
162 | 163 | * @returns {Promise} |
163 | 164 | */ |
164 | -transactionSDK.prototype.buildVeto = function(guid, vote, amount, confirmations, memo, forbidChainTx) { | |
165 | +transactionSDK.prototype.buildVeto = function(address, vote, amount, confirmations, memo, forbidChainTx) { | |
165 | 166 | let net = this.bytom.net; |
166 | - let pm = {guid, vote, amount: amount, forbid_chain_tx: false}; | |
167 | + let pm = { vote, amount: amount, forbid_chain_tx: false}; | |
167 | 168 | if (confirmations) { |
168 | 169 | pm.confirmations = confirmations; |
169 | 170 | } |
@@ -174,7 +175,7 @@ transactionSDK.prototype.buildVeto = function(guid, vote, amount, confirmations, | ||
174 | 175 | pm.forbid_chain_tx = forbidChainTx; |
175 | 176 | } |
176 | 177 | |
177 | - return this.http.request('merchant/build-veto', pm, net); | |
178 | + return this.http.request(`merchant/build-veto?address=${address}`, pm, net); | |
178 | 179 | }; |
179 | 180 | |
180 | 181 | /** |
@@ -185,12 +186,12 @@ transactionSDK.prototype.buildVeto = function(guid, vote, amount, confirmations, | ||
185 | 186 | * @param {Number} fee transaction fee amount |
186 | 187 | * @returns {Promise} |
187 | 188 | */ |
188 | -transactionSDK.prototype.buildTransaction = function(guid, inputs, outputs, fee, confirmations ) { | |
189 | +transactionSDK.prototype.buildTransaction = function(address, inputs, outputs, fee, confirmations, forbid_chain_tx = true) { | |
189 | 190 | let net = this.bytom.net; |
190 | 191 | let pm = { |
191 | - guid, | |
192 | 192 | inputs, |
193 | 193 | outputs, |
194 | + forbid_chain_tx | |
194 | 195 | }; |
195 | 196 | if (fee) { |
196 | 197 | pm.fee = fee; |
@@ -198,7 +199,7 @@ transactionSDK.prototype.buildTransaction = function(guid, inputs, outputs, fee, | ||
198 | 199 | if (confirmations) { |
199 | 200 | pm.confirmations = confirmations; |
200 | 201 | } |
201 | - return this.http.request('merchant/build-transaction', pm, net); | |
202 | + return this.http.request(`merchant/build-advanced-tx?address=${address}`, pm, net); | |
202 | 203 | }; |
203 | 204 | |
204 | 205 | /** |
@@ -0,0 +1,19 @@ | ||
1 | +export const camelize = (object) => { | |
2 | + for (let key in object) { | |
3 | + let value = object[key]; | |
4 | + let newKey = key; | |
5 | + | |
6 | + if (/_/.test(key)) { | |
7 | + newKey = key.replace(/([_][a-z])/g, v => v[1].toUpperCase()); | |
8 | + delete object[key]; | |
9 | + } | |
10 | + | |
11 | + if (typeof value == 'object') { | |
12 | + value = camelize(value); | |
13 | + } | |
14 | + | |
15 | + object[newKey] = value; | |
16 | + } | |
17 | + | |
18 | + return object; | |
19 | +}; | |
\ No newline at end of file |