Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f3ef097
update stellar-sdk for Protocol 19 (#466)
piyalbasu Apr 29, 2022
4426737
add additional tracking for payments and manage assets (#482)
piyalbasu May 11, 2022
8b0c907
when fetching balances filter out liquidity pool shares (#484)
acharb May 12, 2022
0770e01
use asset canonical ID for key in cache (#495)
acharb May 17, 2022
4749fad
add custom select inputs (#502)
acharb May 20, 2022
148805b
use recommended transaction fee by default (#503)
acharb May 23, 2022
e34de09
Merge commit 'd226474d72a8ea83915f302e6eace2986c4e3f89' into release/…
piyalbasu May 24, 2022
dcb5f15
merging 2.3.0 into 2.2.0 - swaps, removing network from onboarding sc…
acharb Jun 9, 2022
3a0a7b9
add a search asset view (#517)
piyalbasu Jun 10, 2022
c12e59d
update the collectedTags array (#524)
acharb Jun 10, 2022
374aede
merge conflicts when merging master
piyalbasu Jun 13, 2022
c6cd007
adding default and loading icons (#525)
piyalbasu Jun 14, 2022
6819df5
make sendSettings rows clickable, add pin extension view (#527)
acharb Jun 14, 2022
eb62cfd
Feature/request public key (#528)
piyalbasu Jun 15, 2022
e6eec09
fix sendAmount header moving (#532)
acharb Jun 16, 2022
84a42bf
qa fixes (#533)
acharb Jun 22, 2022
a9f1fba
show the loader while we're waiting for asset icons to be fetched; ad…
piyalbasu Jun 23, 2022
6da9bf8
New styles for mnemonic phrase confirmed screen (#536)
piyalbasu Jun 23, 2022
0b475ee
load assetIcons from swap screen, increase padding in accounst select…
acharb Jun 24, 2022
b7fa2f3
adjust border radius for Path/Swap asset selects (#539)
piyalbasu Jun 25, 2022
a752c28
use first non-xlm asset as default dest asset for swaps (#540)
acharb Jun 27, 2022
e04a749
Bugfix/allow null network (#545)
piyalbasu Jun 28, 2022
6a05f40
Bugfix/allow muxed federation in sign transaction (#546)
piyalbasu Jun 28, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion @shared/api/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const requestPublicKey = async (): Promise<string> => {

export const submitTransaction = async (
transactionXdr: string,
network?: string,
network?: string | null,
accountToSign?: string,
): Promise<string> => {
let response = { signedTransaction: "", error: "" };
if (network && network !== NETWORKS.PUBLIC && network !== NETWORKS.TESTNET) {
Expand All @@ -33,6 +34,7 @@ export const submitTransaction = async (
response = await sendMessageToContentScript({
transactionXdr,
network,
accountToSign,
type: EXTERNAL_SERVICE_TYPES.SUBMIT_TRANSACTION,
});
} catch (e) {
Expand Down
1 change: 1 addition & 0 deletions @shared/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface Response {
export interface ExternalRequest {
transactionXdr: string;
network: string;
accountToSign: string;
type: EXTERNAL_SERVICE_TYPES;
}

Expand Down
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.1.2",
"version": "1.2.0",
"license": "Apache-2.0",
"author": "Stellar Development Foundation <hello@stellar.org>",
"description": "Utility functions to interact with Freighter extension",
Expand Down
5 changes: 3 additions & 2 deletions @stellar/freighter-api/src/signTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import { submitTransaction } from "@shared/api/external";

export const signTransaction = (
transactionXdr: string,
network?: "PUBLIC" | "TESTNET"
) => submitTransaction(transactionXdr, network);
network?: "PUBLIC" | "TESTNET" | null,
accountToSign?: string
) => submitTransaction(transactionXdr, network, accountToSign);
26 changes: 21 additions & 5 deletions docs/docs/guide/usingFreighterBrowser.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ The user will need to provide their password if the extension does not currently

_NOTE:_ The user must provide a valid transaction XDR string for the extension to properly sign.

The second parameter is an optional string that you may pass to indicate what network you’re intending this transaction to be signed on. The parameter must be either `PUBLIC` or `TESTNET`. If you choose not to pass a param, freighter-api will default to `PUBLIC`.
The second parameter is an optional string that you may pass to indicate what network you’re intending this transaction to be signed on. The network must be either `PUBLIC` or `TESTNET`. If you choose not to pass a network, freighter-api will default to `PUBLIC`. You may also pass `null` here if you choose not to pass a network param, but you would like to pass the third param available.

This is useful in the case that the user's Freighter is configured to the wrong network. Freighter will be able to throw a blocking error message communicating that you intended this transaction to be signed on a different network.

Expand Down Expand Up @@ -137,12 +137,20 @@ const retrievePublicKey = async () => {

const retrievedPublicKey = await retrievePublicKey();

const userSignTransaction = async (xdr: string, network: string) => {
const userSignTransaction = async (
xdr: string,
network: string,
signWith: string
) => {
let signedTransaction = "";
let error = "";

try {
signedTransaction = await window.freighterApi.signTransaction(xdr, network);
signedTransaction = await window.freighterApi.signTransaction(
xdr,
network,
signWith
);
} catch (e) {
error = e;
}
Expand All @@ -161,12 +169,20 @@ const userSignedTransaction = await userSignTransaction(xdr, "TESTNET");
freighter-api will return a signed transaction xdr. Below is an example of how you might submit this signed transaction to Horizon using `stellar-sdk` (https://github.com/stellar/js-stellar-sdk):

```javascript
const userSignTransaction = async (xdr: string, network: string) => {
const userSignTransaction = async (
xdr: string,
network: string,
signWith: string
) => {
let signedTransaction = "";
let error = "";

try {
signedTransaction = await window.freighterApi.signTransaction(xdr, network);
signedTransaction = await window.freighterApi.signTransaction(
xdr,
network,
signWith
);
} catch (e) {
error = e;
}
Expand Down
22 changes: 16 additions & 6 deletions docs/docs/guide/usingFreighterNode.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,20 @@ const result = retrieveNetwork();

### signTransaction

#### `signTransaction(xdr: string, network:? string) -> <Promise<string>>`
#### `signTransaction(xdr: string, network:? string, publicKey:? string) -> <Promise<string>>`

This function accepts a transaction XDR string as the first parameter, which it will decode, sign as the user, and then return the signed transaction to your application.

The user will need to provide their password if the extension does not currently have their private key. Once the user has provided their password, the extension will have access to the user private key for 5 minutes. The user must then review the transaction details and accept within those 5 minutes for the transaction to be signed.

_NOTE:_ The user must provide a valid transaction XDR string for the extension to properly sign.

The second parameter is an optional string that you may pass to indicate what network you’re intending this transaction to be signed on. The parameter must be either `PUBLIC` or `TESTNET`. If you choose not to pass a param, freighter-api will default to `PUBLIC`.
The second parameter is an optional string that you may pass to indicate what network you’re intending this transaction to be signed on. The network must be either `PUBLIC` or `TESTNET`. If you choose not to pass a network, freighter-api will default to `PUBLIC`. You may also pass `null` here if you choose not to pass a network param, but you would like to pass the third param available.

This is useful in the case that the user's Freighter is configured to the wrong network. Freighter will be able to throw a blocking error message communicating that you intended this transaction to be signed on a different network.

The third parameter is another optional parameter that gives you the ability to specify which account's signature you’re requesting. If Freighter has the public key, it will switch to that account. If not, it will alert the user that they do not have the requested account.

```javascript
import {
isConnected,
Expand Down Expand Up @@ -159,12 +161,16 @@ const retrievePublicKey = async () => {

const retrievedPublicKey = retrievePublicKey();

const userSignTransaction = async (xdr: string, network: string) => {
const userSignTransaction = async (
xdr: string,
network: string,
signWith: string
) => {
let signedTransaction = "";
let error = "";

try {
signedTransaction = await signTransaction(xdr, network);
signedTransaction = await signTransaction(xdr, network, signWith);
} catch (e) {
error = e;
}
Expand All @@ -185,12 +191,16 @@ freighter-api will return a signed transaction xdr. Below is an example of how y
```javascript
import StellarSdk from "stellar-sdk";

const userSignTransaction = async (xdr: string, network: string) => {
const userSignTransaction = async (
xdr: string,
network: string,
signWith: string
) => {
let signedTransaction = "";
let error = "";

try {
signedTransaction = await signTransaction(xdr, network);
signedTransaction = await signTransaction(xdr, network, signWith);
} catch (e) {
error = e;
}
Expand Down
30 changes: 26 additions & 4 deletions docs/docs/playground/components/SignTransactionDemo.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
import React, { useState } from "react";
import { signTransaction } from "@stellar/freighter-api";
import { PlaygroundTextarea } from "./basics/inputs";
import { PlaygroundInput, PlaygroundTextarea } from "./basics/inputs";

export const SignTransactionDemo = () => {
const [transactionXdr, setTransactionXdr] = useState("");
const [network, setNetwork] = useState("");
const [publicKey, setPublicKey] = useState("");
const [transactionResult, setTransactionResult] = useState("");
const inputOnChangeHandler = (e: React.ChangeEvent<HTMLTextAreaElement>) => {

const xdrOnChangeHandler = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
setTransactionXdr(e.currentTarget.value);
};
const networkOnChangeHandler = (e: React.ChangeEvent<HTMLInputElement>) => {
setNetwork(e.currentTarget.value);
};
const publicKeyOnChangeHandler = (e: React.ChangeEvent<HTMLInputElement>) => {
setPublicKey(e.currentTarget.value);
};

const btnHandler = async () => {
let signedTransaction;
let error = "";

try {
signedTransaction = await signTransaction(transactionXdr);
signedTransaction = await signTransaction(
transactionXdr,
network === "PUBLIC" || network === "TESTNET" ? network : null,
publicKey
);
} catch (e) {
error = e;
}
Expand All @@ -23,7 +37,15 @@ export const SignTransactionDemo = () => {
<section>
<div>
Enter transaction XDR:
<PlaygroundTextarea onChange={inputOnChangeHandler} />
<PlaygroundTextarea onChange={xdrOnChangeHandler} />
</div>
<div>
Enter network - "TESTNET"|"PUBLIC" (optional):
<PlaygroundInput onChange={networkOnChangeHandler} />
</div>
<div>
Request signature from specific public key (optional):
<PlaygroundInput onChange={publicKeyOnChangeHandler} />
</div>
<div>
Result:
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/playground/signTransaction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ id: signTransaction
title: signTransaction
---

#### `signTransaction( transactionXdr: string )`
#### `signTransaction( transactionXdr: string, network?: "PUBLIC"|"TESTNET"|null, publicKey?: string )`

import { SignTransactionDemo } from "./components/SignTransactionDemo";

Expand Down
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@docusaurus/core": "2.0.0-alpha.68",
"@docusaurus/preset-classic": "2.0.0-alpha.68",
"@mdx-js/react": "^1.6.19",
"@stellar/freighter-api": "1.1.2",
"@stellar/freighter-api": "1.2.0",
"clsx": "^1.1.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand Down
2 changes: 1 addition & 1 deletion extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@
"webextension-polyfill-ts": "^0.19.0",
"yup": "^0.29.1"
}
}
}
14 changes: 4 additions & 10 deletions extension/public/static/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,13 @@
}
},
"background": {
"scripts": [
"background.min.js"
],
"scripts": ["background.min.js"],
"persistent": true
},
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"contentScript.min.js"
],
"matches": ["<all_urls>"],
"js": ["contentScript.min.js"],
"run_at": "document_start"
}
],
Expand All @@ -42,4 +36,4 @@
"128": "images/icon128.png"
},
"manifest_version": 2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,8 @@ export const freighterApiMessageListener = (
};

const submitTransaction = async () => {
const {
transactionXdr,
network = MAINNET_NETWORK_DETAILS.network,
} = request;
const { transactionXdr, network: _network, accountToSign } = request;
const network = _network ?? MAINNET_NETWORK_DETAILS.network;
const isTestnet = getIsTestnet();
const { networkUrl } = getNetworkDetails(isTestnet);
const transaction = StellarSdk.TransactionBuilder.fromXDR(
Expand Down Expand Up @@ -115,7 +113,7 @@ export const freighterApiMessageListener = (
accountData.forEach(
({ address, tags }: { address: string; tags: Array<string> }) => {
if (address === operation.destination) {
const collectedTags = [...tags];
let collectedTags = [...tags];

/* if the user has opted out of validation, remove applicable tags */
if (!isValidatingMemo) {
Expand All @@ -124,10 +122,10 @@ export const freighterApiMessageListener = (
);
}
if (!isValidatingSafety) {
collectedTags.filter(
collectedTags = collectedTags.filter(
(tag) => tag !== TRANSACTION_WARNING.unsafe,
);
collectedTags.filter(
collectedTags = collectedTags.filter(
(tag) => tag !== TRANSACTION_WARNING.malicious,
);
}
Expand Down Expand Up @@ -157,6 +155,7 @@ export const freighterApiMessageListener = (
isDomainListedAllowed,
url: tabUrl,
flaggedKeys,
accountToSign,
} as TransactionInfo;

transactionQueue.push(transaction);
Expand Down
14 changes: 14 additions & 0 deletions extension/src/helpers/stellar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const getTransactionInfo = (search: string) => {
const transactionInfo = parsedSearchParam(search);

const {
accountToSign,
url,
transaction,
isDomainListedAllowed,
Expand All @@ -36,6 +37,7 @@ export const getTransactionInfo = (search: string) => {
);

return {
accountToSign,
transaction,
domain: hostname,
domainTitle: title,
Expand Down Expand Up @@ -90,3 +92,15 @@ export const getConversionRate = (
sourceAmount: string,
destAmount: string,
): BigNumber => new BigNumber(destAmount).div(new BigNumber(sourceAmount));

export const formatDomain = (domain: string) => {
if (domain) {
domain.replace("https://", "").replace("www.", "");
return domain;
}
return "Stellar Network";
};

export const isMuxedAccount = (publicKey: string) => publicKey.startsWith("M");

export const isFederationAddress = (address: string) => address.includes("*");
Loading