Protokol
Search…
Complementary Examples
Scenarios Showing Transaction Creation And Broadcasting Best Practices
You can find runnable example in the following link
guardian/packages/guardian-examples at develop · protokol/guardian
GitHub

Prerequisites

Before we get started we need to make sure that all of the required dependencies are installed.
We recommend the usage of Yarn, but you can choose any other package manager you want!

yarn

1
yarn add @arkecosystem/crypto
2
yarn add @protokol/guardian-crypto
3
yarn add @protokol/client
Copied!

pnpm

1
pnpm add @arkecosystem/crypto
2
pnpm add @protokol/guardian-crypto
3
pnpm add @protokol/client
Copied!

npm

1
npm install @arkecosystem/crypto
2
npm install @protokol/guardian-crypto
3
npm install @protokol/client
Copied!

How To Create And Broadcast Guardian Transactions

GroupPermissions - Creating and Broadcasting

1
import { Identities, Managers, Transactions, Utils } from "@arkecosystem/crypto";
2
import { ProtokolConnection } from "@protokol/client";
3
import { Builders, Transactions as GuardianTransactions } from "@protokol/guardian-crypto";
4
5
export const GroupPermissions = async (): Promise<void> => {
6
// Configure our API client
7
const client = new ProtokolConnection("http://localhost:4003/api");
8
const passphrase = "clay harbor enemy utility margin pretty hub comic piece aerobic umbrella acquire";
9
10
// Configure manager and register transaction type
11
const configs = await client.api("node").crypto();
12
const {
13
body: {
14
data: {
15
block: { height },
16
},
17
},
18
} = await client.get("blockchain");
19
20
Managers.configManager.setConfig({ ...configs.body.data } as any);
21
Managers.configManager.setHeight(height);
22
Transactions.TransactionRegistry.registerTransactionType(GuardianTransactions.GuardianGroupPermissionsTransaction);
23
24
// Step 1: Retrieve the nonce of the sender wallet
25
const senderWallet = await client.api("wallets").get(Identities.Address.fromPassphrase(passphrase));
26
const senderNonce = Utils.BigNumber.make(senderWallet.body.data.nonce).plus(1);
27
28
// Step 2: Create the transaction
29
const transaction = new Builders.GuardianGroupPermissionsBuilder()
30
.GuardianGroupPermissions({
31
name: "Test Guardian Permission Group",
32
allow: [{ transactionType: 1, transactionTypeGroup: 1 }],
33
deny: [{ transactionType: 2, transactionTypeGroup: 1 }],
34
priority: 1,
35
active: true,
36
default: false,
37
})
38
.nonce(senderNonce.toFixed())
39
.sign(passphrase);
40
41
// Step 3: Broadcast the transaction
42
const broadcastResponse = await client.api("transactions").create({ transactions: [transaction.build().toJson()] });
43
44
// Step 4: Log the response
45
console.log(JSON.stringify(broadcastResponse.body, null, 4));
46
};
47
48
GroupPermissions()
49
.then(() => process.exit(0))
50
.catch((error) => {
51
console.error(error);
52
process.exit(1);
53
});
54
Copied!

UserPermissions - Creating and Broadcasting

1
import { Identities, Managers, Transactions, Utils } from "@arkecosystem/crypto";
2
import { ProtokolConnection } from "@protokol/client";
3
import { Builders, Transactions as GuardianTransactions } from "@protokol/guardian-crypto";
4
5
export const UserPermissions = async (): Promise<void> => {
6
// Configure our API client
7
const client = new ProtokolConnection("http://localhost:4003/api");
8
const passphrase = "clay harbor enemy utility margin pretty hub comic piece aerobic umbrella acquire";
9
10
// Configure manager and register transaction type
11
const configs = await client.api("node").crypto();
12
const {
13
body: {
14
data: {
15
block: { height },
16
},
17
},
18
} = await client.get("blockchain");
19
20
Managers.configManager.setConfig({ ...configs.body.data } as any);
21
Managers.configManager.setHeight(height);
22
Transactions.TransactionRegistry.registerTransactionType(GuardianTransactions.GuardianUserPermissionsTransaction);
23
24
// Step 1: Retrieve the nonce of the sender wallet
25
const senderWallet = await client.api("wallets").get(Identities.Address.fromPassphrase(passphrase));
26
const senderNonce = Utils.BigNumber.make(senderWallet.body.data.nonce).plus(1);
27
28
// Step 2: Create the transaction
29
const transaction = new Builders.GuardianUserPermissionsBuilder()
30
.GuardianUserPermissions({
31
groupNames: ["Test Guardian Permission Group"],
32
allow: [{ transactionTypeGroup: 1, transactionType: 2 }],
33
publicKey: Identities.PublicKey.fromPassphrase("This is banned public key passphrase"),
34
})
35
.nonce(senderNonce.toFixed())
36
.sign(passphrase);
37
38
// Step 3: Broadcast the transaction
39
const broadcastResponse = await client.api("transactions").create({ transactions: [transaction.build().toJson()] });
40
41
// Step 4: Log the response
42
console.log(JSON.stringify(broadcastResponse.body, null, 4));
43
};
44
45
UserPermissions()
46
.then(() => process.exit(0))
47
.catch((error) => {
48
console.error(error);
49
process.exit(1);
50
});
51
Copied!