Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate solana to base64 #503

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"dependencies": {
"@metamask/eth-sig-util": "^4.0.1",
"@solana/web3.js": "^1.5.0",
"bs58": "^4.0.1",
"buffer": "^5.6.0",
"events": "^3.2.0",
"isutf8": "^3.1.1"
Expand Down
16 changes: 12 additions & 4 deletions src/solana_provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

import BaseProvider from "./base_provider";
import * as Web3 from "@solana/web3.js";
import bs58 from "bs58";
import Utils from "./utils";
import ProviderRpcError from "./error";
import { Buffer } from "buffer";

const { PublicKey, Connection } = Web3;

Expand Down Expand Up @@ -67,7 +67,7 @@ class TrustSolanaWeb3Provider extends BaseProvider {

mapSignedTransaction(tx, signatureEncoded) {
const version = typeof tx.version !== "number" ? "legacy" : tx.version;
const signature = bs58.decode(signatureEncoded);
const signature = this.decodeFromBase64(signatureEncoded);

tx.addSignature(this.publicKey, signature);

Expand All @@ -86,7 +86,7 @@ class TrustSolanaWeb3Provider extends BaseProvider {
const data = JSON.stringify(tx);
const version = typeof tx.version !== "number" ? "legacy" : tx.version;

const raw = bs58.encode(
const raw = this.encodeToBase64(
version === "legacy" ? tx.serializeMessage() : version === 0 ? tx.message.serialize() : tx.serialize()
);

Expand All @@ -109,7 +109,7 @@ class TrustSolanaWeb3Provider extends BaseProvider {
const data = JSON.stringify(tx);
const version = typeof tx.version !== "number" ? "legacy" : tx.version;

const raw = bs58.encode(
const raw = this.encodeToBase64(
version === "legacy" ? tx.serializeMessage() : version === 0 ? tx.message.serialize() : tx.serialize()
);

Expand Down Expand Up @@ -181,6 +181,14 @@ class TrustSolanaWeb3Provider extends BaseProvider {
}
});
}

encodeToBase64(data) {
return Buffer.from(data).toString("base64");
}

decodeFromBase64(encodedData) {
return Buffer.from(encodedData, "base64").toString();
}
}

module.exports = TrustSolanaWeb3Provider;
12 changes: 12 additions & 0 deletions src/tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@ describe("TrustWeb3Provider constructor tests", () => {
});
});

test("base64test", () => {
const provider = new trustwallet.SolanaProvider({
solana: {
cluster: "mainnet-beta",
isPhantom: false,
},
isDebug: true,
});
expect(provider.encodeToBase64("11111")).toEqual("MTExMTE=");
expect(provider.decodeFromBase64("MTExMTE=")).toEqual("11111");
});

test("test batched sendAsync", (done) => {
const provider = new trustwallet.Provider({ ethereum: bsc });
const web3 = new Web3(provider);
Expand Down
Loading