Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot Build CLKey from Contract Hash #327

Closed
dylanireland opened this issue Jul 6, 2023 · 3 comments
Closed

Cannot Build CLKey from Contract Hash #327

dylanireland opened this issue Jul 6, 2023 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@dylanireland
Copy link
Contributor

dylanireland commented Jul 6, 2023

Using the casper-client, it is possible to give any hash value the type key, but the Casper JS SDK lacks the ability to build a CLKey object from any given hash.

For example, on line 7 in this shell script, a key argument is defined with the value "hash-b62481085812c4f56027f5fb7f3e5e2f087b14190da8762fb88889e4945178be".
This works properly. Here is a successful deploy as an example (expand the raw data and look under the first argument contract_hash).

Using the Casper JS SDK, however, it appears this cannot be done.

Traditionally, building a CLKey object is done by calling CLValueBuilder.key(), which accepts a parameter of type CLKeyParameters, which is any of the following types:

  • CLByteArray
  • CLURef
  • CLAccountHash
  • CLPublicKey

Reference: CLKey, CLValueBuilder.key, CLKeyParameters.

Converting the contract hash string above into any of these four types, and then into a CLKey using CLValueBuilder.key(CLKeyParameters) yields different errors, from the inability to unpack the deploy in the Casper Wallet to empty arguments when deployed.

Here is my attempted implementation.

My proposed solution is creating a new class CLContractHash and conforming it to CLKeyParameters.

@andrzej-casper
Copy link

Just looking how CLKey is parsed for contract hash:

if (value.isHash()) {
return Ok(
concat([
Uint8Array.from([KeyVariant.Hash]),
new CLByteArrayBytesParser()
.toBytes(value.data as CLByteArray)
.unwrap()
])
);

The idea is that regular byte array is prefixed with variant byte.

So as a workaround we can do it manually:

import { KeyVariant, CLByteArray, CLValueBuilder } from "casper-js-sdk";
import { concat } from '@ethersproject/bytes';

const contractHash = "cb781f66f78a398bf1709c4dac40b3cca17106824ea88ca9daa5b822421c9b57";
const clContractHash = new CLByteArray(
    concat([
        Uint8Array.from([KeyVariant.Hash]),
        Uint8Array.from(Buffer.from(contractHash, 'hex'))
    ])
);
const key = CLValueBuilder.key(clContractHash);

It works, but CLContractHash is definitely missing in the SDK.

@dylanireland
Copy link
Contributor Author

dylanireland commented Jul 6, 2023

Nice solution. In my case, adding Uint8Array.from([KeyVariant.Hash]) added another “01”. It appears to actually be as simple as:

const clContractHash = new CLByteArray(Uint8Array.from(Buffer.from(contractHash, "hex")));
const key = CLValueBuilder.key(clContractHash);

I had previously tried this, but was using TextEncoder instead of Buffer because I was in a React environment, and TextEncoder assumed the text was UTF-8 not hex. Instead, for React, one can actually just use decodeBase16 from Conversions like so:

import { decodeBase16, CLByteArray, CLValueBuilder } from "casper-js-sdk";

const contractHash = "cb781f66f78a398bf1709c4dac40b3cca17106824ea88ca9daa5b822421c9b57";
const clContractHash = new CLByteArray(decodeBase16(contractHash));
const key = CLValueBuilder.key(clContractHash);

Should still implement a CLContractHash though.

@hoffmannjan
Copy link
Contributor

hey guys, yeah we will add CLContractHash in upcoming release !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants
@hoffmannjan @dylanireland @andrzej-casper and others