Skip to content

Commit

Permalink
Adding solana standard support
Browse files Browse the repository at this point in the history
  • Loading branch information
y3fers0n committed Feb 20, 2024
1 parent 7034fdc commit 29ee6af
Show file tree
Hide file tree
Showing 26 changed files with 9,148 additions and 16 deletions.
1 change: 1 addition & 0 deletions adapter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib
7 changes: 7 additions & 0 deletions adapter/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"printWidth": 120,
"trailingComma": "es5",
"tabWidth": 4,
"semi": true,
"singleQuote": true
}
19 changes: 19 additions & 0 deletions adapter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# @solana/wallet-standard-ghost

## 0.1.0

### Minor Changes

- f10d202: Add `solana:signIn` (Sign In With Solana) feature

### Patch Changes

- Updated dependencies [f10d202]
- @solana/wallet-standard-features@1.1.0

## 0.0.0

### Patch Changes

- Updated dependencies [ba56499]
- @solana/wallet-standard-features@1.0.0
Empty file added adapter/README.md
Empty file.
4,684 changes: 4,684 additions & 0 deletions adapter/package-lock.json

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions adapter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"private": true,
"name": "@trustwallet/solana-wallet-standard",
"version": "0.1.0",
"author": "Trust <[email protected]>",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+ssh://[email protected]/TrustWallet/trust-web3-provider.git"
},
"engines": {
"node": ">=16"
},
"sideEffects": false,
"module": "./dist/index.js",
"scripts": {
"fmt": "prettier --write '{*,**/*}.{ts,tsx,js,jsx,json}'",
"clean": "shx mkdir -p lib && shx rm -rf lib",
"tsc": "tsc --build --verbose tsconfig.all.json",
"package": "shx mkdir -p lib/cjs && shx echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
"build": "webpack --config webpack.config.js"
},
"dependencies": {
"@solana/wallet-standard-features": "^1.1.0",
"@solana/web3.js": "^1.58.0",
"@wallet-standard/base": "^1.0.1",
"@wallet-standard/features": "^1.0.3",
"bs58": "^4.0.1"
},
"devDependencies": {
"@babel/core": "^7.23.9",
"@babel/preset-env": "^7.23.9",
"@types/bs58": "^4.0.1",
"@types/node-fetch": "^2.6.2",
"babel-loader": "^9.1.3",
"prettier": "^2.7.1",
"shx": "^0.3.4",
"stream-browserify": "^3.0.0",
"ts-loader": "^9.5.1",
"typescript": "^4.8.4",
"webpack": "^5.90.3",
"webpack-cli": "^5.1.4"
}
}
58 changes: 58 additions & 0 deletions adapter/src/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// This is copied with modification from @wallet-standard/wallet

import {
SolanaSignAndSendTransaction,
SolanaSignMessage,
SolanaSignTransaction,
} from '@solana/wallet-standard-features';
import type { WalletAccount } from '@wallet-standard/base';
import { SOLANA_CHAINS } from './solana';

const chains = SOLANA_CHAINS;
const features = [SolanaSignAndSendTransaction, SolanaSignTransaction, SolanaSignMessage] as const;

export class TrustWalletAccount implements WalletAccount {
readonly #address: WalletAccount['address'];
readonly #publicKey: WalletAccount['publicKey'];
readonly #chains: WalletAccount['chains'];
readonly #features: WalletAccount['features'];
readonly #label: WalletAccount['label'];
readonly #icon: WalletAccount['icon'];

get address() {
return this.#address;
}

get publicKey() {
return this.#publicKey.slice();
}

get chains() {
return this.#chains.slice();
}

get features() {
return this.#features.slice();
}

get label() {
return this.#label;
}

get icon() {
return this.#icon;
}

constructor({ address, publicKey, label, icon }: Omit<WalletAccount, 'chains' | 'features'>) {
if (new.target === TrustWalletAccount) {
Object.freeze(this);
}

this.#address = address;
this.#publicKey = publicKey;
this.#chains = chains;
this.#features = features;
this.#label = label;
this.#icon = icon;
}
}
4 changes: 4 additions & 0 deletions adapter/src/icon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { WalletIcon } from '@wallet-standard/base';

export const icon: WalletIcon =
'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNTgiIGhlaWdodD0iNjUiIHZpZXdC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number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './initialize';
9 changes: 9 additions & 0 deletions adapter/src/initialize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { registerWallet } from './register';
import { TrustWallet } from './wallet';
import type { Trust } from './window';

function initialize(trust: Trust): void {
registerWallet(new TrustWallet(trust));
}

export default initialize;
71 changes: 71 additions & 0 deletions adapter/src/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// This is copied from @wallet-standard/wallet

import type {
DEPRECATED_WalletsWindow,
Wallet,
WalletEventsWindow,
WindowRegisterWalletEvent,
WindowRegisterWalletEventCallback,
} from '@wallet-standard/base';

export function registerWallet(wallet: Wallet): void {
const callback: WindowRegisterWalletEventCallback = ({ register }) => register(wallet);
try {
(window as WalletEventsWindow).dispatchEvent(new RegisterWalletEvent(callback));
} catch (error) {
console.error('wallet-standard:register-wallet event could not be dispatched\n', error);
}
try {
(window as WalletEventsWindow).addEventListener('wallet-standard:app-ready', ({ detail: api }) =>
callback(api)
);
} catch (error) {
console.error('wallet-standard:app-ready event listener could not be added\n', error);
}
}

class RegisterWalletEvent extends Event implements WindowRegisterWalletEvent {
readonly #detail: WindowRegisterWalletEventCallback;

get detail() {
return this.#detail;
}

get type() {
return 'wallet-standard:register-wallet' as const;
}

constructor(callback: WindowRegisterWalletEventCallback) {
super('wallet-standard:register-wallet', {
bubbles: false,
cancelable: false,
composed: false,
});
this.#detail = callback;
}

/** @deprecated */
preventDefault(): never {
throw new Error('preventDefault cannot be called');
}

/** @deprecated */
stopImmediatePropagation(): never {
throw new Error('stopImmediatePropagation cannot be called');
}

/** @deprecated */
stopPropagation(): never {
throw new Error('stopPropagation cannot be called');
}
}

/** @deprecated */
export function DEPRECATED_registerWallet(wallet: Wallet): void {
registerWallet(wallet);
try {
((window as DEPRECATED_WalletsWindow).navigator.wallets ||= []).push(({ register }) => register(wallet));
} catch (error) {
console.error('window.navigator.wallets could not be pushed\n', error);
}
}
35 changes: 35 additions & 0 deletions adapter/src/solana.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// This is copied from @solana/wallet-standard-chains

import type { IdentifierString } from '@wallet-standard/base';
import type { Transaction, VersionedTransaction } from '@solana/web3.js';

/** Solana Mainnet (beta) cluster, e.g. https://api.mainnet-beta.solana.com */
export const SOLANA_MAINNET_CHAIN = 'solana:mainnet';

/** Solana Devnet cluster, e.g. https://api.devnet.solana.com */
export const SOLANA_DEVNET_CHAIN = 'solana:devnet';

/** Solana Testnet cluster, e.g. https://api.testnet.solana.com */
export const SOLANA_TESTNET_CHAIN = 'solana:testnet';

/** Solana Localnet cluster, e.g. http://localhost:8899 */
export const SOLANA_LOCALNET_CHAIN = 'solana:localnet';

/** Array of all Solana clusters */
export const SOLANA_CHAINS = [SOLANA_MAINNET_CHAIN] as const;

/** Type of all Solana clusters */
export type SolanaChain = (typeof SOLANA_CHAINS)[number];

/**
* Check if a chain corresponds with one of the Solana clusters.
*/
export function isSolanaChain(chain: IdentifierString): chain is SolanaChain {
return SOLANA_CHAINS.includes(chain as SolanaChain);
}

export function isVersionedTransaction(
transaction: Transaction | VersionedTransaction
): transaction is VersionedTransaction {
return 'version' in transaction;
}
23 changes: 23 additions & 0 deletions adapter/src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// This is copied from @wallet-standard/wallet

export function bytesEqual(a: Uint8Array, b: Uint8Array): boolean {
return arraysEqual(a, b);
}

interface Indexed<T> {
length: number;
[index: number]: T;
}

export function arraysEqual<T>(a: Indexed<T>, b: Indexed<T>): boolean {
if (a === b) return true;

const length = a.length;
if (length !== b.length) return false;

for (let i = 0; i < length; i++) {
if (a[i] !== b[i]) return false;
}

return true;
}
Loading

0 comments on commit 29ee6af

Please sign in to comment.