-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcip-1852-keys.js
113 lines (89 loc) · 4.3 KB
/
cip-1852-keys.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
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
import { entropyToMnemonic } from 'bip39';
import { Buffer } from 'buffer';
import {
Bip32PrivateKey,
BaseAddress,
RewardAddress,
Credential
} from "@emurgo/cardano-serialization-lib-nodejs";
// Constants
const entropy = '00000000000000000000000000000000';
const mnemonic = entropyToMnemonic(entropy);
const accountIndex = 0;
const keyIndex = 0;
const networkTag = 0;
// ########### Keys ###########
// Following CIP-1852
// Generate root key
const rootKey = Bip32PrivateKey.from_bip39_entropy(
Buffer.from(entropy, 'hex'),
Buffer.from('')
);
// Helper function to harden a number
const harden = (num) => {
return 0x80000000 + num;
};
// Derive account level key, according to CIP-1852
const accountKey = rootKey.derive(harden(1852)) // purpose
.derive(harden(1815)) // coin type
.derive(harden(parseInt(accountIndex))); // account
// Derive address level keys
// Private keys
// derive role and address index
const paymentPrivKey = accountKey.derive(0).derive(keyIndex).to_raw_key();
const stakePrivKey = accountKey.derive(2).derive(keyIndex).to_raw_key();
const dRepPrivKey = accountKey.derive(3).derive(keyIndex).to_raw_key();
const ccColdPrivKey = accountKey.derive(4).derive(keyIndex).to_raw_key();
const ccHotPrivKey = accountKey.derive(5).derive(keyIndex).to_raw_key();
// Create public keys from private keys
const paymentPubKey = paymentPrivKey.to_public();
const stakePubKey = stakePrivKey.to_public();
const dRepPubKey = dRepPrivKey.to_public();
const ccColdPubKey = ccColdPrivKey.to_public();
const ccHotPubKey = ccHotPrivKey.to_public();
// Create data from keys
// Credentials
const paymentCredential = Credential.from_keyhash(paymentPubKey.hash());
const stakeCredential = Credential.from_keyhash(stakePubKey.hash());
// Payment address
const baseAddress = BaseAddress.new(networkTag, paymentCredential, stakeCredential);
const paymentAddressBech32 = baseAddress.to_address().to_bech32();
const paymentAddressHex = baseAddress.to_address().to_hex();
// Stake/ Rewards address
// Described via CIP-11 (https://github.com/cardano-foundation/CIPs/tree/master/CIP-0011)
// Bech32 encoding prefix defined via CIP-05 (https://github.com/cardano-foundation/CIPs/tree/master/CIP-0005)
const stakeAddressBech32 = (RewardAddress.new(networkTag, stakeCredential)).to_address().to_bech32();
const stakeAddressHex =(RewardAddress.new(networkTag, stakeCredential)).to_address().to_hex();
// DRep ID
// Described via CIP-105 (https://github.com/cardano-foundation/CIPs/tree/master/CIP-0105)
// Bech32 encoding prefix defined via CIP-05 (https://github.com/cardano-foundation/CIPs/tree/master/CIP-0005)
const dRepIdBech32 = dRepPubKey.hash().to_bech32('drep');
const dRepIdHex = dRepPubKey.hash().to_hex();
console.log('\n=== CIP-1852 Keys ===');
console.log('From mnemonic:', mnemonic);
console.log('For account index:', accountIndex);
console.log('\n> Payment keys');
console.log('Payment private key (hex):', paymentPrivKey.to_hex());
console.log('Payment public key (hex):', paymentPubKey.to_hex());
console.log('\n> Stake keys');
console.log('Stake private key (hex):', stakePrivKey.to_hex());
console.log('Stake public key (hex):', stakePubKey.to_hex());
console.log('\n> DRep keys');
console.log('DRep private key (hex):', dRepPrivKey.to_hex());
console.log('DRep public key (hex):', dRepPubKey.to_hex());
console.log('\n> Constitutional Committee Cold keys');
console.log('CC cold private key (hex):', ccColdPrivKey.to_hex());
console.log('CC cold public key (hex):', ccColdPubKey.to_hex());
console.log('\n> Constitutional Committee Hot keys');
console.log('CC hot private key (hex):', ccHotPrivKey.to_hex());
console.log('CC hot public key (hex):', ccHotPubKey.to_hex());
console.log('\n=== From keys create associated data ===');
console.log('\n> Payment Address (network tag + payment pub key hash + stake pub key hash)');
console.log('Payment Address (Bech32 encoded):', paymentAddressBech32);
console.log('Payment Address (Hex):', paymentAddressHex)
console.log('\n> Stake (rewards) Address (network tag + stake pub key hash)');
console.log('Stake Address (Bech32 encoded):', stakeAddressBech32);
console.log('Stake Address (Hex):', stakeAddressHex);
console.log('\n> DRep ID (DRep key hash)');
console.log('DRep ID (Bech32 encoded):', dRepIdBech32);
console.log('DRep ID (Hex):', dRepIdHex);