Skip to content

Commit

Permalink
refactor: cleanup after update to 0.21.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Aug 8, 2023
1 parent 692ee7d commit 77cab9a
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 245 deletions.
15 changes: 0 additions & 15 deletions apps/extension/src/background/keyring/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
} from "./messages";
import {
ConnectInterfaceMsg,
EncodeInitAccountMsg,
QueryAccountsMsg,
QueryBalancesMsg,
SubmitIbcTransferMsg,
Expand Down Expand Up @@ -89,11 +88,6 @@ export const getHandler: (service: KeyRingService) => Handler = (service) => {
env,
msg as SubmitIbcTransferMsg
);
case EncodeInitAccountMsg:
return handleEncodeInitAccountMsg(service)(
env,
msg as EncodeInitAccountMsg
);
case CloseOffscreenDocumentMsg:
return handleCloseOffscreenDocumentMsg(service)(
env,
Expand Down Expand Up @@ -258,15 +252,6 @@ const handleSubmitIbcTransferMsg: (
};
};

const handleEncodeInitAccountMsg: (
service: KeyRingService
) => InternalHandler<EncodeInitAccountMsg> = (service) => {
return (_, msg) => {
const { address, txMsg } = msg;
return service.encodeInitAccount(txMsg, address);
};
};

const handleSetActiveAccountMsg: (
service: KeyRingService
) => InternalHandler<SetActiveAccountMsg> = (service) => {
Expand Down
2 changes: 0 additions & 2 deletions apps/extension/src/background/keyring/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
} from "./messages";
import {
ConnectInterfaceMsg,
EncodeInitAccountMsg,
QueryAccountsMsg,
QueryBalancesMsg,
SubmitIbcTransferMsg,
Expand All @@ -38,7 +37,6 @@ export function init(router: Router, service: KeyRingService): void {
router.registerMessage(CloseOffscreenDocumentMsg);
router.registerMessage(ConnectInterfaceMsg);
router.registerMessage(DeriveAccountMsg);
router.registerMessage(EncodeInitAccountMsg);
router.registerMessage(GenerateMnemonicMsg);
router.registerMessage(GetActiveAccountMsg);
router.registerMessage(LockKeyRingMsg);
Expand Down
10 changes: 1 addition & 9 deletions apps/extension/src/background/keyring/keyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export class KeyRing {
parentId: string
): DerivedAccountInfo {
const { index = 0 } = path;
const id = generateId("shielded-account", parentId, index);
const id = generateId(UUID_NAMESPACE, "shielded-account", parentId, index);
const zip32 = new ShieldedHDWallet(seed);
const account = zip32.derive_to_serialized_keys(index);

Expand Down Expand Up @@ -593,14 +593,6 @@ export class KeyRing {
return getAccountValuesFromStore(accounts);
}

//TOOD: remove
async encodeInitAccount(
_address: string,
_txMsg: Uint8Array
): Promise<Uint8Array> {
return Promise.resolve(new Uint8Array());
}

async submitBond(txMsg: Uint8Array): Promise<void> {
if (!this._password) {
throw new Error("Not authenticated!");
Expand Down
13 changes: 0 additions & 13 deletions apps/extension/src/background/keyring/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,19 +316,6 @@ export class KeyRingService {
}
}

/**
* Creating an InitAccount for Namada requires a secret,
* therefore, we need to query the private key for this account from
* storage
*/
async encodeInitAccount(txMsg: string, address: string): Promise<string> {
const tx_data = await this._keyRing.encodeInitAccount(
address,
fromBase64(txMsg)
);
return toBase64(tx_data);
}

async setActiveAccount(id: string, type: ParentAccount): Promise<void> {
await this._keyRing.setActiveAccount(id, type);
await this.broadcastAccountsChanged();
Expand Down
7 changes: 5 additions & 2 deletions apps/extension/src/background/ledger/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export class LedgerService {
TxMsgValue
);

if (!publicKey) {
throw new Error("Public key not found in txMsg");
}

// Query account from Ledger storage to determine path for signer
const account = await this._ledgerStore.getRecord("publicKey", publicKey);

Expand All @@ -59,8 +63,7 @@ export class LedgerService {
const bytes = await this.sdk.build_tx(
TxType.RevealPK,
fromBase64(txMsg),
//TODO:
publicKey as string
publicKey
);
const path = makeBip44Path(coinType, account.path);

Expand Down
38 changes: 14 additions & 24 deletions apps/extension/src/provider/InjectedNamada.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
Signer as ISigner,
TxMsgProps,
} from "@namada/types";
import {InjectedProxy} from "./InjectedProxy";
import {Signer} from "./Signer";
import { InjectedProxy } from "./InjectedProxy";
import { Signer } from "./Signer";

export class InjectedNamada implements INamada {
constructor(private readonly _version: string) {}
Expand Down Expand Up @@ -40,10 +40,10 @@ export class InjectedNamada implements INamada {

public async balances(
owner: string
): Promise<{token: string; amount: string}[]> {
): Promise<{ token: string; amount: string }[]> {
return await InjectedProxy.requestMethod<
string,
{token: string; amount: string}[]
{ token: string; amount: string }[]
>("balances", owner);
}

Expand All @@ -52,9 +52,9 @@ export class InjectedNamada implements INamada {
}

public async submitBond(props: TxMsgProps): Promise<void> {
const {txMsg, type} = props;
const { txMsg, type } = props;
return await InjectedProxy.requestMethod<
{txMsg: string; type: AccountType},
{ txMsg: string; type: AccountType },
void
>("submitBond", {
txMsg,
Expand All @@ -63,28 +63,28 @@ export class InjectedNamada implements INamada {
}

public async submitUnbond(props: TxMsgProps): Promise<void> {
const {txMsg, type} = props;
const { txMsg, type } = props;
return await InjectedProxy.requestMethod<
{txMsg: string; type: AccountType},
{ txMsg: string; type: AccountType },
void
>("submitUnbond", {txMsg, type});
>("submitUnbond", { txMsg, type });
}

public async submitWithdraw(props: TxMsgProps): Promise<void> {
const {txMsg, type} = props;
const { txMsg, type } = props;
return await InjectedProxy.requestMethod<
{txMsg: string; type: AccountType},
{ txMsg: string; type: AccountType },
void
>("submitWithdraw", {txMsg, type});
>("submitWithdraw", { txMsg, type });
}

public async submitTransfer(props: {
txMsg: string;
type: AccountType;
}): Promise<void> {
const {txMsg, type} = props;
const { txMsg, type } = props;
return await InjectedProxy.requestMethod<
{txMsg: string; type: AccountType},
{ txMsg: string; type: AccountType },
void
>("submitTransfer", {
txMsg,
Expand All @@ -99,16 +99,6 @@ export class InjectedNamada implements INamada {
);
}

public async encodeInitAccount(props: {
txMsg: string;
address: string;
}): Promise<string | undefined> {
return await InjectedProxy.requestMethod<
{txMsg: string; address: string},
string
>("encodeInitAccount", props);
}

public version(): string {
return this._version;
}
Expand Down
17 changes: 0 additions & 17 deletions apps/extension/src/provider/Namada.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,4 @@ describe("Namada", () => {

await expect(res).resolves.not.toBeDefined();
});

// This test shows that init account is NOT working - it is also unused.
// We will have to change assertion after fixing initAccount fn
it("should THROW AN ERROR on encode init account", async () => {
const accountMsgValue = new AccountMsgValue({
vpCode: new Uint8Array(),
});
const accountMessage = new Message<AccountMsgValue>();
const serialized = accountMessage.encode(accountMsgValue);

await expect(
namada.encodeInitAccount({
txMsg: toBase64(serialized),
address: keyStore[0].address,
})
).rejects.toThrow();
});
});
12 changes: 0 additions & 12 deletions apps/extension/src/provider/Namada.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
GetChainMsg,
GetChainsMsg,
SuggestChainMsg,
EncodeInitAccountMsg,
QueryAccountsMsg,
FetchAndStoreMaspParamsMsg,
HasMaspParamsMsg,
Expand Down Expand Up @@ -140,17 +139,6 @@ export class Namada implements INamada {
);
}

public async encodeInitAccount(props: {
txMsg: string;
address: string;
}): Promise<string | undefined> {
const { txMsg, address } = props;
return await this.requester?.sendMessage(
Ports.Background,
new EncodeInitAccountMsg(txMsg, address)
);
}

public version(): string {
return this._version;
}
Expand Down
24 changes: 1 addition & 23 deletions apps/extension/src/provider/Signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import { toBase64 } from "@cosmjs/encoding";
import {
Account,
Namada,
AccountMsgValue,
IbcTransferMsgValue,
IbcTransferProps,
InitAccountProps,
Message,
Signer as ISigner,
TransferMsgValue,
Expand All @@ -21,7 +19,7 @@ export class Signer implements ISigner {
constructor(
protected readonly chainId: string,
private readonly _namada: Namada
) { }
) {}

public async accounts(): Promise<Account[] | undefined> {
return (await this._namada.accounts(this.chainId))?.map(
Expand Down Expand Up @@ -114,24 +112,4 @@ export class Signer implements ISigner {
toBase64(serializedIbcTransfer)
);
}

/**
* Encode an InitAccount message
*/
public async encodeInitAccount(
args: InitAccountProps,
signer: string
): Promise<string | undefined> {
const { vpCode } = args;
const accountMsgValue = new AccountMsgValue({
vpCode,
});
const accountMessage = new Message<AccountMsgValue>();
const serialized = accountMessage.encode(accountMsgValue);

return await this._namada.encodeInitAccount({
txMsg: toBase64(serialized),
address: signer,
});
}
}
29 changes: 0 additions & 29 deletions apps/extension/src/provider/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ enum MessageType {
QueryBalances = "query-balances",
SubmitIbcTransfer = "submit-ibc-transfer",
SubmitLedgerTransfer = "submit-ledger-transfer",
EncodeInitAccount = "encode-init-account",
EncodeRevealPublicKey = "encode-reveal-public-key",
GetChain = "get-chain",
GetChains = "get-chains",
Expand Down Expand Up @@ -207,34 +206,6 @@ export class SubmitIbcTransferMsg extends Message<void> {
}
}

export class EncodeInitAccountMsg extends Message<string> {
public static type(): MessageType {
return MessageType.EncodeInitAccount;
}

constructor(public readonly txMsg: string, public readonly address: string) {
super();
}

validate(): void {
if (!this.address) {
throw new Error("An address is required!");
}
if (!this.txMsg) {
throw new Error("An encoded txMsg is required!");
}
return;
}

route(): string {
return Route.KeyRing;
}

type(): string {
return EncodeInitAccountMsg.type();
}
}

export class ApproveTransferMsg extends Message<void> {
public static type(): MessageType {
return MessageType.ApproveTransfer;
Expand Down
Loading

0 comments on commit 77cab9a

Please sign in to comment.