Skip to content

Commit

Permalink
- Remove raw auth info
Browse files Browse the repository at this point in the history
- Move panel to ui file
- Remove memo if non existent
  • Loading branch information
codebycarson committed Nov 28, 2023
1 parent 486f5af commit 62d3134
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 33 deletions.
29 changes: 28 additions & 1 deletion packages/metamask-snap/src/__tests__/ui.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { TxBody, AuthInfo } from '@sei-js/proto/dist/types/codegen/cosmos/tx/v1beta1/tx';
import { decodeRawAuthInfo, decodeTxBody } from '../ui';
import Long from 'long';
import { cosmos } from '@sei-js/proto';
import { sanitizedUint8Array } from '../utils';
import { compressedPubKeyToAddress } from '@sei-js/core';
import { decodeRawAuthInfo, decodeTxBody, getDirectPanel } from '../ui';

describe('decodeTxBody', () => {
it('should decode TxBody correctly', () => {
Expand Down Expand Up @@ -146,3 +146,30 @@ describe('decodeRawAuthInfo', () => {
expect(seiAddress).toBe('sei17lf2fjpqywmwa2t678zzrmhjhmwwthh0rsqr5v');
});
});

describe('getDirectPanel', () => {
it('returns the correct panel structure', () => {
const signDoc = {
authInfoBytes: 'authInfoBytesMock',
bodyBytes: 'bodyBytesMock',
chainId: 'test-chain'
};
const accountNumber = Long.fromNumber(1234);

const result = getDirectPanel(signDoc, accountNumber);

expect(result).toEqual([
'Heading: Sign Transaction',
'Divider',
'Heading: Chain ID',
'Text: test-chain',
'Divider',
'Heading: Account Number',
'Text: 1234',
'Divider',
'Heading: Messages',
'Divider',
'Heading: Fee'
]);
});
});
35 changes: 3 additions & 32 deletions packages/metamask-snap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { OnRpcRequestHandler } from '@metamask/snaps-types';
import { verifyArbitrary } from '@sei-js/core';
import { divider, heading, NodeType, panel, text } from '@metamask/snaps-ui';
import { heading, NodeType, panel, text } from '@metamask/snaps-ui';
import Long from 'long';
import { BIP44CoinTypeNode, getBIP44AddressKeyDeriver } from '@metamask/key-tree';
import { cosmos } from '@sei-js/proto';
import { sanitizedUint8Array } from './utils';
import { decodeRawAuthInfo, decodeTxBody } from './ui';
import { getDirectPanel } from './ui';
import { SignAminoRequest, SignDirectRequest, VerifyArbitraryRequest } from './types';
import { SnapWallet } from './snapWallet';

Expand Down Expand Up @@ -51,39 +50,11 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
accountNumber
};

const authInfoBytes = sanitizedUint8Array(signDoc.authInfoBytes);
const decodedAuthInfo = cosmos.tx.v1beta1.AuthInfo.decode(authInfoBytes);
const readableAuthInfo = decodeRawAuthInfo(decodedAuthInfo);

const bodyBytes = sanitizedUint8Array(signDoc.bodyBytes);
const decodedBody = cosmos.tx.v1beta1.TxBody.decode(bodyBytes);
const readableBody = decodeTxBody(decodedBody);

const confirmed = await snap.request({
method: 'snap_dialog',
params: {
type: 'confirmation',
content: panel([
heading('Sign Transaction'),
divider(),
heading('Chain ID'),
text(signDoc.chainId),
divider(),
heading('Account Number'),
text(accountNumber.toString()),
divider(),
heading('Messages'),
text(JSON.stringify(readableBody.messages, null, 2)),
divider(),
heading('Memo'),
text(JSON.stringify(readableBody.memo, null, 2)),
divider(),
heading('Raw Body'),
text(JSON.stringify(readableBody, null, 2)),
divider(),
heading('Raw Auth Info'),
text(JSON.stringify(readableAuthInfo, null, 2))
])
content: panel(getDirectPanel(signDoc, accountNumber))
}
});

Expand Down
33 changes: 33 additions & 0 deletions packages/metamask-snap/src/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { byteArrayToHex, longToNumber, sanitizedUint8Array } from './utils';
import { cosmos, ibc, seiprotocol, tendermint, cosmwasm, google } from '@sei-js/proto';
import { Any } from '@sei-js/proto/dist/types/codegen/google/protobuf/any';
import Long from 'long';
import { divider, heading, text } from '@metamask/snaps-ui';

interface DecodedTxBody {
timeoutHeight: Long;
Expand Down Expand Up @@ -93,3 +94,35 @@ export const decodeRawAuthInfo = (authInfo: AuthInfo): any => {

return { ...authInfo, signerInfos, fee: { ...authInfo.fee, gasLimit } };
};

export const getDirectPanel = (signDoc: any, accountNumber: Long): any => {
const authInfoBytes = sanitizedUint8Array(signDoc.authInfoBytes);
const decodedAuthInfo = cosmos.tx.v1beta1.AuthInfo.decode(authInfoBytes);
const readableAuthInfo = decodeRawAuthInfo(decodedAuthInfo);

const bodyBytes = sanitizedUint8Array(signDoc.bodyBytes);
const decodedBody = cosmos.tx.v1beta1.TxBody.decode(bodyBytes);
const readableBody = decodeTxBody(decodedBody);

const panel = [
heading('Sign Transaction'),
divider(),
heading('Chain ID'),
text(signDoc.chainId),
divider(),
heading('Account Number'),
text(accountNumber.toString()),
divider(),
heading('Messages'),
text(JSON.stringify(readableBody.messages, null, 2)),
divider(),
heading('Fee'),
text(JSON.stringify(readableAuthInfo.fee, null, 2))
];

if (readableBody.memo) {
panel.push(heading('Memo'), text(JSON.stringify(readableBody.memo, null, 2)), divider());
}

return panel;
};

0 comments on commit 62d3134

Please sign in to comment.