Skip to content

Commit

Permalink
Merge pull request #1 from Ryun1/feat/clean-1852
Browse files Browse the repository at this point in the history
feat: tidy and add CIP1852 examples
  • Loading branch information
Ryun1 authored May 21, 2024
2 parents 04c8901 + 8ed2f6f commit 6dcc3d4
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 49 deletions.
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
# CSL Examples Repo
# CSL Javascript Examples Repo

This repository contains example implementations of Cardano specific logic, using the Javascript libraries of [Cardano Serialization Lib](https://github.com/Emurgo/cardano-serialization-lib).

CSL Version: `12.0.0-alpha.29`

## Examples

### [CIP-1852 Key Derivation](./examples/cip-1852-keys.js)

In this example, we show [CIP-1852 HD derivation](https://github.com/cardano-foundation/CIPs/tree/master/CIP-1852) of all keys.
Further, we show how these keys can be used to create addresses and DRep IDs.

### [CIP-08 Message Signing](./examples/cip-0008-signing.js)


### [CIP-105 Conway Era Keys](./examples/cip-0105-conway-keys.js)


## To run examples

Clone the repository.

```shell
git clone
```

Navigate to directory.

```shell
cd csl-js-examples
```

Run a example.

```shell
node examples/cip-1852-keys.js
```
106 changes: 106 additions & 0 deletions examples/cip-1852-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
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 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(0).to_raw_key();
const stakePrivKey = accountKey.derive(2).derive(0).to_raw_key();
const dRepPrivKey = accountKey.derive(3).derive(0).to_raw_key();
const ccColdPrivKey = accountKey.derive(4).derive(0).to_raw_key();
const ccHotPrivKey = accountKey.derive(5).derive(0).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();

// 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();

// 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');

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('\n> Stake (rewards) Address (network tag + stake pub key hash)');
console.log('Stake Address (Bech32 encoded):', stakeAddressBech32);

console.log('\n> DRep ID (DRep key hash)');
console.log('DRep ID (Bech32 encoded):', dRepIdBech32);
48 changes: 0 additions & 48 deletions keys.js

This file was deleted.

11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"license": "ISC",
"type": "module",
"dependencies": {
"@emurgo/cardano-message-signing-nodejs": "^1.0.1",
"@emurgo/cardano-serialization-lib-nodejs": "^12.0.0-alpha.19",
"bip39": "^3.1.0"
}
Expand Down

0 comments on commit 6dcc3d4

Please sign in to comment.