cardano-serialization-lib icon indicating copy to clipboard operation
cardano-serialization-lib copied to clipboard

What's the equivalent to `cardano-cli address build`?

Open MartinSchere opened this issue 4 years ago • 7 comments

I'm trying to get the address of a script (which should be its hash), but there is no hash_script function available. How can I convert a CBOR hex representing a script to its address?

MartinSchere avatar Oct 28 '21 01:10 MartinSchere

Maybe I'm misunderstanding it, but does NativeScript::hash() help you?

e.g. (the below code is for the rust lib not the wasm bindings)

let script = NativeScript::from_bytes(hex::decode("cbor hex bytes go here")?)?;
let hash = script.hash(ScriptHashNamespace::NativeScript);
// I am just noticing now as I write this that I think these are the wrong types...
// so a workaround could be to just re-read the bytes like this?
let scripthash = ScriptHash::from_bytes(hash.to_bytes())?;
let cred = StakeCredential::from_scripthash(&scripthash);
// make the address from the hash (not sure which type you want, e.g. enterprise, base, etc) e.g.:
let addr = BaseAddress::new(0, &cred, &cred);

or use cred in whichever way to make your desired Address type.

I just put up a PR about this type mismatch: #234 but until then that byte-reinterpretation should work I think. Both types are wrappers around blake2b224 hashes so it's more an issue of type names than structure.

rooooooooob avatar Oct 29 '21 04:10 rooooooooob

Thanks a lot for your answer. However, I think it's not NativeScript but rather PlutusScript. I tried hashing it manually with blake2 for nodejs:

const scriptAddr = Address.from_bech32(readFileSync(filePath, 'utf-8')); // readFile just reads what the CLI generated for that script
const h = blake2.createHash('blake2b', { digestLength: 28 });
h.update(Buffer.from('01' + scriptCBOR, 'hex')); // scriptCBOR is a CLI-generated CBOR string representing the script. Prefix with 01 to indicate plutusScriptv1

console.log('BLAKE GENERATED: ', '70' + h.digest('hex')); // 7083be31c91c564a15d63f670c4f3d4444d5bfbbfb4f6440ba306964f1

// Generated by the CLI (correct script address)
console.log('SCRIPT ADDRESS: ', toHexString(scriptAddr.to_bytes())); // 70865ce3fbdff7aafea3093f3f2902b7d486c4946e1f716d73722f60ea

But no matches. I'm on the right path with the 70 prefix tho

MartinSchere avatar Oct 29 '21 20:10 MartinSchere

I had a go at this, and can't get the addresses to match between what is generated by cardano-cli and the serialization lib

// cborhex of the alwayssucceeds.plutus
const cborhex = "4E4D01000033222220051200120011";
const cbor = Buffer.from(cborhex, "hex");
const blake2bhash = blake.blake2b(cbor, 0, 28); //blake2b-224 -> 28 bytes
const scripthash = ScriptHash.from_bytes(blake2bhash);
const cred = StakeCredential.from_scripthash(scripthash);
const networkId = NetworkInfo.testnet().network_id();
const baseAddr = EnterpriseAddress.new(networkId, cred);
const addr = baseAddr.to_address();
const addrBech32 = addr.to_bech32();

// hash of the address generated from script
console.log(Buffer.from(addr.to_bytes(), "utf8").toString("hex"))

// hash of the address generated using cardano-cli
const ScriptAddress = Address.from_bech32("addr_test1wpnlxv2xv9a9ucvnvzqakwepzl9ltx7jzgm53av2e9ncv4sysemm8");
console.log(Buffer.from(ScriptAddress.to_bytes(), "utf8").toString("hex"))

And the hex outputs are: geneated from cborhex: 70d4e0f6e402a19e086f2fb9914124604ece088db0fa5906fb0f66fc07 generated by cardano-cli: 7067f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656

The corresponding bech32 addresses are: geneated from cborhex: addr_test1wpnlxv2xv9a9ucvnvzqakwepzl9ltx7jzgm53av2e9ncv4sysemm8 generated by cardano-cli: addr_test1wr2wpahyq2seuzr097uezsfyvp8vuzydkra9jphmpan0cpczfflxx

The blake package is npm install blakejs

dshibaev avatar Feb 20 '22 01:02 dshibaev

You can now load the script CBOR into a PlutusScript instance and then use hash_plutus_script

MartinSchere avatar Feb 21 '22 16:02 MartinSchere

@MartinSchere unfortunately there is no hash_plutus_script function I am using v10

dshibaev avatar Feb 21 '22 16:02 dshibaev

oops, sorry, I confused this with another function

MartinSchere avatar Feb 21 '22 16:02 MartinSchere

There is a workaround that I found which generates the blake2bhash when running the cabal run on the plutus script and from that I can derive the Address bech32 as per my steps above. This is not ideal however

dshibaev avatar Feb 21 '22 16:02 dshibaev