Skip to content

Commit

Permalink
feat: add method support get-starknet (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleyyconsensys authored Nov 15, 2023
1 parent 9f1c593 commit a857eed
Show file tree
Hide file tree
Showing 15 changed files with 713 additions and 116 deletions.
87 changes: 76 additions & 11 deletions packages/starknet-snap/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,34 @@ <h1>Hello, Snaps!</h1>
<input type="submit" id="signTransaction" value="Sign Transaction" />
</fieldset>
</form>
<form id="signDeployAccountTransaction">
<fieldset>
<legend>Sign Deploy Account Transaction</legend>
<label for="signDeployAccountTransaction_userAddress">User Account Address</label>
<input
type="text"
id="signDeployAccountTransaction_userAddress"
name="signDeployAccountTransaction_userAddress"
/><br />
<label for="signDeployAccountTransaction_transaction">Transaction</label>
<input
type="text"
id="signDeployAccountTransaction_transaction"
name="signDeployAccountTransaction_transaction"
/><br />
<input type="submit" id="signDeployAccountTransaction" value="Sign Deploy Account Transaction" />
</fieldset>
</form>
<form id="signDeclareTransaction">
<fieldset>
<legend>Sign Declare Transaction</legend>
<label for="signDeclareTransaction_userAddress">User Account Address</label>
<input type="text" id="signDeclareTransaction_userAddress" name="signDeclareTransaction_userAddress" /><br />
<label for="signDeclareTransaction_transaction">Transaction</label>
<input type="text" id="signDeclareTransaction_transaction" name="signDeclareTransaction_transaction" /><br />
<input type="submit" id="signDeclareTransaction" value="Sign Declare Transaction" />
</fieldset>
</form>
<form id="verifyTypedDataSignature">
<fieldset>
<legend>Verify Typed Data Message Signature</legend>
Expand Down Expand Up @@ -373,6 +401,14 @@ <h1>Hello, Snaps!</h1>
id: 'signTransaction',
method: signTransaction,
},
{
id: 'signDeployAccountTransaction',
method: signDeployAccountTransaction,
},
{
id: 'signDeclareTransaction',
method: signDeclareTransaction,
},
{
id: 'verifyTypedDataSignature',
method: verifyTypedDataSignature,
Expand Down Expand Up @@ -420,7 +456,7 @@ <h1>Hello, Snaps!</h1>
{
id: 'estimateFees',
method: estimateFees,
}
},
];

for (const form of formWithSubmit) {
Expand Down Expand Up @@ -566,7 +602,10 @@ <h1>Hello, Snaps!</h1>
const typedDataMessage = document.getElementById('typedDataMessage').value;
const signerAddress = document.getElementById('signerUserAddress').value;

await callSnap('starkNet_signMessage', { typedDataMessage, signerAddress });
await callSnap('starkNet_signMessage', {
typedDataMessage: typedDataMessage ? JSON.parse(typedDataMessage) : {},
signerAddress,
});
}

// here we call the snap's "starkNet_signTransaction" method
Expand All @@ -578,12 +617,38 @@ <h1>Hello, Snaps!</h1>
const transactionsDetail = document.getElementById('signTransaction_transactionsDetail').value;

await callSnap('starkNet_signTransaction', {
userAddress,
signerAddress: userAddress,
transactions: transactions ? JSON.parse(transactions) : {},
transactionsDetail: transactionsDetail ? JSON.parse(transactionsDetail) : {},
});
}

// here we call the snap's "starkNet_signDeployAccounrTransaction" method
async function signDeployAccountTransaction(e) {
e.preventDefault(); // to prevent default form behavior

const userAddress = document.getElementById('signDeployAccountTransaction_userAddress').value;
const transaction = document.getElementById('signDeployAccountTransaction_transaction').value;

await callSnap('starkNet_signDeployAccounrTransaction', {
signerAddress: userAddress,
transaction: transaction ? JSON.parse(transaction) : {},
});
}

// here we call the snap's "starkNet_signDeclareTransaction" method
async function signDeclareTransaction(e) {
e.preventDefault(); // to prevent default form behavior

const userAddress = document.getElementById('signDeclareTransaction_userAddress').value;
const transaction = document.getElementById('signDeclareTransaction_transaction').value;

await callSnap('starkNet_signDeclareTransaction', {
signerAddress: userAddress,
transaction: transaction ? JSON.parse(transaction) : {},
});
}

// here we call the snap's "starkNet_verifySignedMessage" method
async function verifyTypedDataSignature(e) {
e.preventDefault(); // to prevent default form behavior
Expand Down Expand Up @@ -731,16 +796,16 @@ <h1>Hello, Snaps!</h1>
e.preventDefault(); // to prevent default form behavior

const senderAddress = document.getElementById('executetxn_senderAddress').value;
const txnInvocation = document.getElementById('executetxn_txnInvocation').value
const txnInvocation = document.getElementById('executetxn_txnInvocation').value;
const abis = document.getElementById('executetxn_abis').value;
const invocationsDetails = document.getElementById('executetxn_invocationsDetails').value
const invocationsDetails = document.getElementById('executetxn_invocationsDetails').value;

await callSnap('starkNet_executeTxn', {
senderAddress,
txnInvocation: txnInvocation ? JSON.parse(txnInvocation) : undefined,
abis: abis ? JSON.parse(abis) : undefined,
invocationsDetails: invocationsDetails ? JSON.parse(invocationsDetails) : undefined
})
invocationsDetails: invocationsDetails ? JSON.parse(invocationsDetails) : undefined,
});
}

async function declareContract(e) {
Expand All @@ -763,11 +828,11 @@ <h1>Hello, Snaps!</h1>
const senderAddress = document.getElementById('estimateFees_senderAddress').value;
const invocationDetails = document.getElementById('estimateFees_invocationDetails').value;
const invocations = document.getElementById('estimateFees_invocations').value;

await callSnap('starkNet_estimateFees', {
senderAddress,
invocations: invocations ? JSON.parse(invocations): undefined,
invocationDetails: invocationDetails ? JSON.parse(invocationDetails) : undefined
invocations: invocations ? JSON.parse(invocations) : undefined,
invocationDetails: invocationDetails ? JSON.parse(invocationDetails) : undefined,
});
}

Expand Down
152 changes: 151 additions & 1 deletion packages/starknet-snap/openrpc/starknet_snap_api_openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@
"paramStructure": "by-name",
"params": [
{
"name": "userAddress",
"name": "signerAddress",
"summary": "Address of the account contract",
"description": "Address of the account contract",
"required": true,
Expand Down Expand Up @@ -1075,6 +1075,98 @@
}
},
"errors": []
},
{
"name": "starkNet_signDeclareTransaction",
"summary": "Sign Declare transaction",
"paramStructure": "by-name",
"params": [
{
"name": "signerAddress",
"summary": "Address of the account contract",
"description": "Address of the account contract",
"required": true,
"schema": {
"$ref": "#/components/schemas/ADDRESS"
}
},
{
"name": "chainId",
"summary": "Id of the target Starknet network",
"description": "Id of the target Starknet network (default to Starknet Goerli Testnet)",
"required": false,
"schema": {
"$ref": "#/components/schemas/CHAIN_ID"
}
},
{
"name": "transaction",
"summary": "Declare transaction payload",
"description": "Declare transaction payload",
"required": true,
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/DECLARE_TRANSACTION_PAYLOAD"
}
}
}
],
"result": {
"name": "result",
"summary": "Sign Transaction result",
"description": "Sign Transaction result consist of list of string",
"schema": {
"type": "array"
}
},
"errors": []
},
{
"name": "starkNet_signDeployAccountTransaction",
"summary": "Sign Deploy Account transaction",
"paramStructure": "by-name",
"params": [
{
"name": "signerAddress",
"summary": "Address of the account contract",
"description": "Address of the account contract",
"required": true,
"schema": {
"$ref": "#/components/schemas/ADDRESS"
}
},
{
"name": "chainId",
"summary": "Id of the target Starknet network",
"description": "Id of the target Starknet network (default to Starknet Goerli Testnet)",
"required": false,
"schema": {
"$ref": "#/components/schemas/CHAIN_ID"
}
},
{
"name": "transaction",
"summary": "Deploy Account transaction payload",
"description": "DecDeploy Accountlare transaction payload",
"required": true,
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/DEPLOY_ACCOUNT_TRANSACTION_PAYLOAD"
}
}
}
],
"result": {
"name": "result",
"summary": "Sign Transaction result",
"description": "Sign Transaction result consist of list of string",
"schema": {
"type": "array"
}
},
"errors": []
}
],
"components": {
Expand Down Expand Up @@ -1120,6 +1212,64 @@
}
}
},
"DECLARE_TRANSACTION_PAYLOAD" : {
"type": "object",
"properties": {
"classHash": {
"type": "string"
},
"senderAddress": {
"$ref": "#/components/schemas/FELT"
},
"chainId": {
"$ref": "#/components/schemas/CHAIN_ID"
},
"maxFee": {
"type": "integer"
},
"nonce": {
"type": "integer"
},
"version": {
"type": "integer"
},
"compiledClassHash": {
"type": "string"
}
}
},
"DEPLOY_ACCOUNT_TRANSACTION_PAYLOAD" : {
"type": "object",
"properties": {
"classHash": {
"type": "string"
},
"contractAddress": {
"$ref": "#/components/schemas/FELT"
},
"constructorCalldata": {
"type": "array"
},
"addressSalt": {
"type": "string"
},
"chainId": {
"$ref": "#/components/schemas/CHAIN_ID"
},
"maxFee": {
"type": "integer"
},
"nonce": {
"type": "integer"
},
"version": {
"type": "integer"
},
"compiledClassHash": {
"type": "string"
}
}
},
"INVOCATION_PAYLOAD": {
"type": "object",
"properties": {
Expand Down
10 changes: 10 additions & 0 deletions packages/starknet-snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import { estimateAccDeployFee } from './estimateAccountDeployFee';
import { executeTxn } from './executeTxn';
import { estimateFees } from './estimateFees';
import { declareContract } from './declareContract';
import { signDeclareTransaction } from './signDeclareTransaction';
import { signDeployAccountTransaction } from './signDeployAccountTransaction';
import { logger } from './utils/logger';

declare const snap;
Expand Down Expand Up @@ -121,6 +123,14 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ origin, request }) =>
apiParams.keyDeriver = await getAddressKeyDeriver(snap);
return signTransaction(apiParams);

case 'starkNet_signDeclareTransaction':
apiParams.keyDeriver = await getAddressKeyDeriver(snap);
return signDeclareTransaction(apiParams);

case 'starkNet_signDeployAccountTransaction':
apiParams.keyDeriver = await getAddressKeyDeriver(snap);
return signDeployAccountTransaction(apiParams);

case 'starkNet_verifySignedMessage':
apiParams.keyDeriver = await getAddressKeyDeriver(snap);
return verifySignedMessage(apiParams);
Expand Down
39 changes: 39 additions & 0 deletions packages/starknet-snap/src/signDeclareTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { toJson } from './utils/serializer';
import { Signature } from 'starknet';
import { ApiParams, SignDeclareTransactionRequestParams } from './types/snapApi';
import { getKeysFromAddress, signDeclareTransaction as signDeclareTransactionUtil } from './utils/starknetUtils';
import { getNetworkFromChainId, getSignTxnTxt } from './utils/snapUtils';
import { DialogType } from '@metamask/rpc-methods';
import { heading, panel } from '@metamask/snaps-ui';
import { logger } from './utils/logger';

export async function signDeclareTransaction(params: ApiParams): Promise<Signature | boolean> {
try {
const { state, keyDeriver, requestParams, wallet } = params;
const requestParamsObj = requestParams as SignDeclareTransactionRequestParams;
const signerAddress = requestParamsObj.signerAddress;
const network = getNetworkFromChainId(state, requestParamsObj.chainId);
const { privateKey } = await getKeysFromAddress(keyDeriver, network, state, signerAddress);

logger.log(`signDeclareTransaction params: ${toJson(requestParamsObj.transaction, 2)}}`);

const snapComponents = getSignTxnTxt(signerAddress, network, requestParamsObj.transaction);

if (requestParamsObj.enableAutherize === true) {
const response = await wallet.request({
method: 'snap_dialog',
params: {
type: DialogType.Confirmation,
content: panel([heading('Do you want to sign this transaction?'), ...snapComponents]),
},
});

if (!response) return false;
}

return await signDeclareTransactionUtil(privateKey, requestParamsObj.transaction);
} catch (error) {
logger.error(`Problem found: ${error}`);
throw error;
}
}
Loading

0 comments on commit a857eed

Please sign in to comment.