openchain-nodejs-ts-yxl 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/doc/SDK_CN.md +1 -1
- package/example/atp10TokenDemo.js +1 -1
- package/example/exchange.js +1 -1
- package/example/offlineSignatureDemo.js +1 -1
- package/example/submitTransactionDemo.js +1 -1
- package/index.d.ts +370 -21
- package/index.js +2 -0
- package/lib/common/buffer-polyfill.js +90 -0
- package/package.json +2 -2
- package/test/Ctp10Token.test.js +2 -2
- package/test/account.test.js +20 -15
- package/test/accountActivateOperation.test.js +20 -18
- package/test/accountSetMetadata.test.js +2 -2
- package/test/accountSetPrivilege.test.js +2 -2
- package/test/asset.test.js +10 -10
- package/test/assetIssueOperation.test.js +7 -6
- package/test/assetSendOperation.test.js +23 -23
- package/test/blob.test.js +4 -4
- package/test/block.test.js +5 -3
- package/test/buSendOperation.test.js +6 -6
- package/test/contract.test.js +1 -1
- package/test/contractCreateOperation.test.js +3 -3
- package/test/contractCreateTransaction.test.js +1 -1
- package/test/contractInvokeByAssetOperation.test.js +2 -2
- package/test/contractInvokeByBUOperation.test.js +1 -1
- package/test/ctp10TokenApproveOperation.test.js +4 -4
- package/test/ctp10TokenAssignOperation.test.js +1 -1
- package/test/ctp10TokenChangeOwnerOperation.test.js +1 -1
- package/test/ctp10TokenIssueOperation.test.js +1 -1
- package/test/ctp10TokenIssueOperationTransaction.test.js +1 -1
- package/test/ctp10TokenTransferFromOperation.test.js +1 -1
- package/test/ctp10TokenTransferOperation.test.js +1 -1
- package/test/log.transaction.test.js +1 -1
- package/test/transaction.test.js +2 -2
- package/test/util.test.js +1 -1
package/doc/SDK_CN.md
CHANGED
package/example/exchange.js
CHANGED
package/index.d.ts
CHANGED
@@ -1,30 +1,246 @@
|
|
1
|
+
/**
|
2
|
+
* OpenChain SDK配置参数接口
|
3
|
+
*/
|
4
|
+
export interface OpenChainSDKConfig {
|
5
|
+
/** 区块链节点API地址 */
|
6
|
+
host: string;
|
7
|
+
/** 链ID */
|
8
|
+
chainID?: number;
|
9
|
+
/** 请求超时时间(ms) */
|
10
|
+
timeout?: number;
|
11
|
+
/** 是否使用HTTPS */
|
12
|
+
secure?: boolean;
|
13
|
+
}
|
14
|
+
|
15
|
+
/**
|
16
|
+
* OpenChain区块链SDK核心类
|
17
|
+
* @class
|
18
|
+
* @example
|
19
|
+
* const sdk = new OpenChainSDK({host: 'http://api.openchain.org'});
|
20
|
+
*/
|
1
21
|
declare class OpenChainSDK {
|
2
|
-
|
22
|
+
/**
|
23
|
+
* 初始化SDK实例
|
24
|
+
* @param {OpenChainSDKConfig} config 配置参数
|
25
|
+
* @example
|
26
|
+
* const sdk = new OpenChainSDK({
|
27
|
+
* host: 'http://api.openchain.org',
|
28
|
+
* chainID: 0,
|
29
|
+
* timeout: 3000,
|
30
|
+
* secure: false
|
31
|
+
* });
|
32
|
+
*/
|
33
|
+
constructor(config: OpenChainSDKConfig);
|
3
34
|
|
4
35
|
account: {
|
36
|
+
/**
|
37
|
+
* 创建区块链账户
|
38
|
+
* @returns {Promise<{errorCode:number, result:{privateKey:string, publicKey:string, address:string}}>}
|
39
|
+
* @example
|
40
|
+
* sdk.account.create().then(console.log);
|
41
|
+
*
|
42
|
+
* // 错误码:
|
43
|
+
* // - SYSTEM_ERROR 20000 系统错误
|
44
|
+
*/
|
5
45
|
create(): Promise<{ errorCode: number; result: { privateKey: string; publicKey: string; address: string } }>;
|
46
|
+
/**
|
47
|
+
* 查询账户信息
|
48
|
+
* @param {string} address 账户地址
|
49
|
+
* @returns {Promise<{errorCode:number, result:AccountInfoResult}>}
|
50
|
+
* @example
|
51
|
+
* sdk.account.getInfo('adxS...').then(console.log);
|
52
|
+
*
|
53
|
+
* // 错误码:
|
54
|
+
* // - INVALID_SOURCEADDRESS_ERROR 11002 无效地址
|
55
|
+
*/
|
6
56
|
getInfo(address: string): Promise<{ errorCode: number; result: AccountInfoResult }>;
|
57
|
+
/**
|
58
|
+
* 校验账户地址有效性
|
59
|
+
* @param {string} address 待校验的区块链地址(需符合adx开头base58编码格式)
|
60
|
+
* @returns {Promise<{errorCode:number, result:{isValid:boolean}}>}
|
61
|
+
* @example
|
62
|
+
* sdk.account.checkValid('adxSqKcX8wGCMKhzNUBoDWfbeQaMhfnGdtyG2').then(console.log);
|
63
|
+
*
|
64
|
+
* // 错误码:
|
65
|
+
* // - INVALID_ADDRESS_ERROR 11002 地址格式错误
|
66
|
+
*/
|
7
67
|
checkValid(address: string): Promise<{ errorCode: number; result: { isValid: boolean } }>;
|
68
|
+
|
69
|
+
/**
|
70
|
+
* 查询账户余额
|
71
|
+
* @param {string} address 账户地址
|
72
|
+
* @returns {Promise<{errorCode:number, result:{balance:string}}>} 余额单位MO(1 MO = 1e8 stroop)
|
73
|
+
* @example
|
74
|
+
* sdk.account.getBalance('adxS...').then(console.log);
|
75
|
+
*
|
76
|
+
* // 错误码:
|
77
|
+
* // - INVALID_ADDRESS_ERROR 11002 无效地址
|
78
|
+
* // - ACCOUNT_NOT_EXIST 11003 账户不存在
|
79
|
+
*/
|
8
80
|
getBalance(address: string): Promise<{ errorCode: number; result: { balance: string } }>;
|
81
|
+
|
82
|
+
/**
|
83
|
+
* 获取账户交易序列号
|
84
|
+
* @param {string} address 账户地址
|
85
|
+
* @returns {Promise<{errorCode:number, result:{nonce:string}}>} nonce值用于防止重放攻击
|
86
|
+
* @example
|
87
|
+
* sdk.account.getNonce('adxS...').then(console.log);
|
88
|
+
*
|
89
|
+
* // 错误码:
|
90
|
+
* // - INVALID_ADDRESS_ERROR 11002 无效地址
|
91
|
+
* // - ACCOUNT_NOT_EXIST 11003 账户不存在
|
92
|
+
*/
|
9
93
|
getNonce(address: string): Promise<{ errorCode: number; result: { nonce: string } }>;
|
94
|
+
|
95
|
+
/**
|
96
|
+
* 查询账户元数据
|
97
|
+
* @param {Object} params 查询参数
|
98
|
+
* @param {string} params.address 账户地址
|
99
|
+
* @param {string} params.key 元数据键
|
100
|
+
* @returns {Promise<{errorCode:number, result:MetadataResult}>} 包含版本号和值的元数据
|
101
|
+
* @example
|
102
|
+
* sdk.account.getMetadata({address:'adxS...', key:'kyc_info'}).then(console.log);
|
103
|
+
*
|
104
|
+
* // 错误码:
|
105
|
+
* // - METADATA_NOT_FOUND 12001 元数据不存在
|
106
|
+
* // - INVALID_ADDRESS_ERROR 11002 无效地址
|
107
|
+
*/
|
10
108
|
getMetadata(params: { address: string; key: string }): Promise<{ errorCode: number; result: MetadataResult }>;
|
109
|
+
|
110
|
+
/**
|
111
|
+
* 检查账户是否激活
|
112
|
+
* @param {string} address 待检查账户地址
|
113
|
+
* @returns {Promise<{errorCode:number, result:{isActivated:boolean}}>}
|
114
|
+
* @example
|
115
|
+
* 参见test/accountActivateOperation.test.js示例
|
116
|
+
*
|
117
|
+
* // 错误码:
|
118
|
+
* // - ACCOUNT_NOT_ACTIVATED 11004 账户未激活
|
119
|
+
*/
|
11
120
|
isActivated(address: string): Promise<{ errorCode: number; result: { isActivated: boolean } }>;
|
12
121
|
};
|
13
122
|
|
14
123
|
contract: {
|
124
|
+
/**
|
125
|
+
* 查询合约信息
|
126
|
+
* @param {string} contractAddress 合约账户地址(需以'adx'开头的base58编码格式)
|
127
|
+
* @returns {Promise<{errorCode:number, result:ContractInfoResult}>}
|
128
|
+
* @example
|
129
|
+
* sdk.contract.getInfo('adxScpCtbeLP2KGRaCkbtrmz8iB5mu6DQcW3r').then(console.log);
|
130
|
+
*
|
131
|
+
* // 错误码:
|
132
|
+
* // - INVALID_CONTRACTADDRESS_ERROR 11037 无效合约地址
|
133
|
+
* // - CONTRACTADDRESS_NOT_CONTRACTACCOUNT_ERROR 11038 非合约账户地址
|
134
|
+
* // - SYSTEM_ERROR 20000 系统错误
|
135
|
+
*/
|
15
136
|
getInfo(contractAddress: string): Promise<{ errorCode: number; result: ContractInfoResult }>;
|
137
|
+
|
138
|
+
/**
|
139
|
+
* 通过交易哈希查询合约地址
|
140
|
+
* @param {string} hash 创建合约的交易哈希(64位十六进制字符串)
|
141
|
+
* @returns {Promise<{errorCode:number, result:ContractAddressInfo[]}>}
|
142
|
+
* @example
|
143
|
+
* sdk.contract.getAddress('cc4c...').then(console.log);
|
144
|
+
*
|
145
|
+
* // 错误码:
|
146
|
+
* // - INVALID_HASH_ERROR 11025 无效交易哈希
|
147
|
+
* // - QUERY_RESULT_NOT_EXIST 12002 查询结果不存在
|
148
|
+
* // 参见test/contractCreateTransaction.test.js示例
|
149
|
+
*/
|
16
150
|
getAddress(hash: string): Promise<{ errorCode: number; result: ContractAddressInfo[] }>;
|
151
|
+
|
152
|
+
/**
|
153
|
+
* 创建智能合约
|
154
|
+
* @param {ContractCreateParams} params 合约创建参数
|
155
|
+
* @returns {Promise<{errorCode:number, result:ContractAddressInfo}>}
|
156
|
+
* @example
|
157
|
+
* sdk.contract.createContract({
|
158
|
+
* sourceAddress: 'adx...',
|
159
|
+
* initBalance: '10',
|
160
|
+
* type: 0,
|
161
|
+
* payload: 'contract code'
|
162
|
+
* }).then(console.log);
|
163
|
+
*
|
164
|
+
* // 错误码:
|
165
|
+
* // - INVALID_SOURCEADDRESS_ERROR 11002 无效源地址
|
166
|
+
* // - PAYLOAD_EMPTY_ERROR 11044 合约代码为空
|
167
|
+
*/
|
168
|
+
createContract(params: ContractCreateParams): Promise<{ errorCode: number; result: ContractAddressInfo }>;
|
169
|
+
|
170
|
+
/**
|
171
|
+
* 调用智能合约
|
172
|
+
* @param {ContractInvokeParams} params 合约调用参数
|
173
|
+
* @returns {Promise<{errorCode:number, result:ContractInvokeResult}>}
|
174
|
+
* @example
|
175
|
+
* sdk.contract.invokeContract({
|
176
|
+
* sourceAddress: 'adx...',
|
177
|
+
* contractAddress: 'adx...',
|
178
|
+
* input: 'methodName(param)'
|
179
|
+
* }).then(console.log);
|
180
|
+
*
|
181
|
+
* // 错误码:
|
182
|
+
* // - CONTRACT_EXECUTE_FAILED 12003 合约执行失败
|
183
|
+
*/
|
184
|
+
invokeContract(params: ContractInvokeParams): Promise<{ errorCode: number; result: ContractInvokeResult }>;
|
17
185
|
};
|
18
186
|
|
19
187
|
block: {
|
188
|
+
/**
|
189
|
+
* 获取当前区块高度
|
190
|
+
* @returns {Promise<{errorCode:number, result:BlockNumberResult}>} 返回当前区块高度信息
|
191
|
+
* @example
|
192
|
+
* sdk.block.getNumber().then(console.log);
|
193
|
+
*
|
194
|
+
* // 错误码:
|
195
|
+
* // - SYSTEM_ERROR 20000 系统错误
|
196
|
+
*/
|
20
197
|
getNumber(): Promise<{ errorCode: number; result: BlockNumberResult }>;
|
198
|
+
|
199
|
+
/**
|
200
|
+
* 检查区块同步状态
|
201
|
+
* @returns {Promise<{errorCode:number, result:BlockStatusResult}>} 返回区块同步状态
|
202
|
+
* @example
|
203
|
+
* sdk.block.checkStatus().then(console.log);
|
204
|
+
*
|
205
|
+
* // 错误码:
|
206
|
+
* // - SYSTEM_ERROR 20000 系统错误
|
207
|
+
*/
|
21
208
|
checkStatus(): Promise<{ errorCode: number; result: BlockStatusResult }>;
|
209
|
+
|
210
|
+
/**
|
211
|
+
* 获取指定区块中的交易列表
|
212
|
+
* @param {string} blockNumber 区块高度
|
213
|
+
* @returns {Promise<{errorCode:number, result:TransactionListResult}>} 返回区块中的交易列表
|
214
|
+
* @example
|
215
|
+
* sdk.block.getTransactions('100').then(console.log);
|
216
|
+
*
|
217
|
+
* // 错误码:
|
218
|
+
* // - INVALID_BLOCKNUMBER_ERROR 11007 无效区块高度
|
219
|
+
* // - BLOCK_NOT_EXIST 11008 区块不存在
|
220
|
+
*/
|
22
221
|
getTransactions(blockNumber: string): Promise<{ errorCode: number; result: TransactionListResult }>;
|
23
222
|
};
|
24
223
|
|
25
224
|
operation: {
|
225
|
+
/**
|
226
|
+
* 创建资产发行操作
|
227
|
+
* @param {AssetIssueParams} params 资产发行参数
|
228
|
+
* @returns {OperationResult} 返回操作结果
|
229
|
+
*/
|
26
230
|
assetIssueOperation(params: AssetIssueParams): { errorCode: number; result: OperationResult };
|
231
|
+
|
232
|
+
/**
|
233
|
+
* 创建账户激活操作
|
234
|
+
* @param {AccountActivateParams} params 账户激活参数
|
235
|
+
* @returns {OperationResult} 返回操作结果
|
236
|
+
*/
|
27
237
|
accountActivateOperation(params: AccountActivateParams): { errorCode: number; result: OperationResult };
|
238
|
+
|
239
|
+
/**
|
240
|
+
* 创建账户元数据设置操作
|
241
|
+
* @param {MetadataParams} params 元数据设置参数
|
242
|
+
* @returns {OperationResult} 返回操作结果
|
243
|
+
*/
|
28
244
|
accountSetMetadataOperation(params: MetadataParams): { errorCode: number; result: OperationResult };
|
29
245
|
};
|
30
246
|
|
@@ -36,7 +252,7 @@ declare class OpenChainSDK {
|
|
36
252
|
};
|
37
253
|
}
|
38
254
|
|
39
|
-
interface AccountInfoResult {
|
255
|
+
export interface AccountInfoResult {
|
40
256
|
address: string;
|
41
257
|
balance: string;
|
42
258
|
nonce: string;
|
@@ -46,7 +262,15 @@ interface AccountInfoResult {
|
|
46
262
|
}>;
|
47
263
|
}
|
48
264
|
|
49
|
-
|
265
|
+
/**
|
266
|
+
* 区块高度查询结果
|
267
|
+
* @interface
|
268
|
+
* @property {Object} header 区块头信息
|
269
|
+
* @property {string} header.blockNumber 区块高度
|
270
|
+
* @property {string} header.timestamp 区块时间戳(毫秒)
|
271
|
+
* @property {number} header.txCount 交易数量
|
272
|
+
*/
|
273
|
+
export interface BlockNumberResult {
|
50
274
|
header: {
|
51
275
|
blockNumber: string;
|
52
276
|
timestamp: string;
|
@@ -54,12 +278,19 @@ interface BlockNumberResult {
|
|
54
278
|
};
|
55
279
|
}
|
56
280
|
|
57
|
-
interface BlockStatusResult {
|
281
|
+
export interface BlockStatusResult {
|
282
|
+
/** 是否完成同步 */
|
58
283
|
isSynchronous: boolean;
|
284
|
+
|
285
|
+
/**
|
286
|
+
* 账本版本号
|
287
|
+
* @pattern ^\d+\.\d+\.\d+$
|
288
|
+
* @example "1.2.3"
|
289
|
+
*/
|
59
290
|
ledgerVersion: string;
|
60
291
|
}
|
61
292
|
|
62
|
-
interface TransactionListResult {
|
293
|
+
export interface TransactionListResult {
|
63
294
|
total_count: number;
|
64
295
|
transactions: Array<{
|
65
296
|
hash: string;
|
@@ -69,7 +300,22 @@ interface TransactionListResult {
|
|
69
300
|
}>;
|
70
301
|
}
|
71
302
|
|
72
|
-
|
303
|
+
/**
|
304
|
+
* 合约创建参数
|
305
|
+
* @interface
|
306
|
+
* @property {string} sourceAddress 操作源账户地址
|
307
|
+
* @property {string} initBalance 合约账户初始余额(单位MO)
|
308
|
+
* @property {number} type 合约类型(0-JavaScript, 1-EVM)
|
309
|
+
* @property {string} payload 合约代码
|
310
|
+
* @property {string} [initInput] 合约初始化参数
|
311
|
+
* @property {string} [metadata] 操作备注
|
312
|
+
* @example
|
313
|
+
* 参见test/contractCreateTransaction.test.js示例
|
314
|
+
*
|
315
|
+
* // 错误码:
|
316
|
+
* // - PAYLOAD_EMPTY_ERROR 11044 合约代码为空
|
317
|
+
*/
|
318
|
+
export interface ContractCreateParams {
|
73
319
|
sourceAddress: string;
|
74
320
|
initBalance: string;
|
75
321
|
type: number;
|
@@ -78,52 +324,113 @@ interface ContractCreateParams {
|
|
78
324
|
metadata?: string;
|
79
325
|
}
|
80
326
|
|
81
|
-
|
327
|
+
/**
|
328
|
+
* 合约调用参数
|
329
|
+
* @interface
|
330
|
+
* @property {string} sourceAddress 调用者账户地址
|
331
|
+
* @property {string} contractAddress 合约账户地址
|
332
|
+
* @property {string} input 合约调用输入数据
|
333
|
+
* @property {string} [metadata] 操作备注
|
334
|
+
*/
|
335
|
+
export interface ContractInvokeParams {
|
336
|
+
sourceAddress: string;
|
337
|
+
contractAddress: string;
|
338
|
+
input: string;
|
339
|
+
metadata?: string;
|
340
|
+
}
|
341
|
+
|
342
|
+
/**
|
343
|
+
* 合约调用结果
|
344
|
+
* @interface
|
345
|
+
* @property {string} result 合约执行结果
|
346
|
+
* @property {string} logs 合约执行日志
|
347
|
+
*/
|
348
|
+
export interface ContractInvokeResult {
|
349
|
+
result: string;
|
350
|
+
logs: string;
|
351
|
+
}
|
352
|
+
|
353
|
+
export interface OperationResult {
|
82
354
|
operation: {
|
83
355
|
type: string;
|
84
356
|
data: Record<string, any>;
|
85
357
|
};
|
86
358
|
}
|
87
359
|
|
88
|
-
|
360
|
+
/**
|
361
|
+
* 合约信息查询结果
|
362
|
+
* @interface
|
363
|
+
* @property {Object} contract 合约详情
|
364
|
+
* @property {number} contract.type 合约类型(0-JavaScript, 1-EVM)
|
365
|
+
* @property {string} contract.payload 合约字节码/源代码
|
366
|
+
* @example
|
367
|
+
* 参见doc/SDK_CN.md中'getInfo-合约'示例
|
368
|
+
*/
|
369
|
+
export interface ContractInfoResult {
|
89
370
|
contract: {
|
90
371
|
type: number;
|
91
372
|
payload: string;
|
92
373
|
};
|
93
374
|
}
|
94
375
|
|
95
|
-
interface ContractAddressInfo {
|
376
|
+
export interface ContractAddressInfo {
|
377
|
+
/**
|
378
|
+
* 合约账户地址
|
379
|
+
* @pattern ^adx[a-zA-Z0-9]{34}$
|
380
|
+
* @example
|
381
|
+
* 'adxSqKcX8wGCMKhzNUBoDWfbeQaMhfnGdtyG2' // 标准地址格式示例
|
382
|
+
*/
|
96
383
|
contract_address: string;
|
384
|
+
|
385
|
+
/**
|
386
|
+
* 操作索引号(对应交易中的操作顺序)
|
387
|
+
* @minimum 0
|
388
|
+
* @maximum 100
|
389
|
+
* @example
|
390
|
+
* 0 // 第一个操作
|
391
|
+
*/
|
97
392
|
operation_index: number;
|
98
393
|
}
|
99
394
|
|
100
|
-
interface AssetIssueParams {
|
395
|
+
export interface AssetIssueParams {
|
101
396
|
sourceAddress: string;
|
102
397
|
code: string;
|
103
398
|
assetAmount: string;
|
104
399
|
metadata?: string;
|
105
400
|
}
|
106
401
|
|
107
|
-
interface MetadataResult {
|
402
|
+
export interface MetadataResult {
|
108
403
|
version: string;
|
109
404
|
value: string;
|
110
405
|
}
|
111
406
|
|
112
|
-
interface MetadataParams {
|
407
|
+
export interface MetadataParams {
|
113
408
|
key: string;
|
114
409
|
value: string;
|
115
410
|
version?: string;
|
116
411
|
deleteFlag?: number;
|
117
412
|
}
|
118
413
|
|
119
|
-
interface AccountActivateParams {
|
414
|
+
export interface AccountActivateParams {
|
120
415
|
sourceAddress: string;
|
121
416
|
destAddress: string;
|
122
417
|
initBalance: string;
|
123
418
|
metadata?: string;
|
124
419
|
}
|
125
420
|
|
126
|
-
|
421
|
+
/**
|
422
|
+
* 交易参数结构定义
|
423
|
+
* @interface
|
424
|
+
* @property {string} sourceAddress 交易发起账户地址
|
425
|
+
* @property {string} nonce 交易序列号(需大于上次交易nonce)
|
426
|
+
* @property {OperationResult[]} operations 操作列表
|
427
|
+
* @property {string} [gasPrice] Gas单价(单位MO)
|
428
|
+
* @property {string} [feeLimit] 交易费上限
|
429
|
+
* @property {string} [metadata] 交易备注信息
|
430
|
+
* @example
|
431
|
+
* 参见test/contractCreateTransaction.test.js中的使用示例
|
432
|
+
*/
|
433
|
+
export interface TransactionParams {
|
127
434
|
sourceAddress: string;
|
128
435
|
nonce: string;
|
129
436
|
operations: OperationResult[];
|
@@ -132,24 +439,51 @@ interface TransactionParams {
|
|
132
439
|
metadata?: string;
|
133
440
|
}
|
134
441
|
|
135
|
-
interface TransactionBuildBlobResult {
|
442
|
+
export interface TransactionBuildBlobResult {
|
136
443
|
transactionBlob: string;
|
137
444
|
hash: string;
|
138
445
|
}
|
139
446
|
|
140
|
-
interface TransactionSignParams {
|
447
|
+
export interface TransactionSignParams {
|
448
|
+
/**
|
449
|
+
* 交易原始数据(序列化后的HEX字符串)
|
450
|
+
* @pattern ^[0-9a-fA-F]+$
|
451
|
+
* @example
|
452
|
+
* 参见test/blob.test.js中的签名示例
|
453
|
+
* @example
|
454
|
+
* '0a246275...' // 完整的交易blob示例
|
455
|
+
*/
|
141
456
|
transactionBlob: string;
|
457
|
+
|
458
|
+
/**
|
459
|
+
* 签名私钥列表(支持多签)
|
460
|
+
* @minItems 1
|
461
|
+
* @maxItems 8
|
462
|
+
* @items {string} 私钥格式要求:以'priv'开头的base58编码字符串
|
463
|
+
* @example
|
464
|
+
* ['privb3dE...', 'privK28m...'] // 多签示例
|
465
|
+
*/
|
142
466
|
privateKeys: string[];
|
143
467
|
}
|
144
468
|
|
145
|
-
interface TransactionSignResult {
|
469
|
+
export interface TransactionSignResult {
|
146
470
|
signatures: Array<{
|
147
471
|
signData: string;
|
148
472
|
publicKey: string;
|
149
473
|
}>;
|
150
474
|
}
|
151
475
|
|
152
|
-
|
476
|
+
/**
|
477
|
+
* 交易提交参数
|
478
|
+
* @interface
|
479
|
+
* @property {string} transactionBlob 交易原始数据
|
480
|
+
* @property {Array} signatures 签名列表
|
481
|
+
* @property {Object[]} signatures.signData 签名数据
|
482
|
+
* @property {string} signatures.publicKey 签名公钥
|
483
|
+
* @example
|
484
|
+
* 参见test/log.transaction.test.js中的使用示例
|
485
|
+
*/
|
486
|
+
export interface TransactionSubmitParams {
|
153
487
|
transactionBlob: string;
|
154
488
|
signatures: Array<{
|
155
489
|
signData: string;
|
@@ -157,11 +491,25 @@ interface TransactionSubmitParams {
|
|
157
491
|
}>;
|
158
492
|
}
|
159
493
|
|
160
|
-
interface TransactionSubmitResult {
|
494
|
+
export interface TransactionSubmitResult {
|
161
495
|
hash: string;
|
162
496
|
}
|
163
497
|
|
164
|
-
|
498
|
+
/**
|
499
|
+
* 交易详情查询结果
|
500
|
+
* @interface
|
501
|
+
* @property {number} totalCount 总交易数
|
502
|
+
* @property {Array} transactions 交易列表
|
503
|
+
* @property {string} transactions.closeTime 交易关闭时间
|
504
|
+
* @property {number} transactions.errorCode 错误码
|
505
|
+
* @property {string} transactions.errorDesc 错误描述
|
506
|
+
* @property {string} transactions.hash 交易哈希
|
507
|
+
* @property {string} transactions.ledgerSeq 账本序列号
|
508
|
+
* @property {Object} transactions.transaction 交易详情
|
509
|
+
* @example
|
510
|
+
* 参见sdk.transaction.getInfo方法示例
|
511
|
+
*/
|
512
|
+
export interface TransactionResult {
|
165
513
|
totalCount: number;
|
166
514
|
transactions: Array<{
|
167
515
|
closeTime: string;
|
@@ -183,4 +531,5 @@ interface TransactionResult {
|
|
183
531
|
};
|
184
532
|
txSize: number;
|
185
533
|
}>;
|
186
|
-
}
|
534
|
+
}
|
535
|
+
export default OpenChainSDK;
|
package/index.js
CHANGED
@@ -0,0 +1,90 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
function createBufferPolyfill() {
|
4
|
+
if (typeof window !== 'undefined' && typeof Buffer === 'undefined') {
|
5
|
+
const BufferPolyfill = {
|
6
|
+
from: function(data, encoding) {
|
7
|
+
if (typeof data === 'string') {
|
8
|
+
if (encoding === 'hex') {
|
9
|
+
// 确保十六进制字符串格式正确
|
10
|
+
if (data.length % 2 !== 0) {
|
11
|
+
throw new Error('Invalid hex string');
|
12
|
+
}
|
13
|
+
return Uint8Array.from(data.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
|
14
|
+
} else if (encoding === 'utf8' || !encoding) {
|
15
|
+
return new TextEncoder().encode(data);
|
16
|
+
}
|
17
|
+
} else if (data instanceof Uint8Array) {
|
18
|
+
return data;
|
19
|
+
} else if (Array.isArray(data)) {
|
20
|
+
return new Uint8Array(data);
|
21
|
+
}
|
22
|
+
throw new Error('Unsupported data type for Buffer.from');
|
23
|
+
},
|
24
|
+
isBuffer: function(obj) {
|
25
|
+
return obj instanceof Uint8Array;
|
26
|
+
},
|
27
|
+
// 添加concat方法
|
28
|
+
concat: function(list, length) {
|
29
|
+
if (!Array.isArray(list)) {
|
30
|
+
throw new Error('Usage: Buffer.concat(list[, length])');
|
31
|
+
}
|
32
|
+
|
33
|
+
if (list.length === 0) {
|
34
|
+
return new Uint8Array(0);
|
35
|
+
}
|
36
|
+
|
37
|
+
let totalLength = 0;
|
38
|
+
for (const buf of list) {
|
39
|
+
totalLength += buf.length;
|
40
|
+
}
|
41
|
+
|
42
|
+
const result = new Uint8Array(length !== undefined ? length : totalLength);
|
43
|
+
let offset = 0;
|
44
|
+
for (const buf of list) {
|
45
|
+
result.set(buf, offset);
|
46
|
+
offset += buf.length;
|
47
|
+
}
|
48
|
+
|
49
|
+
return result;
|
50
|
+
}
|
51
|
+
};
|
52
|
+
|
53
|
+
// 为Uint8Array原型添加toString方法
|
54
|
+
if (!Uint8Array.prototype.toString) {
|
55
|
+
Uint8Array.prototype.toString = function(encoding) {
|
56
|
+
if (encoding === 'hex') {
|
57
|
+
// 添加手动转换备选方案
|
58
|
+
if (typeof Buffer === 'undefined') {
|
59
|
+
return this.manualToHex();
|
60
|
+
}
|
61
|
+
return Array.from(this).map(byte => byte.toString(16).padStart(2, '0')).join('');
|
62
|
+
} else if (encoding === 'utf8' || !encoding) {
|
63
|
+
return new TextDecoder().decode(this);
|
64
|
+
}
|
65
|
+
throw new Error('Unsupported encoding for toString');
|
66
|
+
};
|
67
|
+
}
|
68
|
+
|
69
|
+
// 将手动转换方法挂载到Uint8Array原型链
|
70
|
+
Uint8Array.prototype.manualToHex = function() {
|
71
|
+
if (!(this instanceof Uint8Array)) {
|
72
|
+
return Array.from(new Uint8Array(this)).map(byte =>
|
73
|
+
byte.toString(16).padStart(2, '0')
|
74
|
+
).join('');
|
75
|
+
}
|
76
|
+
return Array.from(this).map(byte =>
|
77
|
+
byte.toString(16).padStart(2, '0')
|
78
|
+
).join('');
|
79
|
+
};
|
80
|
+
|
81
|
+
// 确保在浏览器环境中正确设置全局Buffer对象
|
82
|
+
if (typeof window !== 'undefined') {
|
83
|
+
window.Buffer = BufferPolyfill;
|
84
|
+
} else if (typeof global !== 'undefined') {
|
85
|
+
global.Buffer = BufferPolyfill;
|
86
|
+
}
|
87
|
+
}
|
88
|
+
}
|
89
|
+
|
90
|
+
module.exports = createBufferPolyfill;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "openchain-nodejs-ts-yxl",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.3",
|
4
4
|
"description": "openchain sdk",
|
5
5
|
"main": "index.js",
|
6
6
|
"types": "index.d.ts",
|
@@ -25,7 +25,7 @@
|
|
25
25
|
"protobufjs": "^6.8.8",
|
26
26
|
"request": "^2.87.0",
|
27
27
|
"request-promise": "^4.2.2",
|
28
|
-
"yxchain-encryption-nodejs": "^1.0.
|
28
|
+
"yxchain-encryption-nodejs": "^1.0.2"
|
29
29
|
},
|
30
30
|
"devDependencies": {
|
31
31
|
"chai": "^4.1.2",
|