Skip to content

Commit

Permalink
refactor: replace borsh-js schemas with borsh.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Jul 11, 2023
1 parent 3110ff4 commit 1593ceb
Show file tree
Hide file tree
Showing 29 changed files with 283 additions and 546 deletions.
2 changes: 1 addition & 1 deletion apps/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@ledgerhq/hw-transport-webusb": "^6.27.14",
"@zondax/ledger-namada": "^0.0.2",
"bignumber.js": "^9.1.1",
"borsh": "^0.7.0",
"@dao-xyz/borsh": "^5.1.5",
"buffer": "^6.0.3",
"dompurify": "^3.0.2",
"framer-motion": "6.2.4",
Expand Down
10 changes: 3 additions & 7 deletions apps/extension/src/background/approvals/service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import browser from "webextension-polyfill";
import { fromBase64 } from "@cosmjs/encoding";
import { deserialize } from "borsh";
import { v4 as uuid } from "uuid";
import BigNumber from "bignumber.js";

import { SubmitTransferMsgSchema, TransferMsgValue } from "@anoma/types";
import { TransferMsgValue } from "@anoma/types";
import { KVStore } from "@anoma/storage";

import { ExtensionRequester } from "extension";
import { KeyRingService } from "background/keyring";
import { TabStore } from "background/keyring";
import { deserialize } from "@dao-xyz/borsh";

export class ApprovalsService {
constructor(
Expand All @@ -27,11 +27,7 @@ export class ApprovalsService {
this.txStore.set(id, txMsg);

// Decode tx details and launch approval screen
const txDetails = deserialize(
SubmitTransferMsgSchema,
TransferMsgValue,
txMsgBuffer
);
const txDetails = deserialize(txMsgBuffer, TransferMsgValue);
const { source, target, token, amount: amountBN } = txDetails;
const amount = new BigNumber(amountBN.toString());
const url = `${browser.runtime.getURL(
Expand Down
9 changes: 2 additions & 7 deletions apps/extension/src/background/keyring/keyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
AccountType,
Bip44Path,
DerivedAccount,
SubmitTransferMsgSchema,
TransferMsgValue,
} from "@anoma/types";
import { chains } from "@anoma/chains";
Expand All @@ -39,8 +38,8 @@ import {
readVecStringPointer,
readStringPointer,
} from "@anoma/crypto/src/utils";
import { deserialize } from "borsh";
import { Result } from "@anoma/utils";
import { deserialize } from "@dao-xyz/borsh";

// Generated UUID namespace for uuid v5
const UUID_NAMESPACE = "9bfceade-37fe-11ed-acc0-a3da3461b38c";
Expand Down Expand Up @@ -686,11 +685,7 @@ export class KeyRing {

// We need to get the source address in case it is shielded one, so we can
// decrypt the extended spending key for a transfer.
const { source } = deserialize(
SubmitTransferMsgSchema,
TransferMsgValue,
Buffer.from(txMsg)
);
const { source } = deserialize(Buffer.from(txMsg), TransferMsgValue);

const account = await this._keyStore.getRecord("address", source);
if (!account) {
Expand Down
21 changes: 9 additions & 12 deletions apps/extension/src/provider/Anoma.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ import { toBase64 } from "@cosmjs/encoding";
import BigNumber from "bignumber.js";

import {
AccountMsgSchema,
AccountMsgValue,
SubmitIbcTransferMsgSchema,
IbcTransferMsgValue,
IbcTransferProps,
Message,
SubmitTransferMsgSchema,
TransferMsgValue,
TransferProps,
Chain,
Expand Down Expand Up @@ -96,22 +93,21 @@ describe("Anoma", () => {
feeAmount: new BigNumber(0),
gasLimit: new BigNumber(0),
chainId: chain.chainId,
publicKey: undefined,
},
source: keyStore[0].address,
target:
"atest1d9khqw36gdz5ydzygvcnyvesxgcn2s6zxyung3zzgcmrjwzzgvmnyd3kxym52vzzg5unxve5cm87cr",
token,
amount: new BigNumber(1000),
nativeToken: token,
subPrefix: undefined,
};

const transferMsgValue = new TransferMsgValue(transferProps);

const transferMessage = new Message<TransferMsgValue>();
const serializedTransfer = transferMessage.encode(
SubmitTransferMsgSchema,
transferMsgValue
);
const serializedTransfer = transferMessage.encode(transferMsgValue);

jest.spyOn(keyRingService, "submitTransfer");
anoma.submitTransfer(toBase64(serializedTransfer));
Expand All @@ -132,6 +128,7 @@ describe("Anoma", () => {
feeAmount: new BigNumber(0),
gasLimit: new BigNumber(0),
chainId: chain.chainId,
publicKey: undefined,
},
source: keyStore[0].address,
receiver:
Expand All @@ -140,15 +137,15 @@ describe("Anoma", () => {
amount: new BigNumber(1000),
portId: "transfer",
channelId: "channel-0",
subPrefix: undefined,
timeoutHeight: undefined,
timeoutSecOffset: undefined,
};

const transferMsgValue = new IbcTransferMsgValue(transferProps);

const transferMessage = new Message<IbcTransferMsgValue>();
const serializedTransfer = transferMessage.encode(
SubmitIbcTransferMsgSchema,
transferMsgValue
);
const serializedTransfer = transferMessage.encode(transferMsgValue);

const res = anoma.submitIbcTransfer(toBase64(serializedTransfer));

Expand All @@ -162,7 +159,7 @@ describe("Anoma", () => {
vpCode: new Uint8Array(),
});
const accountMessage = new Message<AccountMsgValue>();
const serialized = accountMessage.encode(AccountMsgSchema, accountMsgValue);
const serialized = accountMessage.encode(accountMsgValue);

await expect(
anoma.encodeInitAccount({
Expand Down
22 changes: 6 additions & 16 deletions apps/extension/src/provider/Signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import {
Account,
Anoma,
AccountMsgValue,
AccountMsgSchema,
SubmitIbcTransferMsgSchema,
IbcTransferMsgValue,
IbcTransferProps,
InitAccountProps,
Expand All @@ -15,10 +13,7 @@ import {
AccountType,
SubmitBondProps,
SubmitBondMsgValue,
SubmitBondMsgSchema,
SubmitTransferMsgSchema,
SubmitUnbondMsgValue,
SubmitUnbondMsgSchema,
} from "@anoma/types";

export class Signer implements ISigner {
Expand All @@ -45,7 +40,7 @@ export class Signer implements ISigner {
const msgValue = new SubmitBondMsgValue(args);

const msg = new Message<SubmitBondMsgValue>();
const encoded = msg.encode(SubmitBondMsgSchema, msgValue);
const encoded = msg.encode(msgValue);

return await this._anoma.submitBond(toBase64(encoded));
}
Expand All @@ -57,7 +52,7 @@ export class Signer implements ISigner {
const msgValue = new SubmitUnbondMsgValue(args);

const msg = new Message<SubmitUnbondMsgValue>();
const encoded = msg.encode(SubmitUnbondMsgSchema, msgValue);
const encoded = msg.encode(msgValue);

return await this._anoma.submitUnbond(toBase64(encoded));
}
Expand All @@ -68,10 +63,7 @@ export class Signer implements ISigner {
public async submitTransfer(args: TransferProps): Promise<void> {
const transferMsgValue = new TransferMsgValue(args);
const transferMessage = new Message<TransferMsgValue>();
const serializedTransfer = transferMessage.encode(
SubmitTransferMsgSchema,
transferMsgValue
);
const serializedTransfer = transferMessage.encode(transferMsgValue);

return await this._anoma.submitTransfer(toBase64(serializedTransfer));
}
Expand All @@ -82,10 +74,8 @@ export class Signer implements ISigner {
public async submitIbcTransfer(args: IbcTransferProps): Promise<void> {
const ibcTransferMsgValue = new IbcTransferMsgValue(args);
const ibcTransferMessage = new Message<IbcTransferMsgValue>();
const serializedIbcTransfer = ibcTransferMessage.encode(
SubmitIbcTransferMsgSchema,
ibcTransferMsgValue
);
const serializedIbcTransfer =
ibcTransferMessage.encode(ibcTransferMsgValue);

return await this._anoma.submitIbcTransfer(toBase64(serializedIbcTransfer));
}
Expand All @@ -102,7 +92,7 @@ export class Signer implements ISigner {
vpCode,
});
const accountMessage = new Message<AccountMsgValue>();
const serialized = accountMessage.encode(AccountMsgSchema, accountMsgValue);
const serialized = accountMessage.encode(accountMsgValue);

return await this._anoma.encodeInitAccount({
txMsg: toBase64(serialized),
Expand Down
23 changes: 12 additions & 11 deletions apps/extension/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
{
"compilerOptions": {
"baseUrl": "./src",
"target": "es2015",
"lib": ["dom", "dom.iterable", "esnext", "webworker"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"baseUrl": "./src",
"esModuleInterop": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"isolatedModules": true,
"jsx": "react-jsx",
"lib": ["dom", "dom.iterable", "esnext", "webworker"],
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"esModuleInterop": true,
"isolatedModules": true,
"noEmit": false,
"jsx": "react-jsx"
"noFallthroughCasesInSwitch": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "es2015"
},
"include": ["src/**/*.ts", "src/**/*.tsx"],
"exclude": ["node_modules"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export const postNewBonding = createAsyncThunk<
feeAmount: new BigNumber(0),
gasLimit: new BigNumber(0),
chainId,
publicKey: undefined,
},
});
});
Expand All @@ -197,6 +198,7 @@ export const postNewUnbonding = createAsyncThunk<
feeAmount: new BigNumber(0),
gasLimit: new BigNumber(0),
chainId,
publicKey: undefined,
},
});
});
7 changes: 7 additions & 0 deletions apps/namada-interface/src/slices/transfers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,14 @@ export const submitTransferTransaction = createAsyncThunk<
feeAmount: new BigNumber(0),
gasLimit: new BigNumber(0),
chainId,
publicKey: undefined,
},
source: txTransferArgs.account.address,
target: txTransferArgs.target,
token: Tokens.NAM.address || "",
amount: txTransferArgs.amount,
nativeToken: Tokens.NAM.address || "",
subPrefix: undefined,
});
}
);
Expand Down Expand Up @@ -194,13 +196,17 @@ export const submitIbcTransferTransaction = createAsyncThunk<
feeAmount: new BigNumber(0),
gasLimit: new BigNumber(0),
chainId,
publicKey: undefined,
},
source: txIbcTransferArgs.account.address,
receiver: txIbcTransferArgs.target,
token: Tokens.NAM.address || "",
amount: txIbcTransferArgs.amount,
portId: txIbcTransferArgs.portId,
channelId: txIbcTransferArgs.channelId,
subPrefix: undefined,
timeoutHeight: undefined,
timeoutSecOffset: undefined,
},
});

Expand Down Expand Up @@ -237,6 +243,7 @@ export const submitBridgeTransferTransaction = createAsyncThunk<
feeAmount: new BigNumber(0),
gasLimit: new BigNumber(0),
chainId,
publicKey: undefined,
},
source: txBridgeTransferArgs.account.address,
target: txBridgeTransferArgs.target,
Expand Down
23 changes: 12 additions & 11 deletions apps/namada-interface/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
{
"compilerOptions": {
"baseUrl": "./src",
"target": "es2015",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"baseUrl": "./src",
"esModuleInterop": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"isolatedModules": true,
"jsx": "react-jsx",
"lib": ["dom", "dom.iterable", "esnext"],
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"esModuleInterop": true,
"isolatedModules": true,
"noEmit": false,
"jsx": "react-jsx"
"noFallthroughCasesInSwitch": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "es2015"
},
"include": ["src/**/*.ts", "src/**/*.tsx"],
"exclude": ["node_modules"],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@cosmjs/json-rpc": "^0.27.1",
"@cosmjs/tendermint-rpc": "^0.27.1",
"bn.js": "^5.2.0",
"borsh": "^0.7.0",
"@dao-xyz/borsh": "^5.1.5",
"bs58": "^5.0.0",
"buffer": "^6.0.3",
"typescript": "^5.1.3",
Expand Down
19 changes: 10 additions & 9 deletions packages/crypto/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
{
"compilerOptions": {
"baseUrl": "./src",
"target": "es2015",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"baseUrl": "./src",
"esModuleInterop": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"isolatedModules": true,
"lib": ["dom", "dom.iterable", "esnext"],
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"noFallthroughCasesInSwitch": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "es2015",
"types": ["node", "jest"]
},
"include": ["src"],
Expand Down
Loading

0 comments on commit 1593ceb

Please sign in to comment.