Skip to content

Commit

Permalink
Connect documentation (#378)
Browse files Browse the repository at this point in the history
Documentation for gala connect, some renaming to fit established standards
  • Loading branch information
IndiaJonathan authored Oct 7, 2024
1 parent f19e0db commit 332399f
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 79 deletions.
80 changes: 7 additions & 73 deletions chain-connect/docs/Examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,13 @@ First, you need to create a client and connect to a web3 wallet, the below shows

```typescript
import { BrowserConnectClient } from "@gala-chain/connect";
import { ref } from "vue";

const web3Wallet = new BrowserConnectClient();
export const message = ref("");
export const connectedUser = ref<string | null>(null);

export async function connectToWeb3Wallet() {
try {
const connectionResult = await web3Wallet.connect();
message.value = `Connected! User: ${connectionResult}`;
connectedUser.value = connectionResult;
console.log(`User connected with wallet: ${connectionResult}`)

// Listening to account changes
web3Wallet.on("accountChanged", (account: string[] | string | null) => {
Expand All @@ -49,8 +45,6 @@ export async function connectToWeb3Wallet() {
}
} else {
console.log(`Account changed: ${account}`);
message.value = `Account Changed! User: ${account}`;
connectedUser.value = account;
}
});
} catch (error) {
Expand Down Expand Up @@ -100,38 +94,6 @@ export async function createTokenClass() {
}
```

### Template

In your Vue component, you can add a simple template with a connect button and a button to create a token class:

```html
<template>
<div>
<button @click="connectToWeb3Wallet">Connect to Web3 Wallet</button>
<button @click="createTokenClass" :disabled="!isConnected">Create Token Class</button>
<p v-if="connectedUser">
Connected Account(s):
<span> {{ connectedUser }}</span>
</p>
</div>
</template>

<script>
import { connectToWeb3Wallet, createTokenClass, connectedUser, isConnected } from './your-code-from-above';
export default {
setup() {
return {
connectToWeb3Wallet,
createTokenClass,
connectedUser,
isConnected,
};
},
};
</script>
```


### Generating Wallets

Expand Down Expand Up @@ -195,63 +157,35 @@ The `createAndRegisterRandomWallet()` function returns an object containing:

```typescript
import { WalletUtils } from "@gala-chain/connect";
import { ref } from "vue";

export const message = ref("");
export const createdUser = ref<string | null>(null);

export async function generateWallet() {
try {
const wallet = await WalletUtils.createAndRegisterRandomWallet(
"https://stage-galaswap.gala.com/v1/CreateHeadlessWallet"
);
message.value = `Wallet generated and registered. Address: ${wallet.ethAddress}`;
console.log(`Wallet generated and registered. Address: ${wallet.ethAddress}`);
createdUser.value = wallet.ethAddress;

//This is unsafe, please don't use in production
console.log("Private Key:", wallet.privateKey);
} catch (error) {
message.value = "Failed to generate wallet!";
console.log("Failed to generate wallet!");
console.error(error);
}
}
```

### Template Integration

In your Vue component, you can add a button to generate a wallet:

```html
<template>
<div>
<button @click="generateWallet">Generate Wallet</button>
</div>
</template>

<script>
import { generateWallet } from "./your-code-from-above";
export default {
setup() {
return {
generateWallet,
};
}
};
</script>
```

## Server-Side Signing

GalaChain Connect supports server-side signing using a private key. This is useful for backend services that need to interact with GalaChain without user interaction.

### Server-Side Example

```typescript
import { PublicKeyApi, ServerSigningClient } from "@gala-chain/connect";
import { PublicKeyApi, SigningClient } from "@gala-chain/connect";

const privateKey = "your-private-key-here";
const connection = new ServerSigningClient(privateKey);
const connection = new SigningClient(privateKey);

// The below URI is an example; you can use any URI that supports this
const uri =
Expand All @@ -278,10 +212,10 @@ getProfile();
You can also create a token class on the server side:

```typescript
import { ServerSigningClient, TokenApi } from "@gala-chain/connect";
import { SigningClient, TokenApi } from "@gala-chain/connect";

const privateKey = "your-private-key-here";
const connection = new ServerSigningClient(privateKey);
const connection = new SigningClient(privateKey);

const tokenClient = new TokenApi("https://your-galachain-api-url/asset/token-contract", connection);

Expand Down
4 changes: 2 additions & 2 deletions chain-connect/src/GalachainClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { ethers } from "ethers";
import { EventEmitter } from "events";

import { generateEIP712Types } from "./Utils";
import { BrowserConnectClient, GalachainConnectTrustClient } from "./customClients";
import { BrowserConnectClient, TrustWalletConnectClient } from "./customClients";

global.fetch = jest.fn((url: string, options?: Record<string, unknown>) =>
Promise.resolve({
Expand Down Expand Up @@ -255,7 +255,7 @@ describe("TrustConnectClient", () => {
};

// call connect
const client = new GalachainConnectTrustClient();
const client = new TrustWalletConnectClient();
await client.connect();

// send dto payload in send function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { CustomClient } from "../GalaChainClient";
import { generateEIP712Types } from "../Utils";
import { calculatePersonalSignPrefix } from "../helpers";

export class ServerSigningClient extends CustomClient {
export class SigningClient extends CustomClient {
get walletAddress(): string {
return this.wallet.address;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function getTrustWalletFromWindow() {
return window["trustwallet"] ?? null;
}

export class GalachainConnectTrustClient extends BrowserConnectClient {
export class TrustWalletConnectClient extends BrowserConnectClient {
constructor() {
super();
this.address = "";
Expand Down
4 changes: 2 additions & 2 deletions chain-connect/src/customClients/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
* limitations under the License.
*/
export * from "./BrowserConnectClient";
export * from "./TrustConnectClient";
export * from "./ServerSigningClient";
export * from "./TrustWalletConnectClient";
export * from "./SigningClient";
export * from "./PresignedClient";

0 comments on commit 332399f

Please sign in to comment.