CSL and Ogmios
Cardano Serialization Lib and Cardano Ogmios in Deno
Get Cardano Bech32 Address from key returned by Ogmios
Ogmios 6.7.0 and CSL 12.1.0 with Deno 1.45.2
import {
PublicKey,
Credential,
EnterpriseAddress,
NetworkInfo,
} from "@emurgo/cardano-serialization-lib-nodejs";
// Key used to sign the TX
// From Address: addr_test1vqfe3zd3ppwpteuutqxc7g4fxk65tr5x0dv2pdkg0swvmlqpsvuxw
// From PKEY: ed25519_pk1majw0h3wtwf8fhyc90eh36ya357nln7w0q0lyetj2sg8pvnnqymsxa97tk
// Key stored on chain (from ogmios block in the signatories object)
const key = "df64e7de2e5b9274dc982bf378e89d8d3d3fcfce781ff26572541070b2730137";
// Trying to reserve (simulating not having who signed what to get back the original address)
console.log(
"1: ",
PublicKey.from_bech32(
"ed25519_pk1majw0h3wtwf8fhyc90eh36ya357nln7w0q0lyetj2sg8pvnnqymsxa97tk",
).to_hex(),
);
console.log("2: ", PublicKey.from_hex(key).to_bech32()); // This one returns the expected value.
console.log(
"4: ",
EnterpriseAddress.new(
NetworkInfo.testnet_preprod().network_id(),
Credential.from_keyhash(PublicKey.from_hex(key).hash()),
)
.to_address()
.to_bech32(),
); // reverse the key stored in ogmios to get back a bech32 address
// Output
// 1: df64e7de2e5b9274dc982bf378e89d8d3d3fcfce781ff26572541070b2730137
// 2: ed25519_pk1majw0h3wtwf8fhyc90eh36ya357nln7w0q0lyetj2sg8pvnnqymsxa97tk
// 4: addr_test1vqfe3zd3ppwpteuutqxc7g4fxk65tr5x0dv2pdkg0swvmlqpsvuxw
The `key` is fetched from an ogmios block within a transaction.
The goal is to try to find out who signed the Tx. So using CSL it can be easily achieved.
First step is using the PublicKey to get a KeyHash Instance and from that Hash we can create a Credential Instance.
Then to recreate the Address, we simply need to use the EnterpriseAddress (or the BaseAddress if you have a StakeKey) to print the original address in bech32 format (I have the code for the BaseAddress in the cardano repository).
Want to learn more?
I have the code in the cardano-indexer repository (for the ogmios part) and in the cardano repository for the address manipulation.
Last updated
Was this helpful?