Skip to content

Commit

Permalink
Merge pull request #449 from zajko/bringing_back_call_by_hash
Browse files Browse the repository at this point in the history
Making it possible to specify contract by hash and name.
  • Loading branch information
zajko authored Jul 25, 2024
2 parents 2ef6cbe + e113b6a commit 1124678
Show file tree
Hide file tree
Showing 17 changed files with 198 additions and 153 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Removed
-->

## [3.0.0-rc04] - 2024-07-25

### Added

- Brought back the functionality of setting contract hash (`setContractHash` method) in `Contract` class. Previously even using that method would ignore the contract hash due to a bug in the node. Now setting it will imply that the contract endpoints need to by called by contract hash. You still can use `setContractName`, but `setContractHash` takes priority if both are used.
- Added the possibility to muffle deprecation warnings in `CasperServiceByJsonRPC`. You can now pass `muffleDeprecationWarnings` as an optional parameter to the constructor. If set to `true`, the SDK will not log any deprecation warnings from that class. Defaults to `false`

### Removed

- `TransactionSessionKind` since field kind was removed from `Session` implementation of `TransactionTarget`
- Removed usages of `toHex`, `fromHex` and `toAccountHashStr` since they are deprecated.

### Changed

- `waitForDeploy` and `waitForTransaction` methods will fail fast if the results for getting deploy/transaction info is present but was not successfull.

## [3.0.0-rc02] - 2024-07-05

This release candidate is compatible with [#node RC3](https://github.com/casper-network/casper-node/tree/release-2.0.0-rc2)
Expand Down
60 changes: 29 additions & 31 deletions e2e/services/CasperServiceByJsonRPC.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ import {
TransactionCategoryMint,
makeV1Transaction
} from '../../src/lib/TransactionUtil';
import {
Native,
Session,
Stored,
TransactionSessionKind
} from '../../src/lib/TransactionTarget';
import { Native, Session, Stored } from '../../src/lib/TransactionTarget';
import { Call, Custom, Transfer } from '../../src/lib/TransactionEntryPoint';
import { Standard } from '../../src/lib/TransactionScheduling';
import { InitiatorAddr } from '../../src/lib/InitiatorAddr';
Expand Down Expand Up @@ -240,7 +235,7 @@ describe('CasperServiceByJsonRPC', () => {
const faucetBalance = '1000000000000000000000000000000000';
const stateRootHash = await client.getStateRootHash();
const entity_identifier = {
PublicKey: faucetKey.publicKey.toHex(false)
PublicKey: faucetKey.publicKey.toFormattedString(false)
};
const entity = await client.getEntity(entity_identifier);
const main_purse = entity.AddressableEntity.entity.main_purse;
Expand All @@ -251,17 +246,17 @@ describe('CasperServiceByJsonRPC', () => {
it('query_balance', async () => {
const balanceByPublicKey = await client.queryBalance(
PurseIdentifier.MainPurseUnderPublicKey,
faucetKey.publicKey.toHex(false)
faucetKey.publicKey.toFormattedString(false)
);

const balanceByAccountHash = await client.queryBalance(
PurseIdentifier.MainPurseUnderAccountHash,
faucetKey.publicKey.toAccountHashStr()
faucetKey.publicKey.toAccountHash().toFormattedString()
);
expect(balanceByAccountHash.eq(balanceByPublicKey)).to.be;

const entity = await client.getEntity({
PublicKey: faucetKey.publicKey.toHex(false)
PublicKey: faucetKey.publicKey.toFormattedString(false)
});

const balanceByUref = await client.queryBalance(
Expand Down Expand Up @@ -310,7 +305,7 @@ describe('CasperServiceByJsonRPC', () => {
await client.transaction(signedTransaction);
await sleep(2500);
const result = await client.waitForTransaction(signedTransaction, 100000);
if (!result) {
if (!result || !client.isTransactionSuccessfull(result)) {
assert.fail('Transfer deploy failed');
}
expect(encodeBase16(signedTransaction.Version1!.hash)).to.be.equal(
Expand All @@ -324,7 +319,7 @@ describe('CasperServiceByJsonRPC', () => {

const balance = await client.queryBalance(
PurseIdentifier.MainPurseUnderPublicKey,
toPublicKey.toHex(false)
toPublicKey.toFormattedString(false)
);

expect('' + paymentAmount).to.be.equal(balance.toString());
Expand Down Expand Up @@ -356,7 +351,7 @@ describe('CasperServiceByJsonRPC', () => {
await sleep(2500);

const result = await client.waitForDeploy(signedDeploy, 100000);
if (!result) {
if (!result || !client.isDeploySuccessfull(result)) {
assert.fail('Transfer deploy failed');
}
expect(deploy_hash).to.be.equal(result.deploy.hash);
Expand All @@ -369,7 +364,7 @@ describe('CasperServiceByJsonRPC', () => {

const balance = await client.queryBalance(
PurseIdentifier.MainPurseUnderPublicKey,
toPublicKey.toHex(false)
toPublicKey.toFormattedString(false)
);

expect(amount).to.be.equal(balance.toString());
Expand Down Expand Up @@ -403,9 +398,12 @@ describe('CasperServiceByJsonRPC', () => {

await client.deploy(signedDeploy);
await sleep(2500);
await client.waitForDeploy(signedDeploy, 100000);
let res = await client.waitForDeploy(signedDeploy, 100000);
if (!res || !client.isDeploySuccessfull(res)) {
assert.fail('Transfer deploy failed');
}
const entity_identifier = {
AccountHash: faucetKey.publicKey.toAccountHashStr()
AccountHash: faucetKey.publicKey.toAccountHash().toFormattedString()
};
const { AddressableEntity } = await client.getEntity(entity_identifier);
const named_key = AddressableEntity!.named_keys.find((i: NamedKey) => {
Expand Down Expand Up @@ -448,9 +446,11 @@ describe('CasperServiceByJsonRPC', () => {
await sleep(2500);

let result = await client.waitForDeploy(signedDeploy, 100000);

if (!result || !client.isDeploySuccessfull(result)) {
assert.fail('Transfer deploy failed');
}
const entity_identifier = {
AccountHash: faucetKey.publicKey.toAccountHashStr()
AccountHash: faucetKey.publicKey.toAccountHash().toFormattedString()
};
const { AddressableEntity } = await client.getEntity(entity_identifier);
const contractHash = AddressableEntity!.named_keys.find((i: NamedKey) => {
Expand All @@ -460,7 +460,6 @@ describe('CasperServiceByJsonRPC', () => {
assert.exists(contractHash);

cep18.setContractHash(contractHash!);
cep18.setContractName(`cep18_contract_hash_${tokenName}`);
const fetchedTokenName = await cep18.queryContractData(['name']);
const fetchedTokenSymbol = await cep18.queryContractData(['symbol']);
const fetchedTokenDecimals: BigNumber = await cep18.queryContractData([
Expand Down Expand Up @@ -510,9 +509,11 @@ describe('CasperServiceByJsonRPC', () => {
const { deploy_hash } = await client.deploy(transferDeploy);
await sleep(2500);
result = await client.waitForDeploy(transferDeploy, 100000);

if (!result || !client.isDeploySuccessfull(result)) {
assert.fail('Transfer deploy failed');
}
assert.equal(result.deploy.hash, deploy_hash);
expect(result.deploy.session).to.have.property('StoredContractByName');
expect(result.deploy.session).to.have.property('StoredContractByHash');
expect(result.execution_info!.execution_result!).to.have.property(
'Version2'
);
Expand All @@ -526,7 +527,7 @@ describe('CasperServiceByJsonRPC', () => {
});

//This needs to wait for a fix in the node which currently prevents wasm transactions
xit('CEP18 should work deployed via "account_put_transaction"', async () => {
it('CEP18 should work deployed via "account_put_transaction"', async () => {
const casperClient = new CasperClient(NODE_URL);
const cep18 = new Contract(casperClient);
const wasmPath = path.resolve(__dirname, './cep18.wasm');
Expand Down Expand Up @@ -556,26 +557,21 @@ describe('CasperServiceByJsonRPC', () => {
const transaction = makeV1Transaction(
params,
runtimeArgs,
Session.build(
TransactionSessionKind.Installer,
wasm,
TransactionRuntime.VmCasperV1
),
Session.build(wasm, TransactionRuntime.VmCasperV1),
new Call(),
new Standard(),
TransactionCategoryInstallUpgrade
);
const signedTransaction = transaction.sign([faucetKey]);
await client.transaction(signedTransaction);

await sleep(2500);

const result = await client.waitForTransaction(signedTransaction, 100000);
if (!result) {
if (!result || !client.isTransactionSuccessfull(result)) {
assert.fail('Deploy failed');
}
const entity_identifier = {
AccountHash: faucetKey.publicKey.toAccountHashStr()
AccountHash: faucetKey.publicKey.toAccountHash().toFormattedString()
};
const { AddressableEntity } = await client.getEntity(entity_identifier);
const contractHash = AddressableEntity!.named_keys.find((i: NamedKey) => {
Expand All @@ -585,7 +581,6 @@ describe('CasperServiceByJsonRPC', () => {
assert.exists(contractHash);

cep18.setContractHash(contractHash!);
cep18.setContractName(`cep18_contract_hash_${tokenName}`);
const fetchedTokenName = await cep18.queryContractData(['name']);
const fetchedTokenSymbol = await cep18.queryContractData(['symbol']);
const fetchedTokenDecimals: BigNumber = await cep18.queryContractData([
Expand Down Expand Up @@ -644,6 +639,9 @@ describe('CasperServiceByJsonRPC', () => {
signedRunEndpointTransaction,
100000
);
if (!transferResult || !client.isTransactionSuccessfull(transferResult)) {
assert.fail('Deploy failed');
}
assert.equal(
transferResult.transaction.Version1.hash,
transaction_hash.Version1!
Expand Down
37 changes: 21 additions & 16 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "casper-js-sdk",
"version": "3.0.0-rc03",
"version": "3.0.0-rc04",
"license": "Apache 2.0",
"description": "SDK to interact with the Casper blockchain",
"homepage": "https://github.com/casper-ecosystem/casper-js-sdk#README.md",
Expand Down Expand Up @@ -109,7 +109,7 @@
"webpack-node-externals": "^2.5.2"
},
"dependencies": {
"@ethersproject/bignumber": "^5.0.8",
"@ethersproject/bignumber": "^5.7.0",
"@ethersproject/bytes": "^5.0.5",
"@ethersproject/constants": "^5.0.5",
"@noble/curves": "^1.1.0",
Expand All @@ -130,4 +130,4 @@
"ts-results": "npm:@casperlabs/ts-results@^3.3.4",
"typedjson": "^1.6.0-rc2"
}
}
}
2 changes: 1 addition & 1 deletion run_e2e_locally.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export FAUCET_PRIV_KEY="MC4CAQAwBQYDK2VwBCIEIHXch+pMtOX6MqKQHT+yLnflYxl5XytTYZtYuItlko4g"
export FAUCET_PRIV_KEY=`cat /home/casperlabs-dev/DEV/src/casper-nctl/assets/net-1/users/user-1/secret_key.pem | sed -n '2 p'`
export NODE_URL="http://127.0.0.1:7777/rpc"
export HTTP_EVENT_STREAM_URL="http://127.0.0.1:19999/events"
export HTTPS_EVENT_STREAM_URL="https://events.mainnet.casperlabs.io/events"
Expand Down
4 changes: 2 additions & 2 deletions src/lib/ByteConverters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,13 +579,13 @@ describe(`numbers' toBytes`, () => {
it('should compute hex from PublicKey correctly', () => {
const ed25519Account = Keys.Ed25519.new();
const ed25519AccountHex = ed25519Account.accountHex();
expect(CLPublicKey.fromHex(ed25519AccountHex)).to.deep.equal(
expect(CLPublicKey.fromFormattedString(ed25519AccountHex)).to.deep.equal(
ed25519Account.publicKey
);

const secp256K1Account = Keys.Secp256K1.new();
const secp256K1AccountHex = secp256K1Account.accountHex();
expect(CLPublicKey.fromHex(secp256K1AccountHex)).to.deep.equal(
expect(CLPublicKey.fromFormattedString(secp256K1AccountHex)).to.deep.equal(
secp256K1Account.publicKey
);
});
Expand Down
24 changes: 12 additions & 12 deletions src/lib/CLValue/PublicKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,23 @@ describe('CLPublicKey', () => {
const ed25519Account = Keys.Ed25519.new();
const ed25519AccountHex = ed25519Account.accountHex();

expect(CLPublicKey.fromHex(ed25519AccountHex).value()).to.deep.equal(
ed25519Account.publicKey.value()
);
expect(
CLPublicKey.fromFormattedString(ed25519AccountHex).value()
).to.deep.equal(ed25519Account.publicKey.value());

const secp256K1Account = Keys.Secp256K1.new();
const secp256K1AccountHex = secp256K1Account.accountHex();

expect(CLPublicKey.fromHex(secp256K1AccountHex).value()).to.deep.equal(
secp256K1Account.publicKey.value()
);
expect(
CLPublicKey.fromFormattedString(secp256K1AccountHex).value()
).to.deep.equal(secp256K1Account.publicKey.value());

const badFn = () => CLPublicKey.fromHex('1');
const badFn = () => CLPublicKey.fromFormattedString('1');
expect(badFn).to.throw('Asymmetric key error: too short');

// Check mixed case pubkeys
const goodFn = () =>
CLPublicKey.fromHex(
CLPublicKey.fromFormattedString(
'02025853315DB0e4757cC616bfA97800e2FEa61b7cE37D4376D1719148cd0EA5Fc1C'
);

Expand All @@ -92,12 +92,12 @@ describe('CLPublicKey', () => {
const accountKey =
'019E130bF86201a701cB0DAb4Aae1D6E558781d6fB6195Fc291685777A36cdb388';

const publicKey = CLPublicKey.fromHex(accountKey);
const accountHex = publicKey.toHex(false);
const publicKey = CLPublicKey.fromFormattedString(accountKey);
const accountHex = publicKey.toFormattedString(false);

expect(accountHex).to.be.eq(accountKey.toLowerCase());

const checksummedAccountHex = publicKey.toHex(true);
const checksummedAccountHex = publicKey.toFormattedString(true);

expect(checksummedAccountHex).to.be.eq(accountKey);

Expand All @@ -108,7 +108,7 @@ describe('CLPublicKey', () => {
const accountKey =
'01f9235ff9c46c990e1e2eee0d531e488101fab48c05b75b8ea9983658e228f06b';

const publicKey = CLPublicKey.fromHex(accountKey, false);
const publicKey = CLPublicKey.fromFormattedString(accountKey, false);
const accountHash = publicKey.toAccountHash();
// prettier-ignore
const validResult = Uint8Array.from([
Expand Down
Loading

0 comments on commit 1124678

Please sign in to comment.