forked from cardano-foundation/cardano-rosetta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool-retirement-example.ts
116 lines (102 loc) · 3.25 KB
/
pool-retirement-example.ts
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* eslint-disable no-magic-numbers */
/* eslint-disable camelcase */
/* eslint-disable new-cap */
/* eslint-disable no-console */
import {
buildDelegationOperation,
constructionDerive,
constructionPreprocess,
constructionMetadata,
constructionPayloads,
constructionCombine,
constructionSubmit,
generateKeys,
signPayloads,
waitForBalanceToBe,
buildOperation,
} from "./commons";
const logger = console;
const PRIVATE_KEY =
"41d9523b87b9bd89a4d07c9b957ae68a7472d8145d7956a692df1a8ad91957a2c117d9dd874447f47306f50a650f1e08bf4bec2cfcb2af91660f23f2db912977";
const SEND_FUNDS_ADDRESS =
"addr_test1qqr585tvlc7ylnqvz8pyqwauzrdu0mxag3m7q56grgmgu7sxu2hyfhlkwuxupa9d5085eunq2qywy7hvmvej456flknswgndm3";
// cold keys
const coldKeys = {
secretKey: Buffer.from(
"45ad0a9123f966e5d584140c1fe49d8f8430ee9f55c8c4177bdf632191dca496cb18b6cb54eb79376134cd6c19f8d4bf2f9dfe1503d53547c4f8800897b50d3a",
"hex"
),
publicKey: Buffer.from(
"cb18b6cb54eb79376134cd6c19f8d4bf2f9dfe1503d53547c4f8800897b50d3a",
"hex"
),
};
const POOL_KEY_HASH =
"1677d50dcecc49c58bdad62cf2ad9bef6e7adb8a722665c11a0cfec2";
const buildPoolRetirementOperation = (
currentIndex: number,
poolKeyHash: string
) => ({
operation_identifier: { index: currentIndex + 1 },
type: "poolRetirement",
status: "success",
account: { address: poolKeyHash },
metadata: {
epoch: 135
},
});
const doRun = async (): Promise<void> => {
const keyAddressMapper = {};
keyAddressMapper[POOL_KEY_HASH] = coldKeys;
const paymentKeys = generateKeys(PRIVATE_KEY);
logger.info(
`[doRun] secretKey ${Buffer.from(paymentKeys.secretKey).toString("hex")}`
);
const paymentPublicKey = Buffer.from(paymentKeys.publicKey).toString("hex");
const paymentAddress = await constructionDerive(paymentPublicKey);
keyAddressMapper[paymentAddress] = paymentKeys;
const { unspents, balances } = await waitForBalanceToBe(
paymentAddress,
(response) => response.coins.length !== 0
);
const builtOperations = buildOperation(
unspents,
balances,
paymentAddress,
SEND_FUNDS_ADDRESS,
true,
45
);
const currentIndex = builtOperations.operations.length - 1;
const builtPoolRetirementOperation = buildPoolRetirementOperation(
currentIndex + 1,
POOL_KEY_HASH
);
builtOperations.operations.push(builtPoolRetirementOperation);
logger.info(
`[doRun] operations to be sent are ${JSON.stringify(
builtOperations.operations
)}`
);
const preprocess = await constructionPreprocess(
builtOperations.operations,
1000
);
const metadata = await constructionMetadata(preprocess);
const payloads = await constructionPayloads({
operations: builtOperations.operations,
metadata,
});
const signatures = signPayloads(payloads.payloads, keyAddressMapper);
const combined = await constructionCombine(
payloads.unsigned_transaction,
signatures
);
logger.info(`[doRun] signed transaction is ${combined.signed_transaction}`);
const hashResponse = await constructionSubmit(combined.signed_transaction);
const transactionHash = hashResponse.transaction_identifier.hash;
logger.info(`[doRun] transaction with hash ${transactionHash} sent`);
};
doRun()
.then(() => logger.info("Pool Registration finished"))
.catch(console.error);