dmc.js

NPM version

DMC 區塊鏈的通用庫。

請使用 DMC 進行操作,見 安裝

Basic

在區塊鏈中對用戶賬戶信息操作的各種方法

獲取客戶端實例

以鏈接到測試網為例

1
2
3
4
5
6
7
8
9
10
var DMC = require("dmc.js");
let dmc_client = DMC({
chainId: "ChainId",
keyProvider: "KeyProvider",
httpEndpoint: "httpEndpoint",
logger: {
log: null,
error: null
}
});

get_account

返回一個對象,其中包含有關區塊鏈上特定帳戶的各種詳細信息。

1
dmc_client.rpc.get_account_sync(account_name)

參數

參數名 類型 描述
account_name string 賬戶名

示例

1
2
var getAccount = dmc_client.rpc.get_account_sync("testnetbppa1");
console.log(getAccount)

返回值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
{
"account_name": "testnetbppa1",
"head_block_num": 2528,
"head_block_time": "2023-02-10T03:55:55.500",
"privileged": false,
"last_code_update": "1970-01-01T00:00:00.000",
"created": "2023-02-10T03:35:15.500",
"core_liquid_balance": "10000.0000 DMC",
"ram_quota": 5244279,
"net_weight": "1000000100000",
"cpu_weight": "1000000100000",
"net_limit": {
"used": 281,
"available": "272169239974",
"max": "272169240255",
"last_usage_update_time": "2023-02-10T03:35:40.500",
"current_used": 277
},
"cpu_limit": {
"used": 319,
"available": "86493297511",
"max": "86493297830",
"last_usage_update_time": "2023-02-10T03:35:40.500",
"current_used": 314
},
"ram_usage": 4185,
"permissions": [
{
"perm_name": "active",
"parent": "owner",
"required_auth": [Object],
"linked_actions": []
},
{
"perm_name": "owner",
"parent": "",
"required_auth": [Object],
"linked_actions": []
}
],
"total_resources": {
"owner": "testnetbppa1",
"net_weight": "100000010.0000 DMC",
"cpu_weight": "100000010.0000 DMC",
"ram_bytes": 5242879
},
"self_delegated_bandwidth": {
"from": "testnetbppa1",
"to": "testnetbppa1",
"net_weight": "100000010.0000 DMC",
"cpu_weight": "100000010.0000 DMC"
},
"refund_request": null,
"voter_info": {
"owner": "testnetbppa1",
"proxy": "",
"producers": [
"testnetbppa1"
],
"staked": "2000000200000",
"last_vote_weight": "18915650661469794304.00000000000000000",
"proxied_vote_weight": "0.00000000000000000",
"flags1": 0,
"reserved2": 0,
"reserved3": "0 "
},
"rex_info": null,
"subjective_cpu_bill_limit": {
"used": 0,
"available": 0,
"max": 0,
"last_usage_update_time": "2000-01-01T00:00:00.000",
"current_used": 0
},
"dmc_any_linked_actions": []
}

get_info

獲取區塊信息

1
dmc_client.rpc.get_info_sync();

示例

1
2
var getInfo = dmc_client.get_info_sync();
console.log(getInfo);

返回值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"server_version": "3f9cb99e",
"chain_id": "fe50c592f3d9b395455ee6499c13e2269cb9cdc9605465c7ba04d4af54cf2e3f",
"head_block_num": 1783,
"last_irreversible_block_num": 1782,
"last_irreversible_block_id": "000006f60537ce6fbb108a693e55338ca7fe948f5f9992c0591434df7635420e",
"head_block_id": "000006f7bc72c02492fb41e6cfc056c62cc7a6c3f7ed9d14dbd280f7e92298f1",
"head_block_time": "2023-02-10T03:49:43.000",
"head_block_producer": "testnetbppa1",
"virtual_block_cpu_limit": 5938636,
"virtual_block_net_limit": 18686584,
"block_cpu_limit": 999999,
"block_net_limit": 3145728,
"server_version_string": "v3.2.1",
"fork_db_head_block_num": 1783,
"fork_db_head_block_id": "000006f7bc72c02492fb41e6cfc056c62cc7a6c3f7ed9d14dbd280f7e92298f1",
"server_full_version_string": "v3.2.1-3f9cb99e068da8581036d212d74fb56144ef0fc4",
"total_cpu_weight": "25000002600000",
"total_net_weight": "25000002600000",
"earliest_available_block_num": 1,
"last_irreversible_block_time": "2023-02-10T03:49:42.500"
}

返回值參數說明

參數 類型 說明
server_version string 服務端版本
chain_id string 區塊鏈的鏈地址
head_block_num number 當前區塊高度
last_irreversible_block_num number 不可逆區塊高度
last_irreversible_block_id string 不可逆區塊地址
head_block_id string 當前區塊地址
head_block_time date 當前區塊時間
head_block_producer string 當前區塊進程
virtual_block_cpu_limit number 虛擬區塊 cpu 限制
virtual_block_net_limit number 虛擬區塊網絡限制
block_cpu_limit number 區塊 cpu 限制
block_net_limit number 區塊網絡限制
server_version_string string 服務端版本字符串
fork_db_head_block_num number fork 數據庫區塊高度
fork_db_head_block_id string fork 數據庫區塊地址
server_full_version_string string 服務端完整版本字符串
earliest_available_block_num number 最早可用區塊高度
last_irreversible_block_time date 不可逆區塊時間

get_code

返回一個包含指定合約wasm的對象

1
dmc_client.rpc.get_code_sync(account_name, code_as_wasm);

參數

參數 類型 說明
account_name string 合約名
code_as_wasm boolean true/false

示例

1
2
var getCode = dmc_client.rpc.get_code_sync("dmc.token",true);
console.log(getCode)

返回值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
{
"account_name": "dmc.token",
"code_hash": "ef767c5372409a154a4dd0d73c3a819dd0ae481e7e686793f43afdaf060abc1e",
"wast": "",
"wasm": "\u0000asm\u0001\u0000\u0000\u0000\u0001\u0002(`\u0000\u0000`\u0003\u007f\u007f\u007f\u0001\u007f`\u0003\u007f~\u007f\u0001~`\u0004\u007f\u007f\u007f\u007f\u0000`\u0001~\u0000`\u0002\u007f\u007f\u0000`\u0004~~~~\u0001\u007f`\u0000\u0001~`\u0006~~~~\u007f\u007f\u0001\u007f`\u0004\u007f~\u007f\u007f\u0000`......",
"abi": {
"version": "dmc::abi/1.2",
"types": [],
"structs": [
{
"name": "account",
"base": "",
"fields": [
{
"name": "balance",
"type": "asset"
}
]
},
{
"name": "close",
"base": "",
"fields": [
{
"name": "owner",
"type": "name"
},
{
"name": "symbol",
"type": "symbol"
}
]
},
.....
],
"actions": [
{
"name": "close",
"type": "close",
"ricardian_contract": ""
},
{
"name": "create",
"type": "create",
"ricardian_contract": ""
},
....
],
"tables": [
{
"name": "accounts",
"index_type": "i64",
"key_names": [],
"key_types": [],
"type": "account"
},
{
"name": "stat",
"index_type": "i64",
"key_names": [],
"key_types": [],
"type": "currency_stats"
}
],
"ricardian_clauses": [],
"error_messages": [],
"abi_extensions": [],
"variants": [],
"action_results": []
}
}

get_abi

獲取賬戶的abi數據

1
dmc_client.rpc.get_abi_sync(account_name);

參數

參數 類型 說明
account_name string 賬號名

示例

1
2
var getAbi = dmc_client.rpc.get_abi_sync("dmc");
console.log(getAbi)

返回值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
{
"account_name": "dmc",
"abi": {
"version": "dmc::abi/1.2",
"types": [
{
"new_type_name": "block_signing_authority",
"type": "variant_block_signing_authority_v0"
}
],
"structs": [
{
"name": "abi_hash",
"base": "",
"fields": [
{
"name": "owner",
"type": "name"
},
{
"name": "hash",
"type": "checksum256"
}
]
},
{
"name": "activate",
"base": "",
"fields": [
{
"name": "feature_digest",
"type": "checksum256"
}
]
},
......
],
"tables": [
{
"name": "abihash",
"index_type": "i64",
"key_names": [],
"key_types": [],
"type": "abi_hash"
},
{
"name": "bidrefunds",
"index_type": "i64",
"key_names": [],
"key_types": [],
"type": "bid_refund"
},
{
"name": "cpuloan",
"index_type": "i64",
"key_names": [],
"key_types": [],
"type": "rex_loan"
},
........
],
"ricardian_clauses": [],
"error_messages": [],
"abi_extensions": [],
"variants": [
{
"name": "variant_block_signing_authority_v0",
"types": [
"block_signing_authority_v0"
]
}
],
"action_results": []
}
}

get_block

獲取塊信息

1
dmc_client.rpc.get_block_sync(block_num_or_id);

參數

參數 類型 描述
block_num_or_id int 塊的編號或者id值

示例

1
2
var getBlock = dmc_client.rpc.get_block_sync(5);
console.log(getBlock)

返回值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"timestamp": "2023-02-10T03:34:54.000",
"producer": "dmc",
"confirmed": 0,
"previous": "00000004d6d64a943e923058733e12d9b61639d2706d5f506eb81460864bc769",
"transaction_mroot": "0000000000000000000000000000000000000000000000000000000000000000",
"action_mroot": "46aeeb444eaf0161365fa51477d7aa767faac11ed63b89f33b32d9aba73f8e7a",
"schedule_version": 0,
"new_producers": null,
"producer_signature": "SIG_K1_KeWB4eG1zbcuJogdnX1Yjyt43Vx6c5U5easEUGh2WVqD8hRxYeNy1G4AqN6UJvjXeVg1si75sxKLJwVDiUCC2WLvsYTQzQ",
"transactions": [],
"id": "00000005c5380276d85087a0c34f094b2b083e4bb83fe4ad6b4224d1b275eb11",
"block_num": 5,
"ref_block_prefix": 2693222616
}

get_table_rows

返回指定表中的數據

1
dmc_client.rpc.getTableRows(code, table,scope, index_position,key_type, encode_type, lower_bound, upper_bound, limit, reverse, show_payer);

參數

參數名 類型 是否必須 描述
code string 合約名
table string 表名
scope string DMC 賬戶名
index_position string 使用的索引位置
key_type string 由index_position指定的鍵類型
encode_type string 編碼類型
lower_bound string 最低界限
upper_bound string 最高界限
limit integer 返回結果數量
reverse boolean 是否將結果倒置
show_payer boolean 是否展示RAM支付人

示例

1
2
3
4
5
6
7
var getTableRows = dmc_client.rpc.get_table_rows_sync({
json: true,
scope: "dmc",
code: "dmc",
table: "global"
});;
console.log(getTableRows)

返回值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{
"rows": [
{
"max_block_net_usage": 3145728,
"target_block_net_usage_pct": 1000,
"max_transaction_net_usage": 524287,
"base_per_transaction_net_usage": 12,
"net_usage_leeway": 500,
"context_free_discount_net_usage_num": 20,
"context_free_discount_net_usage_den": 100,
"max_block_cpu_usage": 1000000,
"target_block_cpu_usage_pct": 10,
"max_transaction_cpu_usage": 150000,
"min_transaction_cpu_usage": 1,
"max_transaction_lifetime": 3600,
"deferred_trx_expiration_window": 600,
"max_transaction_delay": 3888000,
"max_inline_action_size": 524287,
"max_inline_action_depth": 32,
"max_authority_depth": 6,
"max_ram_size": "68719476736",
"total_ram_bytes_reserved": 136314841,
"total_ram_stake": 198758485,
"last_producer_schedule_update": "2023-02-10T04:13:59.500",
"last_pervote_bucket_fill": "2023-02-10T03:35:41.000",
"pervote_bucket": 0,
"perblock_bucket": 0,
"total_unpaid_blocks": 4705,
"total_activated_stake": "2000000200000",
"thresh_activated_stake_time": "2023-02-10T03:35:40.500",
"last_producer_schedule_size": 1,
"total_producer_vote_weight": "18915650661469794304.00000000000000000",
"last_name_close": "2000-01-01T00:00:00.000"
}
],
"more": false,
"next_key": ""
}

ecc 模塊

橢圓曲線加密函數, 內置多個方法用來生成我們需要的秘鑰

randomKey()

生成隨機私鑰

1
DMC.ecc.randomKey_sync(cpuEntropyBits?, options);

參數

參數名 類型 是否必須 描述
cpuEntropyBits number cpu熵位
options object 配置項

返回值

返回一個隨機私鑰

示例

1
2
3
4
5
6
var randomKey = DMC.ecc.randomKey_sync(undefined, { secureEnv: true })
console.log(randomKey)
/*
運行結果:
5JJznTGghwYBCJoW6EcpzZfybKEDqFULMniatBp9aykeauP55bB
*/

privateToPublic()

通過私鑰信息來計算公鑰信息

1
DMC.ecc.privateToPublic(key,pubkey_prefix?);

參數

參數名 類型 是否必須 描述
key string 私鑰
pubkey_prefix string 公鑰前綴

返回值

返回公鑰

示例

1
2
3
4
5
6
var publicKey = DMC.ecc.privateToPublic("5JJznTGghwYBCJoW6EcpzZfybKEDqFULMniatBp9aykeauP55bB");
console.log(publicKey)
/*
運行結果:
DM75N93rrsoBzEzrdZePZtkDUQsXiTWX51irsSBBukxB6Jg4nrua
*/

isValidPublic()

判斷公鑰是否有效

1
DMC.ecc.isValidPublic(pubkey,pubkey_prefix?);

參數

參數名 類型 是否必須 描述
pubkey string 公鑰
pubkey_prefix string 公鑰前綴

返回值

判斷公鑰是否有效

示例

1
2
3
4
5
var isValidPublic = DMC.ecc.isValidPublic("DM75N93rrsoBzEzrdZePZtkDUQsXiTWX51irsSBBukxB6Jg4nrua");
console.log(isValidPublic)
/*
運行結果:true
*/

isValidPrivate()

判斷私鑰是否有效

1
DMC.ecc.isValidPrivate(wif);

參數

參數名 類型 是否必須 描述
wif string 私鑰

返回值

判斷私鑰是否有效

示例

1
2
3
4
5
var isValidPrivate = DMC.ecc.isValidPrivate("5JJznTGghwYBCJoW6EcpzZfybKEDqFULMniatBp9aykeauP55bB");
console.log(isValidPrivate)
/*
運行結果:true
*/

sign()

使用 data 信息或 hash 來創建簽名

1
DMC.ecc.sign(data,privateKey,encoding?);

參數

參數名 類型 是否必須 描述
data string 數據
PrivateKey string 私鑰
encoding string 數據編碼

返回值

字符串簽名

示例

1
2
3
4
5
6
var sign = DMC.ecc.sign("i am alive","5JJznTGghwYBCJoW6EcpzZfybKEDqFULMniatBp9aykeauP55bB");
console.log(sign)
/*
運行結果:
SIG_K1_Kb64YDjawgAV8fbJaJXpyjwwFVTAp8tBjzq8zMZKmw4SUrRnWsLiSxcfHfbWp1XFikuBuC7gV71MCoCZm4xfoGLCC7KkGJ
*/

signHash()

使用進行 hash 加密的 data 信息和私鑰來創建簽名

1
DMC.ecc.signHash(dataSha256, privateKey, encoding?);

參數

參數名 類型 是否必須 描述
dataSha256 string sha256 hash 32 字節緩衝區或字符串
PrivateKey string 私鑰
encoding string dataSha256 編碼(如果是字符串)

返回值

帶有 hash 加密的字符串簽名

示例

1
2
3
4
5
6
7
var dataSha256 = ecc.sha256("i am alive")
var signHash = DMC.ecc.signHash(dataSha256,"5JJznTGghwYBCJoW6EcpzZfybKEDqFULMniatBp9aykeauP55bB");
console.log(signHash)
/*
運行結果:
SIG_K1_JvdebW7gjJkmUMm4xVa6poGZEPGvu37qXgu38TKRfAmC4mJpSJ2rJZejZUDkLshtQc1i7EhbkEVoN4ZwAsciWT3D8re2pD
*/

verify()

驗證簽名數據

1
DMC.ecc.verify(signature,data,pubkey,encoding?,hashData?);

參數

參數名 類型 是否必須 描述
signature string 簽名
data string 數據
pubkey string 公鑰
encoding string 數據編碼
hashData boolean 是否對數據進行 hash 加密

返回值

判斷簽名數據是否成立

示例

1
2
3
4
5
var verify = DMC.ecc.verify("SIG_K1_Kb64YDjawgAV8fbJaJXpyjwwFVTAp8tBjzq8zMZKmw4SUrRnWsLiSxcfHfbWp1XFikuBuC7gV71MCoCZm4xfoGLCC7KkGJ","i am alive","DM75N93rrsoBzEzrdZePZtkDUQsXiTWX51irsSBBukxB6Jg4nrua");
console.log(verify)
/*
運行結果:true
*/

recover()

恢復用於創建簽名的公鑰

1
DMC.ecc.recover(signature, data,encoding?);

參數

參數名 類型 是否必須 描述
signature string 簽名
data string 數據
encoding string 數據編碼

返回值

返回公鑰

示例

1
2
3
4
5
6
var recover = DMC.ecc.recover("SIG_K1_Kb64YDjawgAV8fbJaJXpyjwwFVTAp8tBjzq8zMZKmw4SUrRnWsLiSxcfHfbWp1XFikuBuC7gV71MCoCZm4xfoGLCC7KkGJ","i am alive")
console.log(recover)
/*
運行結果:
DM75N93rrsoBzEzrdZePZtkDUQsXiTWX51irsSBBukxB6Jg4nrua
*/

recoverHash()

恢復用於創建 hash 簽名的公鑰

1
DMC.ecc.recoverHash(signature,dataSha256,encoding);

參數

參數名 類型 是否必須 描述
signature string 簽名
dataSha256 string sha256 hash 32 字節緩衝區或字符串
encoding string dataSha256 編碼(如果是字符串)

返回值

返回一個公鑰

示例

1
2
3
4
5
6
7
var dataSha256 = ecc.sha256("i am alive")
var recoverHash = DMC.ecc.recoverHash("SIG_K1_Kb64YDjawgAV8fbJaJXpyjwwFVTAp8tBjzq8zMZKmw4SUrRnWsLiSxcfHfbWp1XFikuBuC7gV71MCoCZm4xfoGLCC7KkGJ",dataSha256)
console.log(recoverHash)
/*
運行結果:
DM75N93rrsoBzEzrdZePZtkDUQsXiTWX51irsSBBukxB6Jg4nrua
*/

sha256()

sha256 加密

1
DMC.ecc.sha256(data,resultEncoding?,encoding?);

參數

參數名 類型 是否必須 描述
data string 二進制數據,需要 Buffer.from(data,’hex’)
resultEncoding string 結果編碼成 ‘hex’,’binary’ 或 ‘base64’(可選,默認'hex'
encoding string data 編碼(如果是字符串)(可選,默認'hex'

返回值

使用 hash.sha256 加密後的數據

示例

1
2
3
4
5
6
var dataSha256 = DMC.ecc.sha256("i am alive")
console.log(dataSha256)
/*
運行結果:
[211,201,31,63,220,1,231,226,206,123,34,189,12,24,164,249,248,105,109,209,215,75,232,139,218,218,158,87,121,243,78,241]
*/