Skip to content

Commit

Permalink
feat: init message signing example
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryun1 committed Sep 14, 2024
1 parent cedd979 commit f97c430
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ Further, we show how these keys can be used to create addresses and DRep IDs.

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

In this example, we show an implementation of [CIP-0008 Message Signing](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0008).

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

todo

## To run examples

Expand Down
162 changes: 162 additions & 0 deletions examples/cip-0008-signing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import { entropyToMnemonic } from 'bip39';
import { Buffer } from 'buffer';
import {
Bip32PrivateKey,
Address,
Credential,
make_vkey_witness,
TransactionHash,
EnterpriseAddress,
} from "@emurgo/cardano-serialization-lib-nodejs";
import {
HeaderMap,
Label,
AlgorithmId,
CBORValue,
ProtectedHeaderMap,
COSESign1Builder,
COSEKey,
KeyType,
BigNum,
Int,
Headers,
} from "@emurgo/cardano-message-signing-nodejs"

// Constants
const entropy = '00000000000000000000000000000000';
const mnemonic = entropyToMnemonic(entropy);
const accountIndex = 0;
const messageToSign = 'cc4ab8ead604ddb498ed4b2916af7b454c65ac783b5d836fddf388e72a40eccb'

// ########### Keys ###########

// Following CIP-1852

// Generate root key
const rootKey = Bip32PrivateKey.from_bip39_entropy(
Buffer.from(entropy, 'hex'),
Buffer.from('')
);

// Derive account key
const harden = (num) => {
return 0x80000000 + num;
};

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);
const ccHotPrivKey = accountKey.derive(5).derive(0);
// Public 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();

console.log('\n=== CC COLD KEY DETAILS ===')

const coldChaincode = ccColdPrivKey.chaincode()

console.log('chainCode', Buffer.from(coldChaincode).toString('hex'))
console.log('ccColdKey', Buffer.from(ccColdPrivKey.to_128_xprv()).toString('hex'))
// console.log('ccColdPubKey', ccColdPubKey.to_hex())
// console.log('ccColdPubKeyHash', ccColdPubKey.hash().to_hex())

console.log('=== CC HOT KEY DETAILS ===')

const hotChaincode = ccHotPrivKey.chaincode()

console.log('chainCode', Buffer.from(hotChaincode).toString('hex'))
console.log('ccHotKey', Buffer.from(ccHotPrivKey.to_128_xprv()).toString('hex'))
// console.log('ccHotPubKey', ccHotPubKey.to_hex())
// console.log('ccHotPubKeyHash', ccHotPubKey.hash().to_hex())

// ########### Witnesses ###########

// const ed25519Signature = paymentPrivKey.sign(Buffer.from(messageToSign, 'hex'));

// // cip-08

// const payload = messageToSign;
// const addressHex = (EnterpriseAddress.new(1, Credential.from_keyhash(paymentPubKey.hash()))).to_address().to_hex();
// const publicKey = paymentPubKey;
// const paymentAccountKey = paymentPrivKey;

// const protectedHeaders = HeaderMap.new();
// protectedHeaders.set_algorithm_id(
// Label.from_algorithm_id(AlgorithmId.EdDSA)
// );

// protectedHeaders.set_header(
// Label.new_text('address'),
// CBORValue.new_bytes(Buffer.from(addressHex, 'hex'))
// );

// const protectedSerialized =
// ProtectedHeaderMap.new(protectedHeaders);
// const unprotectedHeaders = HeaderMap.new();
// const headers = Headers.new(
// protectedSerialized,
// unprotectedHeaders
// );
// const builder = COSESign1Builder.new(
// headers,
// Buffer.from(payload, 'hex'),
// false
// );

// const toSign = builder.make_data_to_sign().to_bytes();
// const signedSigStruc = paymentAccountKey.sign(toSign).to_bytes();
// const coseSign1 = builder.build(signedSigStruc);

// const key = COSEKey.new(
// Label.from_key_type(KeyType.OKP)
// );

// key.set_algorithm_id(
// Label.from_algorithm_id(AlgorithmId.EdDSA)
// );

// key.set_header(
// Label.new_int(
// Int.new_negative(BigNum.from_str('1'))
// ),
// CBORValue.new_int(
// Int.new_i32(6)
// )
// );
// key.set_header(
// Label.new_int(
// Int.new_negative(BigNum.from_str('2'))
// ),
// CBORValue.new_bytes(publicKey.as_bytes())
// );

// const coseSig = Buffer.from(coseSign1.to_bytes()).toString('hex')

// // ########### Logs ###########

// console.log('keys')
// console.log('Payment private key:', paymentPrivKey.to_hex());
// console.log('Payment public key:', paymentPubKey.to_hex());
// console.log('Payment public key hash:', paymentPubKey.hash().to_hex());
// console.log('Payment Address:', addressHex);

// console.log('\nSignatures')
// console.log('ed25519Signature:', ed25519Signature.to_hex());
// console.log('coseSig:', coseSig);

// console.log("mnemonic:", mnemonic);


0 comments on commit f97c430

Please sign in to comment.