/
declare.js
63 lines (58 loc) · 2.16 KB
/
declare.js
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
/* eslint-disable import/no-extraneous-dependencies */
/* eslint no-console:"off" */
/**
* This script demonstrates how to declare an identity on the blockchain
*
* In real world, identities may belong to different entities: application, user, node, device
*
* Run script with: `DEBUG=@arcblock/grpc-client node examples/declare_account.js`
*/
const Mcrypto = require('@arcblock/mcrypto');
const GRpcClient = require('@arcblock/grpc-client');
const { fromRandom, WalletType } = require('@arcblock/forge-wallet');
const endpoint = process.env.FORGE_API_HOST || 'http://127.0.0.1:8210'; // testnet
const client = new GRpcClient({ endpoint: 'tcp://127.0.0.1:28210' });
(async () => {
try {
// Send directly: GrpcClient will sign
const wallet = fromRandom();
let hash = await client.declare({
moniker: `grpc_user_${Math.round(Math.random() * 10000)}`,
wallet,
});
console.log('view account', `${endpoint}/node/explorer/accounts/${wallet.toAddress()}`);
console.log('view account tx', `${endpoint}/node/explorer/txs/${hash}`);
// Sign and then send: declare
const user2 = fromRandom();
const signed = await client.signDeclareTx({
tx: {
itx: {
moniker: 'sign_and_send',
},
},
wallet: user2,
});
hash = await client.sendDeclareTx({ tx: signed, wallet: user2 });
console.log('view user2 account', `${endpoint}/node/explorer/accounts/${user2.toAddress()}`);
console.log('view user2 tx', `${endpoint}/node/explorer/txs/${hash}`);
// Declare an application
const type = WalletType({
role: Mcrypto.types.RoleType.ROLE_APPLICATION,
pk: Mcrypto.types.KeyType.ED25519,
hash: Mcrypto.types.HashType.SHA3,
});
const application = fromRandom(type);
hash = await client.declare({
moniker: `application_${Math.round(Math.random() * 10000)}`,
wallet,
});
console.log(
'view application',
`${endpoint}/node/explorer/accounts/${application.toAddress()}`
);
console.log('view application tx', `${endpoint}/node/explorer/txs/${hash}`);
} catch (err) {
console.error(err);
console.log(JSON.stringify(err.errors));
}
})();