Skip to content

Instantly share code, notes, and snippets.

@piotrwitek
Last active June 7, 2021 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piotrwitek/11651815162d158a6afc472006db65b8 to your computer and use it in GitHub Desktop.
Save piotrwitek/11651815162d158a6afc472006db65b8 to your computer and use it in GitHub Desktop.
Casper Deploys
// delegatorPublicKey: string, // 02024ef1def792992eeed81b2c55c42420236700b9c0eb55229cfd31a5e1ef437ac4
// validatorPublicKey: string, // 017d96b9a63abcb61c870a4f55187a0a7ac24096bdb5fc585c12a686a4d892009e
// delegateAmount: BigNumber,
const delegatorPublicKey = PublicKey.fromHex(publicKeyHex);
const validatorPublicKey = PublicKey.fromHex(validatorHex);
const args = RuntimeArgs.fromMap({
delegator: CLValue.publicKey(delegatorPublicKey),
validator: CLValue.publicKey(validatorPublicKey),
amount: CLValue.u512(delegateAmount),
});
const session = DeployUtil.ExecutableDeployItem.newStoredContractByHash(
decodeBase16(config.auction_manager_contract_hash),
DELEGATE_ENTRY_POINT,
args
);
const payment = DeployUtil.standardPayment(
BigNumber.from(config.delegate_cost)
);
const deploy = DeployUtil.makeDeploy(
new DeployUtil.DeployParams(delegatorPublicKey, config.network.chainName),
session,
payment
);
const deployJson: any = DeployUtil.deployToJson(deploy);
Signer.sign(deployJson, publicKeyHex).then((signedDeployJson) => {
const signedDeploy = DeployUtil.deployFromJson(signedDeployJson);
return casperService.deploy(signedDeploy!).then((res) => {
return res;
});
});
// publicKeyHex: string, // 02024ef1def792992eeed81b2c55c42420236700b9c0eb55229cfd31a5e1ef437ac4
// recipientHex: string, // 019c0ff3ec5903540908e478d6ccda45c68405e08d73a0a064e0f1e4239a7ba959
// transferAmount: BigNumber,
// transferIdMemo: number
const senderPublicKey = PublicKey.fromHex(publicKeyHex);
const recipientPublicKey = PublicKey.fromHex(recipientHex);
const deployParams = new DeployUtil.DeployParams(
senderPublicKey,
config.network.chainName // casper-test
);
const session = DeployUtil.ExecutableDeployItem.newTransfer(
transferAmount,
recipientPublicKey,
undefined,
transferIdMemo
);
const payment = DeployUtil.standardPayment(DEFAULT_AMOUNT); // 10000000000000
const deploy = DeployUtil.makeDeploy(deployParams, session, payment);
const deployJson: any = DeployUtil.deployToJson(deploy);
Signer.sign(deployJson, publicKeyHex).then((signedDeployJson) => {
const signedDeploy = DeployUtil.deployFromJson(signedDeployJson);
return casperService.deploy(signedDeploy!).then((res) => {
return res;
});
});
import TransportWebUSB from '@ledgerhq/hw-transport-webusb';
import LedgerApp from '@zondax/ledger-casper';
// ledgerPublicKeyHex: string, // 028b2ddbe59976ad2f4138ca46553866de5124d13db4e13611ca751eedde9e0297
// recipientPublicKeyHex: string, // 020351a626464d6d0ae68efe33f049a0639aaefe07a956151296c708e837935d84f6
// transferAmount: BigNumber,
// transferIdMemo: number
const senderPublicKey = PublicKey.fromHex('02' + ledgerPublicKeyHex);
const recipientPublicKey = PublicKey.fromHex(recipientPublicKeyHex);
const deployParams = new DeployUtil.DeployParams(
senderPublicKey,
config.network.chainName // casper-test
);
const session = DeployUtil.ExecutableDeployItem.newTransfer(
transferAmount, // 5 000 000 000 motes
recipientPublicKey,
undefined,
transferIdMemo, // 5
);
const payment = DeployUtil.standardPayment(DEFAULT_AMOUNT); // 10 000 000 000 000
const deploy = DeployUtil.makeDeploy(deployParams, session, payment);
const deployBytes = DeployUtil.deployToBytes(deploy);
// call ledger sign method
const transport = await TransportWebUSB.create();
const ledgerApp = new LedgerApp(transport);
// sign => https://github.com/Zondax/ledger-casper/blob/master/js/src/index.ts#L203
const res = await ledgerApp.sign(
`m/44'/506'/0'/0/0`,
deployBytes
);
// signature verification
const deployHash = deploy.hash;
const pk = Uint8Array.from(Buffer.from(ledgerPublicKeyHex, 'hex'));
const digest = deployHash;
const signature = res.signatureRS;
const signatureOk = secp256k1.ecdsaVerify(signature, digest, pk); // true
// sign deploy object
const signedDeploy = DeployUtil.setSignature(
deploy,
signature,
PublicKey.fromHex(ledgerPublicKeyHex)
);
// deploy method call throw Uncaught (in promise) Error: Invalid params
casperService.deploy(signedDeploy).then((res) => {
return res;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment