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

Implement new sdk version #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
.env.development.local
.env.test.local
.env.production.local
.idea

npm-debug.log*
yarn-debug.log*
Expand Down
24,019 changes: 5,693 additions & 18,326 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,33 @@
"@mui/icons-material": "5.8.4",
"@mui/material": "5.9.3",
"@mui/styled-engine": "5.8.7",
"@noble/curves": "^1.1.0",
"@noble/ed25519": "^1.7.3",
"@noble/hashes": "^1.2.0",
"@noble/secp256k1": "^1.7.1",
"@testing-library/jest-dom": "5.16.5",
"@testing-library/react": "13.3.0",
"@testing-library/user-event": "13.5.0",
"@types/jest": "27.5.2",
"@types/node": "16.11.47",
"@types/react": "18.0.17",
"@types/react-dom": "18.0.6",
"axios": "^1.7.7",
"casper-js-sdk": "2.9.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"typescript": "4.7.4",
"web-vitals": "2.1.4"
"web-vitals": "2.1.4",
"buffer": "^6.0.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"tsc": "tsc -p ./ --noEmit"
"tsc": "tsc -p ./ --noEmit",
"postinstall": "patch-package"
},
"eslintConfig": {
"extends": [
Expand All @@ -46,5 +53,8 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"patch-package": "^8.0.0"
}
}
9,402 changes: 9,402 additions & 0 deletions patches/casper-js-sdk+2.9.1.patch

Large diffs are not rendered by default.

42 changes: 20 additions & 22 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
makeAuctionManagerDeploy,
makeNativeTransferDeploy
} from './deploy-utils';
import { CLPublicKey, DeployUtil } from 'casper-js-sdk';
import { Deploy, PublicKey } from 'casper-js-sdk';

const Container = styled('div')({
backgroundColor: '#282c34',
Expand Down Expand Up @@ -46,22 +46,19 @@ function App() {

// const isConnected = Boolean(activePublicKey);

const handleSignDeploy = (
accountPublicKey: string,
deploy: DeployUtil.Deploy
) => {
const handleSignDeploy = (accountPublicKey: string, deploy: Deploy) => {
if (accountPublicKey) {
const deployJson = DeployUtil.deployToJson(deploy);
const deployJson = Deploy.toJson(deploy);
// console.log('deployJson', JSON.stringify(deployJson));
sign(JSON.stringify(deployJson), accountPublicKey)
.then(res => {
if (res.cancelled) {
alert('Sign cancelled');
} else {
const signedDeploy = DeployUtil.setSignature(
const signedDeploy = Deploy.setSignature(
deploy,
res.signature,
CLPublicKey.fromHex(accountPublicKey)
PublicKey.fromHex(accountPublicKey)
);
alert('Sign successful: ' + JSON.stringify(signedDeploy, null, 2));
}
Expand All @@ -75,30 +72,31 @@ function App() {

const handleMultiSignDeploy = async (
accountPublicKey: string,
deploy: DeployUtil.Deploy
deploy: Deploy
) => {
if (accountPublicKey) {
const deployJson = DeployUtil.deployToJson(deploy);
const deployJson = Deploy.toJson(deploy);
// console.log('deployJson', JSON.stringify(deployJson));

sign(JSON.stringify(deployJson), accountPublicKey)
.then(res => {
if (res.cancelled) {
alert('Sign cancelled');
} else {
const signedDeploy = DeployUtil.setSignature(
const signedDeploy = Deploy.setSignature(
deploy,
res.signature,
CLPublicKey.fromHex(accountPublicKey)
PublicKey.fromHex(accountPublicKey)
);

const deployJson = DeployUtil.deployToJson(signedDeploy);
const deployJson = Deploy.toJson(signedDeploy);

sign(JSON.stringify(deployJson), accountPublicKey).then(res => {
if (res.cancelled) {
alert(res.message)
}
})
sign(JSON.stringify(deployJson), accountPublicKey)
.then(res => {
if (res.cancelled) {
alert(res.message);
}
})
.catch(err => {
alert('Error: ' + err);
throw err;
Expand Down Expand Up @@ -265,11 +263,11 @@ function App() {
const deployJson = JSON.parse(
'{"deploy":{"approvals":[],"hash":"97035958Ab5E2a1EB187B8239491EaEe7FBB97340684d5442D8F84aCB630aeae","header":{"account":"0111BC2070A9aF0F26F94B8549BfFA5643eAD0bc68EBa3b1833039Cfa2a9a8205d","timestamp":"2022-12-06T21:35:31.194Z","ttl":"30m","dependencies":[],"gas_price":1,"body_hash":"01863FC06867f1E007a3236758a8e9D301dc89662Dc2A9bC042C36561d610ae6","chain_name":"casper-test"},"payment":{"ModuleBytes":{"module_bytes":"","args":[["amount",{"cl_type":"U512","bytes":"0400ca9A3B","parsed":"1000000000"}]]}},"session":{"StoredVersionedContractByHash":{"hash":"6ca070C78D4Eb468b4db4CBC5CaDd815c35E15019a841c137372A88D7e247d1D","version":null,"entry_point":"burn","args":[["owner",{"cl_type":"Key","bytes":"00989ca079a5E446071866331468AB949483162588D57ec13ba6BB051f1E15f8b7","parsed":{"Account":"account-hash-989Ca079A5E446071866331468Ab949483162588d57EC13BA6Bb051f1E15f8b7"}}],["token_ids",{"cl_type":{"List":"U256"},"bytes":"010000000168","parsed":""}]]}}}}'
);
const deploy = DeployUtil.deployFromJson(deployJson);
if (deploy.ok) {
handleSignDeploy(signingKey, deploy.val);
const deploy = Deploy.fromJSON(deployJson);
if (deploy) {
handleSignDeploy(signingKey, deploy);
} else {
alert(deploy.val);
alert(deploy);
}
}}
>
Expand Down
79 changes: 47 additions & 32 deletions src/deploy-utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import {
CLPublicKey,
CLValueBuilder,
decodeBase16,
DeployUtil,
RuntimeArgs
Args,
CLValue,
CLValueUInt512,
ContractHash,
Deploy,
DeployHeader,
ExecutableDeployItem,
PublicKey,
StoredContractByHash,
TransferDeployItem
} from 'casper-js-sdk';

const config = {
Expand Down Expand Up @@ -43,37 +48,41 @@ export const makeAuctionManagerDeploy = (
redelegateValidatorPublicKeyHex: string | null,
amountMotes: string
) => {
const delegatorPublicKey = CLPublicKey.fromHex(delegatorPublicKeyHex);
const validatorPublicKey = CLPublicKey.fromHex(validatorPublicKeyHex);
const delegatorPublicKey = PublicKey.fromHex(delegatorPublicKeyHex);
const validatorPublicKey = PublicKey.fromHex(validatorPublicKeyHex);
const newValidatorPublicKey =
redelegateValidatorPublicKeyHex &&
CLPublicKey.fromHex(redelegateValidatorPublicKeyHex);

const deployParams = new DeployUtil.DeployParams(
delegatorPublicKey,
config.network_name
);
PublicKey.fromHex(redelegateValidatorPublicKeyHex);
const executableDeployItem = new ExecutableDeployItem();
const deployCost = getAuctionManagerDeployCost(contractEntryPoint);
const payment = ExecutableDeployItem.standardPayment(deployCost);

const args = RuntimeArgs.fromMap({
delegator: delegatorPublicKey,
validator: validatorPublicKey,
amount: CLValueBuilder.u512(amountMotes),
const args = Args.fromMap({
delegator: CLValue.newCLPublicKey(delegatorPublicKey),
validator: CLValue.newCLPublicKey(validatorPublicKey),
amount: CLValueUInt512.newCLUInt512(amountMotes),
...(newValidatorPublicKey && {
new_validator: newValidatorPublicKey
new_validator: CLValue.newCLPublicKey(newValidatorPublicKey)
})
});

const session = DeployUtil.ExecutableDeployItem.newStoredContractByHash(
decodeBase16(config.auction_manager_contract_hash),
const deployHeader = new DeployHeader(
config.network_name,
undefined,
undefined,
undefined,
undefined,
delegatorPublicKey,
undefined
);

executableDeployItem.storedContractByHash = new StoredContractByHash(
ContractHash.newContract(config.auction_manager_contract_hash),
contractEntryPoint,
args
);

const deployCost = getAuctionManagerDeployCost(contractEntryPoint);

const payment = DeployUtil.standardPayment(deployCost);

return DeployUtil.makeDeploy(deployParams, session, payment);
return Deploy.fromHeaderAndItems(deployHeader, payment, executableDeployItem);
};

export const makeNativeTransferDeploy = (
Expand All @@ -82,22 +91,28 @@ export const makeNativeTransferDeploy = (
amountMotes: string,
transferIdMemo: string
) => {
const senderPublicKey = CLPublicKey.fromHex(senderPublicKeyHex);
const recipientPublicKey = CLPublicKey.fromHex(recipientPublicKeyHex);
const senderPublicKey = PublicKey.fromHex(senderPublicKeyHex);
const recipientPublicKey = PublicKey.fromHex(recipientPublicKeyHex);
const executableDeployItem = new ExecutableDeployItem();

const deployParams = new DeployUtil.DeployParams(
const deployHeader = new DeployHeader(
config.network_name,
undefined,
undefined,
undefined,
undefined,
senderPublicKey,
config.network_name
undefined
);

const session = DeployUtil.ExecutableDeployItem.newTransfer(
executableDeployItem.transfer = TransferDeployItem.newTransfer(
amountMotes,
recipientPublicKey,
undefined,
transferIdMemo
);

const payment = DeployUtil.standardPayment(config.transfer_cost);
const payment = ExecutableDeployItem.standardPayment(config.transfer_cost);

return DeployUtil.makeDeploy(deployParams, session, payment);
return Deploy.fromHeaderAndItems(deployHeader, payment, executableDeployItem);
};
9 changes: 3 additions & 6 deletions src/wallet-service.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { CasperServiceByJsonRPC } from 'casper-js-sdk';
import React, {
createContext,
useCallback,
Expand Down Expand Up @@ -30,9 +29,6 @@ type WalletStorageState = {
publicKey: string | null;
};

const GRPC_URL = 'https://casper-node-proxy.dev.make.services/rpc';
export let casperService = new CasperServiceByJsonRPC(GRPC_URL);

type WalletService = {
logs: [string, object][];
log: (msg: string, payload?: any) => void;
Expand All @@ -43,7 +39,8 @@ type WalletService = {
deployJson: string,
accountPublicKey: string
) => Promise<
{ cancelled: true; message?: string } | { cancelled: false; signature: Uint8Array }
| { cancelled: true; message?: string }
| { cancelled: false; signature: Uint8Array }
>;
signMessage: (
message: string,
Expand Down Expand Up @@ -120,7 +117,7 @@ export const WalletServiceProvider = props => {
}
// no op
});
}, [extensionLoaded,setActivePublicKey]);
}, [extensionLoaded, setActivePublicKey]);

// WALLET SUBSCRIPTIONS
useEffect(() => {
Expand Down