dmc.js

NPM version

Common library for the DMC blockchain.

Please utilize the DMC, see installation

Basic

Various methods for operating user account information in the blockchain.

Instance of Retrieving Client

Taking the testnet as an instance.

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

Returns an object containing various details about a specific account on the blockchain.

1
dmc_client.rpc.get_account_sync(account_name)

Parameter

Parameter name Type Description
account_name string Account name

Instance

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

Return Value

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

Get block information.

1
dmc_client.rpc.get_info_sync();

Instance

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

Return Value

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

Explanation of return value parameters

Parameters Type Description
server_version string Server Version
chain_id string Blockchain Chain ID
head_block_num number Current Block Height
last_irreversible_block_num number Irreversible Block Height
last_irreversible_block_id string Irreversible Block ID
head_block_id string Current Block ID
head_block_time date Current Block Time
head_block_producer string Current Block Producer
virtual_block_cpu_limit number Virtual Block CPU Limit
virtual_block_net_limit number Virtual Block Network Limit
block_cpu_limit number Block CPU Limit
block_net_limit number Block Network Limit
server_version_string string Server Version String
fork_db_head_block_num number fork Database Block Height
fork_db_head_block_id string fork Database Block Address
server_full_version_string string Server Full Version String
earliest_available_block_num number Earliest Available Block Height
last_irreversible_block_time date Irreversible Block Time

get_code

Returns an object containing the smart contract wasm.

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

Parameters

Parameters Type Description
account_name string Contract name
code_as_wasm boolean True/false

Instance

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

Return Value

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

Retrieve the ABI data of an account.

1
dmc_client.rpc.get_abi_sync(account_name);

Parameter

Parameter Type Description
account_name string Account name

Instance

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

Return Value

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

Get block information.

1
dmc_client.rpc.get_block_sync(block_num_or_id);

Parameter

Parameter Type Description
block_num_or_id int Block number or ID

Instance

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

Return Value

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

Returns to the data from the specified table.

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

Parameters

Parameter Type Whether it is required or not Description
code string yes Contract name
table string yes Table name
scope string yes DMC account name
index_position string no Index position that to be used.
key_type string no Key type specified by index_position
encode_type string no Encoding type
lower_bound string no Lower bound
upper_bound string no Upper bound
limit integer no Number of returned results
reverse boolean no Whether to reverse the results
show_payer boolean no Whether to show the RAM payer

Instance

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)

Return Value

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 Module

Elliptic curve encryption function, built-in with multiple methods to generate the desired keys.

randomKey()

Generate a random private key.

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

Parameters

Parameter Type Whether it is required or not Description
cpuEntropyBits number no CPU entropy bits
options object no Configuration item

Return Value

Return a random private key.

Instance

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

privateToPublic()

Calculate the public key information from the private key information.

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

Parameters

Parameter Type Whether it is required or not Description
key string yes Private key
pubkey_prefix string no Public key prefix

Return Value

Return to public key.

Instance

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

isValidPublic()

Determine if the public key is valid.

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

Parameters

Parameter Type Whether it is required or not Description
pubkey string yes Public key
pubkey_prefix string no Public key prefix

Return Value

Check if the public key is valid.

Instance

1
2
3
4
5
var isValidPublic = DMC.ecc.isValidPublic("DM75N93rrsoBzEzrdZePZtkDUQsXiTWX51irsSBBukxB6Jg4nrua");
console.log(isValidPublic)
/*
Execution result:true
*/

isValidPrivate()

Check if the private key is valid.

1
DMC.ecc.isValidPrivate(wif);

Parameters

Parameter Type Whether it is required or not Description
wif string yes Private key

Return Value

Check if the private key is valid.

Instance

1
2
3
4
5
var isValidPrivate = DMC.ecc.isValidPrivate("5JJznTGghwYBCJoW6EcpzZfybKEDqFULMniatBp9aykeauP55bB");
console.log(isValidPrivate)
/*
Execution result:true
*/

sign()

Create a signature using the data information or hash.

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

Parameters

Parameter Type Whether it is required or not Description
data string yes Data
PrivateKey string yes Private key
encoding string no Data encoding

Return Value

String signature.

Instance

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

signHash()

Create a signature using the data information and private key for hash encryption.

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

Parameters

Parameter Type Whether it is required or not Description
dataSha256 string yes SHA256 hash of a 32-byte buffer or string
PrivateKey string yes Private key
encoding string no Encoding data with SHA256 (if it is a string)

Return Value

String signature with hash encryption.

Instance

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

verify()

Verify the signature data.

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

Parameters

Parameter Type Whether it is required or not Description
signature string yes Signature
data string yes Data
pubkey string yes Public key
encoding string no Data encoding
hashData boolean no Whether to hash encrypt the data

Return Value

Verify the validity of the signature data.

Instance

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

recover()

Recover the public key used to create the signature.

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

Parameters

Parameter Type Whether it is required or not Description
signature string yes Signature
data string yes Data
encoding string no Data encoding

Return Value

return to public key.

Instance

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

recoverHash()

Recover the public key used to create the hash signature.

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

Parameters

Parameter Type Whether it is required or not Description
signature string yes Signature
dataSha256 string yes SHA256 hash of a 32-byte buffer or string
encoding string no Encoding data with SHA256 (if it is a string)

Return Value

Return a public key.

Instance

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)
/*
Execution result:
DM75N93rrsoBzEzrdZePZtkDUQsXiTWX51irsSBBukxB6Jg4nrua
*/

sha256()

SHA256 encryption.

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

Parameters

Parameter Type Whether it is required or not Description
data string yes Binary data, requires Buffer.from(data, ‘hex’).
resultEncoding string no Encode the result as ‘hex’, ‘binary’, or ‘base64’ (optional, default is 'hex')
encoding string no Encode the data (if it is a string) as ‘hex’ (optional, default is 'hex')

Return Value

Use the data encrypted with hash.sha256

Instance

1
2
3
4
5
6
var dataSha256 = DMC.ecc.sha256("i am alive")
console.log(dataSha256)
/*
Execution result:
[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]
*/