Skip to content

Commit

Permalink
Release/5.2.6 (#922)
Browse files Browse the repository at this point in the history
* API signTransaction accepts arbitrary blobs (#912)

* adds signBlob workflows and allows for signTransaction to take a b64 blob

* cleans up SignTransaction and splits blob and tx signing review

* cleans up SignTransaction and splits blob and tx signing review

* Added translations

* add domain to blob signing, split blob and tx signing body components

* Added translations

* refactors SignTransaction to abstract common state to parent

* adds new api, signBlob

* splits out sign blob and sign tx bodies

* restore var name in signTransaction

* adds blob warning and blob preview to sign blob flow

* Added translations

* fixes data blob styling and placement

* Added translations

* manually format changes

* fixes grammar in signing warning

* Added translations

* Feature/wal 509 remove domain connection (#911)

* update slack notify as old version was erroring

* add UI to remove connected domains

* Added translations

* add a separate service for saveAllowList and add an empty state

* clean up allowList value that UI receives

* fixes bad references to request/response keys in the listener layer

* renames args, drops unused args, and splits Request interfaces for tx and blobs

* renames forgotten reference in response handler

* makes options optional for signBlob

* bump freighter-api version for next release (#925)

* set current account after accountMap has been generated (#927)

* set current account after accountMap has been generated

* de-dupe accountsMap logic

---------

Co-authored-by: aristides <[email protected]>
  • Loading branch information
piyalbasu and aristidesstaffieri authored Aug 3, 2023
1 parent 7946a03 commit 42a5dc0
Show file tree
Hide file tree
Showing 37 changed files with 1,239 additions and 363 deletions.
26 changes: 26 additions & 0 deletions @shared/api/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,32 @@ export const submitTransaction = async (
return signedTransaction;
};

export const submitBlob = async (
blob: string,
opts?: {
accountToSign?: string;
},
): Promise<string> => {
let response = { signedBlob: "", error: "" };
const _opts = opts || {};
const accountToSign = _opts.accountToSign || "";
try {
response = await sendMessageToContentScript({
blob,
accountToSign,
type: EXTERNAL_SERVICE_TYPES.SUBMIT_BLOB,
});
} catch (e) {
console.error(e);
}
const { signedBlob, error } = response;

if (error) {
throw error;
}
return signedBlob;
};

export const requestNetwork = async (): Promise<string> => {
let response = { network: "", error: "" };
try {
Expand Down
32 changes: 32 additions & 0 deletions @shared/api/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,16 @@ export const signTransaction = async (): Promise<void> => {
}
};

export const signBlob = async (): Promise<void> => {
try {
await sendMessageToBackground({
type: SERVICE_TYPES.SIGN_BLOB,
});
} catch (e) {
console.error(e);
}
};

export const signFreighterTransaction = async ({
transactionXDR,
network,
Expand Down Expand Up @@ -674,6 +684,27 @@ export const showBackupPhrase = async (
return response;
};

export const saveAllowList = async ({
allowList,
}: {
allowList: string[];
}): Promise<{ allowList: string[] }> => {
let response = {
allowList: [""],
};

try {
response = await sendMessageToBackground({
allowList,
type: SERVICE_TYPES.SAVE_ALLOWLIST,
});
} catch (e) {
console.error(e);
}

return response;
};

export const saveSettings = async ({
isDataSharingAllowed,
isMemoValidationEnabled,
Expand All @@ -688,6 +719,7 @@ export const saveSettings = async ({
isExperimentalModeEnabled: boolean;
}): Promise<Settings> => {
let response = {
allowList: [""],
isDataSharingAllowed: false,
networkDetails: MAINNET_NETWORK_DETAILS,
networksList: DEFAULT_NETWORKS,
Expand Down
16 changes: 14 additions & 2 deletions @shared/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface Response {
};
transactionXDR: string;
signedTransaction: string;
signedBlob: string;
source: string;
type: SERVICE_TYPES;
url: string;
Expand Down Expand Up @@ -71,6 +72,7 @@ export interface Response {
isConnected: boolean;
isAllowed: boolean;
userInfo: UserInfo;
allowList: string[];
}

export interface BlockedDomains {
Expand All @@ -84,14 +86,23 @@ export interface BlockedAccount {
tags: string[];
}

export interface ExternalRequest {
transactionXdr: string;
export interface ExternalRequestBase {
network: string;
networkPassphrase: string;
accountToSign: string;
type: EXTERNAL_SERVICE_TYPES;
}

export interface ExternalRequestTx extends ExternalRequestBase {
transactionXdr: string;
}

export interface ExternalRequestBlob extends ExternalRequestBase {
blob: string;
}

export type ExternalRequest = ExternalRequestTx | ExternalRequestBlob;

export interface Account {
publicKey: string;
name: string;
Expand All @@ -116,6 +127,7 @@ export interface Preferences {
}

export type Settings = {
allowList: string[];
networkDetails: NetworkDetails;
networksList: NetworkDetails[];
error: string;
Expand Down
3 changes: 3 additions & 0 deletions @shared/constants/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export enum SERVICE_TYPES {
REJECT_ACCESS = "REJECT_ACCESS",
GRANT_ACCESS = "GRANT_ACCESS",
SIGN_TRANSACTION = "SIGN_TRANSACTION",
SIGN_BLOB = "SIGN_BLOB",
HANDLE_SIGNED_HW_TRANSACTION = "HANDLE_SIGNED_HW_TRANSACTION",
REJECT_TRANSACTION = "REJECT_TRANSACTION",
SIGN_FREIGHTER_TRANSACTION = "SIGN_FREIGHTER_TRANSACTION",
Expand All @@ -22,6 +23,7 @@ export enum SERVICE_TYPES {
LOAD_RECENT_ADDRESSES = "LOAD_RECENT_ADDRESSES",
SIGN_OUT = "SIGN_OUT",
SHOW_BACKUP_PHRASE = "SHOW_BACKUP_PHRASE",
SAVE_ALLOWLIST = "SAVE_ALLOWLIST",
SAVE_SETTINGS = "SAVE_SETTINGS",
LOAD_SETTINGS = "LOAD_SETTINGS",
GET_CACHED_ASSET_ICON = "GET_CACHED_ASSET_ICON",
Expand All @@ -42,6 +44,7 @@ export enum SERVICE_TYPES {
export enum EXTERNAL_SERVICE_TYPES {
REQUEST_ACCESS = "REQUEST_ACCESS",
SUBMIT_TRANSACTION = "SUBMIT_TRANSACTION",
SUBMIT_BLOB = "SUBMIT_BLOB",
REQUEST_NETWORK = "REQUEST_NETWORK",
REQUEST_NETWORK_DETAILS = "REQUEST_NETWORK_DETAILS",
REQUEST_CONNECTION_STATUS = "REQUEST_CONNECTION_STATUS",
Expand Down
Binary file added @stellar/freighter-api/api.tgz
Binary file not shown.
2 changes: 1 addition & 1 deletion @stellar/freighter-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stellar/freighter-api",
"version": "1.5.1",
"version": "1.6.0",
"license": "Apache-2.0",
"author": "Stellar Development Foundation <[email protected]>",
"description": "Utility functions to interact with Freighter extension",
Expand Down
1 change: 1 addition & 0 deletions @stellar/freighter-api/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ describe("freighter API", () => {
expect(typeof FreighterAPI.isConnected).toBe("function");
expect(typeof FreighterAPI.getPublicKey).toBe("function");
expect(typeof FreighterAPI.signTransaction).toBe("function");
expect(typeof FreighterAPI.signBlob).toBe("function");
});
});
18 changes: 18 additions & 0 deletions @stellar/freighter-api/src/__tests__/signBlob.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as apiExternal from "@shared/api/external";
import { signBlob } from "../signBlob";

describe("signBlob", () => {
it("returns a signed blob", async () => {
const TEST_BLOB = atob("AAA");
apiExternal.submitBlob = jest.fn().mockReturnValue(TEST_BLOB);
const blob = await signBlob();
expect(blob).toBe(TEST_BLOB);
});
it("throws a generic error", () => {
const TEST_ERROR = "Error!";
apiExternal.submitBlob = jest.fn().mockImplementation(() => {
throw TEST_ERROR;
});
expect(signBlob).toThrowError(TEST_ERROR);
});
});
3 changes: 3 additions & 0 deletions @stellar/freighter-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getPublicKey } from "./getPublicKey";
import { signTransaction } from "./signTransaction";
import { signBlob } from "./signBlob";
import { isConnected } from "./isConnected";
import { getNetwork } from "./getNetwork";
import { getNetworkDetails } from "./getNetworkDetails";
Expand All @@ -12,6 +13,7 @@ export const isBrowser = typeof window !== "undefined";
export {
getPublicKey,
signTransaction,
signBlob,
isConnected,
getNetwork,
getNetworkDetails,
Expand All @@ -22,6 +24,7 @@ export {
export default {
getPublicKey,
signTransaction,
signBlob,
isConnected,
getNetwork,
getNetworkDetails,
Expand Down
10 changes: 10 additions & 0 deletions @stellar/freighter-api/src/signBlob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { submitBlob } from "@shared/api/external";
import { isBrowser } from ".";

export const signBlob = (
blob: string,
opts?: {
accountToSign?: string;
}
): Promise<string> =>
isBrowser ? submitBlob(blob, opts) : Promise.resolve("");
Binary file not shown.
11 changes: 11 additions & 0 deletions docs/docs/guide/usingFreighterNode.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
isConnected,
getPublicKey,
signTransaction,
signBlob,
} from "@stellar/freighter-api";
```

Expand Down Expand Up @@ -82,6 +83,7 @@ import {
isConnected,
getPublicKey,
signTransaction,
signBlob,
} from "@stellar/freighter-api";

if (await isConnected()) {
Expand Down Expand Up @@ -123,6 +125,7 @@ import {
setAllowed,
getUserInfo,
signTransaction,
signBlob,
} from "@stellar/freighter-api";

if (await isConnected()) {
Expand Down Expand Up @@ -175,6 +178,7 @@ import {
isConnected,
getNetwork,
signTransaction,
signBlob,
} from "@stellar/freighter-api";

if (await isConnected()) {
Expand Down Expand Up @@ -219,11 +223,18 @@ These 2 configurations are useful in the case that the user's Freighter is confi

You can also use this `opts` to specify which account's signature you’re requesting. If Freighter has the public key requested, it will switch to that account. If not, it will alert the user that they do not have the requested account.

### signBlob

#### `signBlob(xdr: string, opts: { network: string, networkPassphrase: string, accountToSign: string }) -> <Promise<string>>`

This is the same as `signTransaction` but accepts a base64 encoded blob.

```javascript
import {
isConnected,
getPublicKey,
signTransaction,
signBlob
} from "@stellar/freighter-api";

if (await isConnected()) {
Expand Down
12 changes: 12 additions & 0 deletions extension/src/background/helpers/account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ACCOUNT_NAME_LIST_ID,
ALLOWLIST_ID,
KEY_ID_LIST,
KEY_ID,
IS_VALIDATING_MEMO_ID,
Expand Down Expand Up @@ -63,6 +64,17 @@ export const getIsFuturenet = async () => {
return isFuturenet(networkDetails);
};

export const getAllowList = async () => {
const allowList = (await localStore.getItem(ALLOWLIST_ID)) || "";

if (allowList === "") {
// manually return an empty array as calling .split(",") will return [""]
return [];
}

return allowList.split(",");
};

export const getIsMemoValidationEnabled = async () =>
(await localStore.getItem(IS_VALIDATING_MEMO_ID)) ?? true;

Expand Down
Loading

0 comments on commit 42a5dc0

Please sign in to comment.