Skip to content

Commit 4fb652e

Browse files
thdailongTrương Long
and
Trương Long
authored
Wallet construct with parameter (#676)
* Add walletURL to the the WalletSession * Update type for walletURL and changelog.md * Add type HttpNetworkConfig to types * Solve conflict changelog.md * Update changelog.md and fix export function algoexploer * Update changelog.md * push develop to this branch Co-authored-by: Trương Long <[email protected]>
1 parent e33e248 commit 4fb652e

File tree

7 files changed

+59
-31
lines changed

7 files changed

+59
-31
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ console.log("txn1 information: ", receipts[2]);
119119
- Allow token to be empty.
120120
- Throw error when issue inner transactions in clear program. Fixed in [#667](https://github.com/scale-it/algo-builder/pull/667).
121121
- Parameters in `extract*` opcodes can greater than uint8. Fixed in [#666](https://github.com/scale-it/algo-builder/pull/666).
122-
- Restirct duplicate transaction in group transaction.
122+
- Wallet contructor come from a parameter walletURL(token, server, port)
123+
- Restirct duplicate transaction in group transaction.
123124

124125
### Infrastructure
125126

docs/tutorials/t-08.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ We use `connectAlgoSigner()` method to connect the AlgoSigner wallet. Next, we c
7272
```js
7373
async connectMyAlgoWallet() {
7474
try {
75-
let myAlgo = new MyAlgoWalletSession(ChainType.MainNet);
75+
let walletURL = {
76+
token: "",
77+
server: "https://node.algoexplorerapi.io",
78+
port: "",
79+
}
80+
let myAlgo = new MyAlgoWalletSession(walletURL);
7681
await myAlgo.connectToMyAlgo();
7782
if (myAlgo.accounts.length) {
7883
this.walletAddress = myAlgo.accounts[0].address;
@@ -88,7 +93,12 @@ We use `connectAlgoSigner()` method to connect the AlgoSigner wallet. Next, we c
8893
```js
8994
async connectWalletConnect() {
9095
try {
91-
let walletConnector = new WallectConnectSession(ChainType.MainNet);
96+
let walletURL = {
97+
token: "",
98+
server: "https://node.algoexplorerapi.io",
99+
port: "",
100+
}
101+
let walletConnector = new WallectConnectSession(walletURL);
92102
await walletConnector.create(true);
93103
walletConnector.onConnect((error, response) => {
94104
if (response.accounts.length) {

packages/web/README.md

+21-7
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,33 @@ You can connect to `web` package in your react app by using different wallets. C
3434
```
3535
2. ### MyAlgo Wallet:
3636

37-
Create an instance of the `MyAlgoWalletSession` class by passing the chain name and connect it using `connectToMyAlgo`.
37+
Create an instance of the `MyAlgoWalletSession` class by passing the walletURL(token, server, port) and connect it using `connectToMyAlgo`.
3838

39-
```js
40-
const wcSession = new MyAlgoWalletSession(CHAIN_NAME)
39+
40+
```js
41+
const walletURL = {
42+
token: token,
43+
host: host,
44+
port: port,
45+
}
46+
const wcSession = new MyAlgoWalletSession(walletURL)
4147
await wcSession.connectToMyAlgo();
42-
```
48+
```
49+
4350

4451
3. ### Wallet Connect:
4552

46-
Create an instance of the `WallectConnectSession` class by passing the chain name and create a new session using `create` and connect to it using `onConnect`.
53+
Create an instance of the `WallectConnectSession` class by passing the walletURL(token, server, port) and create a new session using `create` and connect to it using `onConnect`.
54+
55+
56+
```js
57+
const walletURL = {
58+
token: token,
59+
host: host,
60+
port: port,
61+
}
62+
const wcSession = new WallectConnectSession(walletURL);
4763
48-
```js
49-
const wcSession = new WallectConnectSession(CHAIN_NAME);
5064
await wcSession.create(true);
5165
wcSession.onConnect((error, response) => console.log(error, response));
5266
```

packages/web/src/lib/api.ts

+3-16
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
11
import algosdk, { Algodv2, ALGORAND_MIN_TX_FEE, SuggestedParams } from "algosdk";
22

3-
import { ChainType, TxParams } from "../types";
4-
import { betanetURL, mainnetURL, testnetURL } from "./constants";
3+
import { HttpNetworkConfig, TxParams } from "../types";
54

6-
export function algoexplorerAlgod(chain: string): algosdk.Algodv2 {
7-
const mainnetAlgoExplorer = new Algodv2("", mainnetURL, "");
8-
const testnetAlgoExplorer = new Algodv2("", testnetURL, "");
9-
const betanetAlgoExplorer = new Algodv2("", betanetURL, "");
10-
switch (chain) {
11-
case ChainType.MainNet:
12-
return mainnetAlgoExplorer;
13-
case ChainType.TestNet:
14-
return testnetAlgoExplorer;
15-
case ChainType.BetaNet:
16-
return betanetAlgoExplorer;
17-
default:
18-
throw new Error(`Unknown chain type: ${chain}`);
19-
}
5+
export function algoexplorerAlgod(walletURL: HttpNetworkConfig): algosdk.Algodv2 {
6+
return new Algodv2(walletURL.token, walletURL.server, walletURL.port);
207
}
218

229
/**

packages/web/src/lib/myalgowallet-mode.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
import algosdk, { Transaction } from "algosdk";
1111

1212
import { mkTxParams } from "..";
13-
import { ExecParams, TransactionInGroup } from "../types";
13+
import { ExecParams, HttpNetworkConfig, TransactionInGroup } from "../types";
1414
import { algoexplorerAlgod } from "./api";
1515
import { WAIT_ROUNDS } from "./constants";
1616
import { error, log } from "./logger";
@@ -57,8 +57,8 @@ export class MyAlgoWalletSession {
5757
accounts: Accounts[] = [];
5858
addresses: Address[] = [];
5959

60-
constructor(chain: string, connector?: MyAlgoConnect) {
61-
this.algodClient = algoexplorerAlgod(chain);
60+
constructor(walletURL: HttpNetworkConfig, connector?: MyAlgoConnect) {
61+
this.algodClient = algoexplorerAlgod(walletURL);
6262
try {
6363
const MyAlgoConnect = require("@randlabs/myalgo-connect"); // eslint-disable-line @typescript-eslint/no-var-requires
6464
if (connector) {

packages/web/src/lib/wallectconnect-mode.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import algosdk, { Transaction } from "algosdk";
66
import { WalletTransaction } from "../algo-signer-types";
77
import {
88
ExecParams,
9+
HttpNetworkConfig,
910
isSDKTransactionAndSign,
1011
SessionConnectResponse,
1112
SessionDisconnectResponse,
@@ -24,8 +25,8 @@ export class WallectConnectSession {
2425
private readonly algodClient: algosdk.Algodv2;
2526
wcAccounts: string[];
2627

27-
constructor(chain: string, connector?: WalletConnect) {
28-
this.algodClient = algoexplorerAlgod(chain);
28+
constructor(walletURL: HttpNetworkConfig, connector?: WalletConnect) {
29+
this.algodClient = algoexplorerAlgod(walletURL);
2930
if (connector) {
3031
this.connector = connector;
3132
} else {

packages/web/src/types.ts

+15
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,18 @@ export interface TransactionInGroup {
370370
msig?: WalletMultisigMetadata;
371371
message?: string;
372372
}
373+
374+
export interface AlgodTokenHeader {
375+
"X-Algo-API-Token": string;
376+
}
377+
378+
export interface CustomTokenHeader {
379+
[headerName: string]: string;
380+
}
381+
382+
export interface HttpNetworkConfig {
383+
server: string; // with optional http o https prefix
384+
port: string | number;
385+
token: string | AlgodTokenHeader | CustomTokenHeader;
386+
httpHeaders?: { [name: string]: string };
387+
}

0 commit comments

Comments
 (0)