Skip to content

Commit

Permalink
Release/5.18.4 (#1272)
Browse files Browse the repository at this point in the history
* replaces all remaining instances of wallet-sdk

* tweaks tx detail row layouts to avoid overflows

* adds getContractSpec, uses it to display parameter names in tx sign detail view

* migrates KeyManager to ts-wallet-sdk

* upgrades react and react dom types to 18

* adds react types to extension workspace, fixes type errors

* Added translations

* adds standalone version of getContractSpec and related helpers

* Added translations

* Added translations

* undo husky pre push comments

* Feature/p21 futurenet release (#1278)

* switch between stellar-sdk and stellar-sdk-next based on network

* increase max diff pixel ratio for playwright

* rm console

* adds tx timeout in send and swap settings

* Added translations

* tweaks tooltip text

* Added translations

* fix issue with metric middleware not running on page load (#1280)

* renders byte arrays in hex

* removes only used in test debugging

* alert on unheathy rpc (#1281)

* tweaks failed to fetch token sentry exceptions

---------

Co-authored-by: Piyal Basu <pbasu235@gmail.com>
  • Loading branch information
aristidesstaffieri and piyalbasu authored May 13, 2024
1 parent 6992b41 commit a665065
Show file tree
Hide file tree
Showing 59 changed files with 3,540 additions and 2,965 deletions.
7 changes: 4 additions & 3 deletions @shared/api/helpers/getDomainFromIssuer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Horizon } from "stellar-sdk";
import { getSdk } from "@shared/helpers/stellar";
import { sendMessageToBackground } from "./extensionMessaging";
import { SERVICE_TYPES } from "../../constants/services";
import { NetworkDetails } from "../../constants/stellar";
Expand Down Expand Up @@ -31,9 +31,10 @@ export const getDomainFromIssuer = async ({

try {
/* Otherwise, 1. load their account from the API */
const { networkUrl } = networkDetails;
const { networkUrl, networkPassphrase } = networkDetails;
const Sdk = getSdk(networkPassphrase);

const server = new Horizon.Server(networkUrl);
const server = new Sdk.Horizon.Server(networkUrl);
response = await server.loadAccount(key);
} catch (e) {
return assetDomain;
Expand Down
4 changes: 2 additions & 2 deletions @shared/api/helpers/getIconUrlFromIssuer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ export const getIconUrlFromIssuer = async ({

try {
/* Otherwise, 1. load their account from the API */
const { networkUrl } = networkDetails;
const server = stellarSdkServer(networkUrl);
const { networkUrl, networkPassphrase } = networkDetails;
const server = stellarSdkServer(networkUrl, networkPassphrase);

response = await server.loadAccount(key);
} catch (e) {
Expand Down
115 changes: 114 additions & 1 deletion @shared/api/helpers/soroban.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Address, scValToBigInt, xdr, ScInt } from "stellar-sdk";
import {
Address,
scValToBigInt,
xdr,
ScInt,
SorobanRpc,
ContractSpec,
} from "stellar-sdk";
import { XdrReader } from "@stellar/js-xdr";

export const accountIdentifier = (account: string) =>
new Address(account).toScVal();
Expand All @@ -25,3 +33,108 @@ export const decodeU32 = (b64: string) =>

export const numberToI128 = (value: number): xdr.ScVal =>
new ScInt(value).toI128();

export const getLedgerKeyContractCode = (contractId: string) => {
const ledgerKey = xdr.LedgerKey.contractData(
new xdr.LedgerKeyContractData({
contract: new Address(contractId).toScAddress(),
key: xdr.ScVal.scvLedgerKeyContractInstance(),
durability: xdr.ContractDataDurability.persistent(),
}),
);
return ledgerKey.toXDR("base64");
};

export const getLedgerEntries = async (
entryKey: string,
rpcUrl: string,
id: number = new Date().getDate(),
): Promise<{
error: Error;
result: SorobanRpc.Api.RawGetLedgerEntriesResponse;
}> => {
let requestBody = {
jsonrpc: "2.0",
id: id,
method: "getLedgerEntries",
params: {
keys: [entryKey],
},
};

let res = await fetch(rpcUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(requestBody),
});
let json = await res.json();
if (!res.ok) {
throw new Error(json);
}
return json;
};

export const getLedgerKeyWasmId = (contractLedgerEntryData: string) => {
const contractCodeWasmHash = xdr.LedgerEntryData.fromXDR(
contractLedgerEntryData,
"base64",
)
.contractData()
.val()
.instance()
.executable()
.wasmHash();
const ledgerKey = xdr.LedgerKey.contractCode(
new xdr.LedgerKeyContractCode({
hash: contractCodeWasmHash,
}),
);
return ledgerKey.toXDR("base64");
};

export const parseWasmXdr = async (xdrContents: string) => {
const wasmBuffer = xdr.LedgerEntryData.fromXDR(xdrContents, "base64")
.contractCode()
.code();
const wasmModule = await WebAssembly.compile(wasmBuffer);
const reader = new XdrReader(
Buffer.from(
WebAssembly.Module.customSections(wasmModule, "contractspecv0")[0],
),
);

const specs = [];
do {
specs.push(xdr.ScSpecEntry.read(reader));
} while (!reader.eof);
const contractSpec = new ContractSpec(specs);
return contractSpec.jsonSchema();
};

export const getContractSpec = async (
contractId: string,
serverUrl: string,
) => {
const contractDataKey = getLedgerKeyContractCode(contractId);
const { error, result } = await getLedgerEntries(contractDataKey, serverUrl);
const entries = result.entries || [];
if (error || !entries.length) {
throw new Error("Unable to fetch contract spec");
}

const contractCodeLedgerEntryData = entries[0].xdr;
const wasmId = getLedgerKeyWasmId(contractCodeLedgerEntryData);
const { error: wasmError, result: wasmResult } = await getLedgerEntries(
wasmId,
serverUrl,
);
const wasmEntries = wasmResult.entries || [];
if (wasmError || !wasmEntries.length) {
throw new Error("Unable to fetch contract spec");
}

const spec = await parseWasmXdr(wasmEntries[0].xdr);
return spec;
};
15 changes: 12 additions & 3 deletions @shared/api/helpers/stellarSdkServer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { FeeBumpTransaction, Horizon, Transaction } from "stellar-sdk";
import { Horizon as HorizonNext } from "stellar-sdk-next";

import { getSdk } from "@shared/helpers/stellar";

interface HorizonError {
response: {
status: number;
};
}

const isHorizonError = (val: unknown): val is HorizonError =>
typeof val === "object" &&
val !== null &&
Expand All @@ -16,16 +20,21 @@ const isHorizonError = (val: unknown): val is HorizonError =>
export const getIsAllowHttp = (networkUrl: string) =>
!networkUrl.includes("https");

export const stellarSdkServer = (networkUrl: string) =>
new Horizon.Server(networkUrl, {
export const stellarSdkServer = (
networkUrl: string,
networkPassphrase: string,
) => {
const Sdk = getSdk(networkPassphrase);
return new Sdk.Horizon.Server(networkUrl, {
allowHttp: getIsAllowHttp(networkUrl),
});
};

export const submitTx = async ({
server,
tx,
}: {
server: Horizon.Server;
server: Horizon.Server | HorizonNext.Server;
tx: Transaction | FeeBumpTransaction;
}): Promise<any> => {
let submittedTx;
Expand Down
Loading

0 comments on commit a665065

Please sign in to comment.