Skip to content

Commit

Permalink
feat: temp fix for issues
Browse files Browse the repository at this point in the history
  • Loading branch information
gicharujohn committed Dec 29, 2022
1 parent 8210855 commit f3458e3
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 142 deletions.
14 changes: 14 additions & 0 deletions src/electron/events/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ module.exports = () => {
),
network: parsedData.network || 'testnet',
};

console.log(returnValue);

sendMessage('accountCsprBalanceResponse', JSON.stringify(returnValue));

event.returnValue = JSON.stringify(returnValue);
Expand Down Expand Up @@ -203,6 +206,8 @@ module.exports = () => {
parsedData.network,
);

console.log(response);

sendMessage(
'sendErc20TokensResponse',
JSON.stringify({
Expand All @@ -228,6 +233,7 @@ module.exports = () => {
const parsedData = JSON.parse(data);
try {
const res = await getTokenDetails(parsedData.contractHash, parsedData.network ?? 'testnet');
console.log(res);

const returnVal = JSON.stringify({
data: {
Expand Down Expand Up @@ -293,6 +299,9 @@ module.exports = () => {
network: network ?? 'testnet',
algorithm: algorithm ?? 'ed25519',
});

console.log(response);

sendMessage(
'delegateResponse',
JSON.stringify({
Expand Down Expand Up @@ -354,6 +363,9 @@ module.exports = () => {
network: network ?? 'tesnet',
algorithm: algorithm ?? 'ed25519',
});

console.log(response);

sendMessage(
'undelegateResponse',
JSON.stringify({
Expand Down Expand Up @@ -403,6 +415,8 @@ module.exports = () => {
data.ledger_account_index,
);

console.log(response);

sendMessage(
'deployErc20ContractResponse',
JSON.stringify({
Expand Down
84 changes: 44 additions & 40 deletions src/electron/utils/staking.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -80,48 +80,52 @@ module.exports = {
network = 'testnet',
algorithm,
}) => {
const { casperService, casperClient } = getCasperClientAndService(network);
const networkName = network === 'mainnet' ? 'casper' : 'casper-test';
try {
const { casperService, casperClient } = getCasperClientAndService(network);
const networkName = network === 'mainnet' ? 'casper' : 'casper-test';

// Read keys from the structure created in #Generating keys
if (privateKey.length !== 128) {
privateKey = Keys.Ed25519.parsePrivateKey(privateKey);
}
// Read keys from the structure created in #Generating keys
if (privateKey.length !== 128) {
privateKey = Keys.Ed25519.parsePrivateKey(privateKey);
}

const publicKey = Keys.Ed25519.privateToPublicKey(privateKey);

const keyPair =
algorithm === 'ed25519'
? Keys.Ed25519.parseKeyPair(Buffer.from(publicKey, 'hex'), Buffer.from(privateKey, 'hex'))
: Keys.Secp256K1.parseKeyPair(
Buffer.from(publicKey.slice(2), 'hex'),
Buffer.from(privateKey, 'hex'),
'raw',
);

const publicKey = Keys.Ed25519.privateToPublicKey(privateKey);

const keyPair =
algorithm === 'ed25519'
? Keys.Ed25519.parseKeyPair(Buffer.from(publicKey, 'hex'), Buffer.from(privateKey, 'hex'))
: Keys.Secp256K1.parseKeyPair(
Buffer.from(publicKey.slice(2), 'hex'),
Buffer.from(privateKey, 'hex'),
'raw',
);

const deployParams = new DeployUtil.DeployParams(keyPair.publicKey, networkName);
const payment = DeployUtil.standardPayment(5000000000);

amount = ethers.utils.parseUnits(amount.toString(), 9);

const args = RuntimeArgs.fromMap({
delegator: keyPair.publicKey,
validator: CLPublicKey.fromHex(validatorPublicKey),
amount: CLValueBuilder.u512(amount),
});
let contractHash = 'ccb576d6ce6dec84a551e48f0d0b7af89ddba44c7390b690036257a04a3ae9ea';
if (network === 'testnet') {
contractHash = '93d923e336b20a4c4ca14d592b60e5bd3fe330775618290104f9beb326db7ae2';
const deployParams = new DeployUtil.DeployParams(keyPair.publicKey, networkName);
const payment = DeployUtil.standardPayment(5000000000);

amount = ethers.utils.parseUnits(amount.toString(), 9);

const args = RuntimeArgs.fromMap({
delegator: keyPair.publicKey,
validator: CLPublicKey.fromHex(validatorPublicKey),
amount: CLValueBuilder.u512(amount),
});
let contractHash = 'ccb576d6ce6dec84a551e48f0d0b7af89ddba44c7390b690036257a04a3ae9ea';
if (network === 'testnet') {
contractHash = '93d923e336b20a4c4ca14d592b60e5bd3fe330775618290104f9beb326db7ae2';
}
const session = DeployUtil.ExecutableDeployItem.newStoredContractByHash(
Uint8Array.from(Buffer.from(contractHash, 'hex')),
'undelegate',
args,
);
const deploy = DeployUtil.makeDeploy(deployParams, session, payment);
const signedDeploy = DeployUtil.signDeploy(deploy, keyPair);
const executionResult = await client.putDeploy(signedDeploy);

return await casperClient.getDeploy(executionResult);
} catch (error) {
throw error;
}
const session = DeployUtil.ExecutableDeployItem.newStoredContractByHash(
Uint8Array.from(Buffer.from(contractHash, 'hex')),
'undelegate',
args,
);
const deploy = DeployUtil.makeDeploy(deployParams, session, payment);
const signedDeploy = DeployUtil.signDeploy(deploy, keyPair);
const executionResult = await client.putDeploy(signedDeploy);

return await casperClient.getDeploy(executionResult);
},
};
219 changes: 117 additions & 102 deletions src/electron/utils/tokens/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,62 +40,71 @@ const deployMintableContract = async (
authorized_minter = null,
ledger_account_index = undefined,
) => {
const { casperClient } = getCasperClientAndService(network);
const networkName = network === 'mainnet' ? 'casper' : 'casper-test';

const erc20MintableContractCode = fs.readFileSync(path.join(__dirname, './erc20_mintable.wasm'), {
encoding: 'hex',
});

const contractCode = Uint8Array.from(Buffer.from(erc20MintableContractCode, 'hex'));

const clPublicKey = CLPublicKey.fromHex(public_key);

const deployParams = new DeployUtil.DeployParams(clPublicKey, networkName);
const payment = DeployUtil.standardPayment(90000000000);

const session = DeployUtil.ExecutableDeployItem.newModuleBytes(
contractCode,
RuntimeArgs.fromMap({
name: new CLString(token_name),
symbol: new CLString(token_symbol),
decimals: new CLU8(token_decimals),
total_supply: new CLU256(ethers.utils.parseUnits(token_supply.toString(), token_decimals)),
authorized_minter: new CLString(authorized_minter),
contract_key_name: new CLString(token_name),
}),
);
try {
const { casperClient } = getCasperClientAndService(network);
const networkName = network === 'mainnet' ? 'casper' : 'casper-test';

const erc20MintableContractCode = fs.readFileSync(
path.join(__dirname, './erc20_mintable.wasm'),
{
encoding: 'hex',
},
);

let deploy = DeployUtil.makeDeploy(deployParams, session, payment);
const contractCode = Uint8Array.from(Buffer.from(erc20MintableContractCode, 'hex'));

/** Sign Deploy */
if (_.isNumber(ledger_account_index)) {
// Use Ledger
const signature = await signTransactionUsingLedger(ledger_account_index, deploy);
if (!signature || signature?.error || signature?.errorMessage?.toLowerCase() !== 'no errors') {
throw 'Failed to sign transaction. Please make sure your ledger is unlocked and Casper App is Active/Open and try again';
}
const clPublicKey = CLPublicKey.fromHex(public_key);

deploy = DeployUtil.setSignature(deploy, signature.signatureRS, clPublicKey);
} else {
const customPublicKeyDerivation =
pvk_algorithm === 'ed25519'
? Keys.Ed25519.privateToPublicKey(Buffer.from(private_key, 'hex'))
: Keys.Secp256K1.privateToPublicKey(Buffer.from(private_key, 'hex'));
const keyPair =
pvk_algorithm === 'ed25519'
? Keys.Ed25519.parseKeyPair(customPublicKeyDerivation, Buffer.from(private_key, 'hex'))
: Keys.Secp256K1.parseKeyPair(
customPublicKeyDerivation,
Buffer.from(private_key, 'hex'),
'raw',
);
deploy = DeployUtil.signDeploy(deploy, keyPair);
}
const deployParams = new DeployUtil.DeployParams(clPublicKey, networkName);
const payment = DeployUtil.standardPayment(90000000000);

const session = DeployUtil.ExecutableDeployItem.newModuleBytes(
contractCode,
RuntimeArgs.fromMap({
name: new CLString(token_name),
symbol: new CLString(token_symbol),
decimals: new CLU8(token_decimals),
total_supply: new CLU256(ethers.utils.parseUnits(token_supply.toString(), token_decimals)),
authorized_minter: new CLString(authorized_minter),
contract_key_name: new CLString(token_name),
}),
);

const deployHash = await casperClient.putDeploy(deploy);
let deploy = DeployUtil.makeDeploy(deployParams, session, payment);

/** Sign Deploy */
if (_.isNumber(ledger_account_index)) {
// Use Ledger
const signature = await signTransactionUsingLedger(ledger_account_index, deploy);
if (
!signature ||
signature?.error ||
signature?.errorMessage?.toLowerCase() !== 'no errors'
) {
throw 'Failed to sign transaction. Please make sure your ledger is unlocked and Casper App is Active/Open and try again';
}

deploy = DeployUtil.setSignature(deploy, signature.signatureRS, clPublicKey);
} else {
const customPublicKeyDerivation =
pvk_algorithm === 'ed25519'
? Keys.Ed25519.privateToPublicKey(Buffer.from(private_key, 'hex'))
: Keys.Secp256K1.privateToPublicKey(Buffer.from(private_key, 'hex'));
const keyPair =
pvk_algorithm === 'ed25519'
? Keys.Ed25519.parseKeyPair(customPublicKeyDerivation, Buffer.from(private_key, 'hex'))
: Keys.Secp256K1.parseKeyPair(
customPublicKeyDerivation,
Buffer.from(private_key, 'hex'),
'raw',
);
deploy = DeployUtil.signDeploy(deploy, keyPair);
}

return deployHash;
return await casperClient.putDeploy(deploy);
} catch (error) {
throw error;
}
};

const deployNormalContract = async (
Expand All @@ -109,62 +118,68 @@ const deployNormalContract = async (
network = 'testnet',
ledger_account_index = undefined,
) => {
const { casperClient } = getCasperClientAndService(network);
const networkName = network === 'mainnet' ? 'casper' : 'casper-test';

const erc20MintableContractCode = fs.readFileSync(path.join(__dirname, './erc20_token.wasm'), {
encoding: 'hex',
});

const contractCode = Uint8Array.from(Buffer.from(erc20MintableContractCode, 'hex'));

const clPublicKey = CLPublicKey.fromHex(public_key);

const deployParams = new DeployUtil.DeployParams(clPublicKey, networkName);
const payment = DeployUtil.standardPayment(90000000000);

const session = DeployUtil.ExecutableDeployItem.newModuleBytes(
contractCode,
RuntimeArgs.fromMap({
name: new CLString(token_name),
symbol: new CLString(token_symbol),
decimals: new CLU8(token_decimals),
total_supply: new CLU256(ethers.utils.parseUnits(token_supply.toString(), token_decimals)),
// authorized_minter: new CLString(authorized_minter),
contract_key_name: new CLString(token_name),
}),
);
try {
const { casperClient } = getCasperClientAndService(network);
const networkName = network === 'mainnet' ? 'casper' : 'casper-test';

const erc20MintableContractCode = fs.readFileSync(path.join(__dirname, './erc20_token.wasm'), {
encoding: 'hex',
});

const contractCode = Uint8Array.from(Buffer.from(erc20MintableContractCode, 'hex'));

const clPublicKey = CLPublicKey.fromHex(public_key);

const deployParams = new DeployUtil.DeployParams(clPublicKey, networkName);
const payment = DeployUtil.standardPayment(90000000000);

const session = DeployUtil.ExecutableDeployItem.newModuleBytes(
contractCode,
RuntimeArgs.fromMap({
name: new CLString(token_name),
symbol: new CLString(token_symbol),
decimals: new CLU8(token_decimals),
total_supply: new CLU256(ethers.utils.parseUnits(token_supply.toString(), token_decimals)),
// authorized_minter: new CLString(authorized_minter),
contract_key_name: new CLString(token_name),
}),
);

let deploy = DeployUtil.makeDeploy(deployParams, session, payment);
let deploy = DeployUtil.makeDeploy(deployParams, session, payment);

/** Sign Deploy */
if (_.isNumber(ledger_account_index)) {
// Use Ledger
const signature = await signTransactionUsingLedger(ledger_account_index, deploy);
if (
!signature ||
signature?.error ||
signature?.errorMessage?.toLowerCase() !== 'no errors'
) {
throw 'Failed to sign transaction. Please make sure your ledger is unlocked and Casper App is Active/Open and try again';
}

/** Sign Deploy */
if (_.isNumber(ledger_account_index)) {
// Use Ledger
const signature = await signTransactionUsingLedger(ledger_account_index, deploy);
if (!signature || signature?.error || signature?.errorMessage?.toLowerCase() !== 'no errors') {
throw 'Failed to sign transaction. Please make sure your ledger is unlocked and Casper App is Active/Open and try again';
deploy = DeployUtil.setSignature(deploy, signature.signatureRS, clPublicKey);
} else {
const customPublicKeyDerivation =
pvk_algorithm === 'ed25519'
? Keys.Ed25519.privateToPublicKey(Buffer.from(private_key, 'hex'))
: Keys.Secp256K1.privateToPublicKey(Buffer.from(private_key, 'hex'));
const keyPair =
pvk_algorithm === 'ed25519'
? Keys.Ed25519.parseKeyPair(customPublicKeyDerivation, Buffer.from(private_key, 'hex'))
: Keys.Secp256K1.parseKeyPair(
customPublicKeyDerivation,
Buffer.from(private_key, 'hex'),
'raw',
);
deploy = DeployUtil.signDeploy(deploy, keyPair);
}

deploy = DeployUtil.setSignature(deploy, signature.signatureRS, clPublicKey);
} else {
const customPublicKeyDerivation =
pvk_algorithm === 'ed25519'
? Keys.Ed25519.privateToPublicKey(Buffer.from(private_key, 'hex'))
: Keys.Secp256K1.privateToPublicKey(Buffer.from(private_key, 'hex'));
const keyPair =
pvk_algorithm === 'ed25519'
? Keys.Ed25519.parseKeyPair(customPublicKeyDerivation, Buffer.from(private_key, 'hex'))
: Keys.Secp256K1.parseKeyPair(
customPublicKeyDerivation,
Buffer.from(private_key, 'hex'),
'raw',
);
deploy = DeployUtil.signDeploy(deploy, keyPair);
return await casperClient.putDeploy(deploy);
} catch (error) {
throw error;
}

const deployHash = await casperClient.putDeploy(deploy);

return deployHash;
};

const getTokenNamedKeyValue = async (contractHash, key, network) => {
Expand Down

0 comments on commit f3458e3

Please sign in to comment.