@txnlab/use-wallet 0.1.21 → 0.1.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.d.ts +2 -0
- package/dist/cjs/index.js +1281 -41
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/providers/index.d.ts +7 -7
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +1258 -15
- package/dist/esm/providers/index.d.ts +7 -7
- package/dist/index.d.ts +63 -1
- package/package.json +8 -8
- package/dist/cjs/base-9794b627.js +0 -174
- package/dist/cjs/base-9794b627.js.map +0 -1
- package/dist/cjs/clients/algosigner.js +0 -144
- package/dist/cjs/clients/algosigner.js.map +0 -1
- package/dist/cjs/clients/defly.js +0 -159
- package/dist/cjs/clients/defly.js.map +0 -1
- package/dist/cjs/clients/exodus.js +0 -118
- package/dist/cjs/clients/exodus.js.map +0 -1
- package/dist/cjs/clients/kmd.js +0 -140
- package/dist/cjs/clients/kmd.js.map +0 -1
- package/dist/cjs/clients/myalgowallet.js +0 -128
- package/dist/cjs/clients/myalgowallet.js.map +0 -1
- package/dist/cjs/clients/perawallet.js +0 -169
- package/dist/cjs/clients/perawallet.js.map +0 -1
- package/dist/cjs/clients/walletconnect.js +0 -199
- package/dist/cjs/clients/walletconnect.js.map +0 -1
- package/dist/cjs/constants-e76d1f05.js +0 -63
- package/dist/cjs/constants-e76d1f05.js.map +0 -1
- package/dist/cjs/constants.js +0 -22
- package/dist/cjs/constants.js.map +0 -1
- package/dist/cjs/providers.js +0 -81
- package/dist/cjs/providers.js.map +0 -1
- package/dist/cjs/utils.js +0 -74
- package/dist/cjs/utils.js.map +0 -1
- package/dist/esm/base-71089716.js +0 -152
- package/dist/esm/base-71089716.js.map +0 -1
- package/dist/esm/clients/algosigner.js +0 -142
- package/dist/esm/clients/algosigner.js.map +0 -1
- package/dist/esm/clients/defly.js +0 -139
- package/dist/esm/clients/defly.js.map +0 -1
- package/dist/esm/clients/exodus.js +0 -116
- package/dist/esm/clients/exodus.js.map +0 -1
- package/dist/esm/clients/kmd.js +0 -138
- package/dist/esm/clients/kmd.js.map +0 -1
- package/dist/esm/clients/myalgowallet.js +0 -108
- package/dist/esm/clients/myalgowallet.js.map +0 -1
- package/dist/esm/clients/perawallet.js +0 -149
- package/dist/esm/clients/perawallet.js.map +0 -1
- package/dist/esm/clients/walletconnect.js +0 -179
- package/dist/esm/clients/walletconnect.js.map +0 -1
- package/dist/esm/constants-49b44514.js +0 -53
- package/dist/esm/constants-49b44514.js.map +0 -1
- package/dist/esm/constants.js +0 -2
- package/dist/esm/constants.js.map +0 -1
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/providers.js +0 -77
- package/dist/esm/providers.js.map +0 -1
- package/dist/esm/utils.js +0 -70
- package/dist/esm/utils.js.map +0 -1
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var base = require('../base-9794b627.js');
|
|
4
|
-
var constants = require('../constants-e76d1f05.js');
|
|
5
|
-
var providers = require('../providers.js');
|
|
6
|
-
|
|
7
|
-
class ExodusClient extends base.BaseWallet {
|
|
8
|
-
#client;
|
|
9
|
-
id;
|
|
10
|
-
provider;
|
|
11
|
-
constructor({ client, id, provider, algosdk, algodClient }) {
|
|
12
|
-
super(algosdk, algodClient);
|
|
13
|
-
this.#client = client;
|
|
14
|
-
this.id = id;
|
|
15
|
-
this.provider = provider;
|
|
16
|
-
}
|
|
17
|
-
static async init() {
|
|
18
|
-
if (typeof window == "undefined" ||
|
|
19
|
-
window.exodus === undefined) {
|
|
20
|
-
throw new Error("Exodus is not available.");
|
|
21
|
-
}
|
|
22
|
-
const { algosdk, algodClient } = await base.Algod.init();
|
|
23
|
-
const exodus = window.exodus.algorand;
|
|
24
|
-
return new ExodusClient({
|
|
25
|
-
id: constants.PROVIDER_ID.EXODUS,
|
|
26
|
-
client: exodus,
|
|
27
|
-
provider: providers.providers[constants.PROVIDER_ID.EXODUS],
|
|
28
|
-
algosdk: algosdk,
|
|
29
|
-
algodClient: algodClient,
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
async connect() {
|
|
33
|
-
const { address } = await this.#client.connect();
|
|
34
|
-
if (!address) {
|
|
35
|
-
throw new Error(`No accounts found for ${this.provider}`);
|
|
36
|
-
}
|
|
37
|
-
const accounts = [
|
|
38
|
-
{
|
|
39
|
-
name: `Exodus 1`,
|
|
40
|
-
address,
|
|
41
|
-
providerId: this.provider.id,
|
|
42
|
-
},
|
|
43
|
-
];
|
|
44
|
-
return {
|
|
45
|
-
...this.provider,
|
|
46
|
-
accounts,
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
async reconnect(onDisconnect) {
|
|
50
|
-
if (window === undefined ||
|
|
51
|
-
window.exodus === undefined ||
|
|
52
|
-
window.exodus.algorand.isConnected !== true) {
|
|
53
|
-
onDisconnect();
|
|
54
|
-
}
|
|
55
|
-
return null;
|
|
56
|
-
}
|
|
57
|
-
async disconnect() {
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
async signTransactions(activeAdress, transactions) {
|
|
61
|
-
// Decode the transactions to access their properties.
|
|
62
|
-
const decodedTxns = transactions.map((txn) => {
|
|
63
|
-
return this.algosdk.decodeObj(txn);
|
|
64
|
-
});
|
|
65
|
-
// Get the unsigned transactions.
|
|
66
|
-
const txnsToSign = decodedTxns.reduce((acc, txn, i) => {
|
|
67
|
-
// If the transaction isn't already signed and is to be sent from a connected account,
|
|
68
|
-
// add it to the arrays of transactions to be signed.
|
|
69
|
-
if (!("txn" in txn) &&
|
|
70
|
-
this.algosdk.encodeAddress(txn["snd"]) === activeAdress) {
|
|
71
|
-
acc.push(transactions[i]);
|
|
72
|
-
}
|
|
73
|
-
return acc;
|
|
74
|
-
}, []);
|
|
75
|
-
// Sign them with the client.
|
|
76
|
-
const result = await this.#client.signTransaction(txnsToSign);
|
|
77
|
-
// Join the newly signed transactions with the original group of transactions.
|
|
78
|
-
const signedTxns = decodedTxns.reduce((acc, txn, i) => {
|
|
79
|
-
if (!("txn" in txn)) {
|
|
80
|
-
const signedByUser = result.shift();
|
|
81
|
-
signedByUser && acc.push(signedByUser);
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
acc.push(transactions[i]);
|
|
85
|
-
}
|
|
86
|
-
return acc;
|
|
87
|
-
}, []);
|
|
88
|
-
return signedTxns;
|
|
89
|
-
}
|
|
90
|
-
async signEncodedTransactions(transactions) {
|
|
91
|
-
const transactionsToSign = [];
|
|
92
|
-
const signedRawTransactions = [];
|
|
93
|
-
for (const [type, txn] of transactions) {
|
|
94
|
-
if (type === "u") {
|
|
95
|
-
const decoded = this.algosdk.decodeUnsignedTransaction(Buffer.from(txn, "base64"));
|
|
96
|
-
transactionsToSign.push(decoded.toByte());
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
const result = await this.#client.signTransaction(transactionsToSign);
|
|
100
|
-
if (!result) {
|
|
101
|
-
throw new Error("Signing failed.");
|
|
102
|
-
}
|
|
103
|
-
let resultIndex = 0;
|
|
104
|
-
for (const [type, txn] of transactions) {
|
|
105
|
-
if (type === "u") {
|
|
106
|
-
signedRawTransactions.push(result[resultIndex]);
|
|
107
|
-
resultIndex++;
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
signedRawTransactions.push(new Uint8Array(Buffer.from(txn, "base64")));
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
return signedRawTransactions;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
module.exports = ExodusClient;
|
|
118
|
-
//# sourceMappingURL=exodus.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"exodus.js","sources":["../../../src/clients/exodus.ts"],"sourcesContent":["/**\n * Helpful resources:\n * https://docs.exodus.com/api-reference/algorand-provider-api/\n */\nimport type _algosdk from \"algosdk\";\nimport BaseWallet from \"./base\";\nimport Algod from \"../algod\";\nimport { PROVIDER_ID } from \"../constants\";\nimport { providers } from \"../providers\";\nimport type { WalletProvider } from \"../types\";\nimport { TransactionsArray } from \"../types\";\nimport type { DecodedTransaction, DecodedSignedTransaction } from \"../types\";\n\ntype WindowExtended = { exodus: { algorand: Exodus } } & Window &\n typeof globalThis;\n\ntype Bytes = Readonly<Uint8Array>;\n\ntype Exodus = {\n isConnected: boolean;\n address: string | null;\n connect: () => Promise<{\n address: string;\n }>;\n disconnect: () => void;\n signAndSendTransaction(transactions: Bytes[]): Promise<{\n txId: string;\n }>;\n signTransaction(transactions: Bytes[]): Promise<Bytes[]>;\n};\n\ntype InitWallet = {\n client: Exodus;\n id: PROVIDER_ID;\n provider: WalletProvider;\n algosdk: typeof _algosdk;\n algodClient: _algosdk.Algodv2;\n};\n\nclass ExodusClient extends BaseWallet {\n #client: Exodus;\n id: PROVIDER_ID;\n provider: WalletProvider;\n\n constructor({ client, id, provider, algosdk, algodClient }: InitWallet) {\n super(algosdk, algodClient);\n\n this.#client = client;\n this.id = id;\n this.provider = provider;\n }\n\n static async init() {\n if (\n typeof window == \"undefined\" ||\n (window as WindowExtended).exodus === undefined\n ) {\n throw new Error(\"Exodus is not available.\");\n }\n\n const { algosdk, algodClient } = await Algod.init();\n const exodus = (window as WindowExtended).exodus.algorand as Exodus;\n\n return new ExodusClient({\n id: PROVIDER_ID.EXODUS,\n client: exodus,\n provider: providers[PROVIDER_ID.EXODUS],\n algosdk: algosdk,\n algodClient: algodClient,\n });\n }\n\n async connect() {\n const { address } = await this.#client.connect();\n\n if (!address) {\n throw new Error(`No accounts found for ${this.provider}`);\n }\n\n const accounts = [\n {\n name: `Exodus 1`,\n address,\n providerId: this.provider.id,\n },\n ];\n\n return {\n ...this.provider,\n accounts,\n };\n }\n\n async reconnect(onDisconnect: () => void) {\n if (\n window === undefined ||\n (window as WindowExtended).exodus === undefined ||\n (window as WindowExtended).exodus.algorand.isConnected !== true\n ) {\n onDisconnect();\n }\n\n return null;\n }\n\n async disconnect() {\n return;\n }\n\n async signTransactions(\n activeAdress: string,\n transactions: Array<Uint8Array>\n ) {\n // Decode the transactions to access their properties.\n const decodedTxns = transactions.map((txn) => {\n return this.algosdk.decodeObj(txn);\n }) as Array<DecodedTransaction | DecodedSignedTransaction>;\n\n // Get the unsigned transactions.\n const txnsToSign = decodedTxns.reduce<Uint8Array[]>((acc, txn, i) => {\n // If the transaction isn't already signed and is to be sent from a connected account,\n // add it to the arrays of transactions to be signed.\n if (\n !(\"txn\" in txn) &&\n this.algosdk.encodeAddress(txn[\"snd\"]) === activeAdress\n ) {\n acc.push(transactions[i]);\n }\n\n return acc;\n }, []);\n\n // Sign them with the client.\n const result = await this.#client.signTransaction(txnsToSign);\n\n // Join the newly signed transactions with the original group of transactions.\n const signedTxns = decodedTxns.reduce<Uint8Array[]>((acc, txn, i) => {\n if (!(\"txn\" in txn)) {\n const signedByUser = result.shift();\n signedByUser && acc.push(signedByUser);\n } else {\n acc.push(transactions[i]);\n }\n\n return acc;\n }, []);\n\n return signedTxns;\n }\n\n async signEncodedTransactions(transactions: TransactionsArray) {\n const transactionsToSign: Uint8Array[] = [];\n const signedRawTransactions: Uint8Array[] = [];\n\n for (const [type, txn] of transactions) {\n if (type === \"u\") {\n const decoded = this.algosdk.decodeUnsignedTransaction(\n Buffer.from(txn, \"base64\")\n );\n transactionsToSign.push(decoded.toByte());\n }\n }\n\n const result = await this.#client.signTransaction(transactionsToSign);\n\n if (!result) {\n throw new Error(\"Signing failed.\");\n }\n\n let resultIndex = 0;\n\n for (const [type, txn] of transactions) {\n if (type === \"u\") {\n signedRawTransactions.push(result[resultIndex]);\n resultIndex++;\n } else {\n signedRawTransactions.push(new Uint8Array(Buffer.from(txn, \"base64\")));\n }\n }\n\n return signedRawTransactions;\n }\n}\n\nexport default ExodusClient;\n"],"names":["BaseWallet","Algod","PROVIDER_ID","providers"],"mappings":";;;;;;AAuCA,MAAM,YAAa,SAAQA,eAAU,CAAA;AACnC,IAAA,OAAO,CAAS;AAChB,IAAA,E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,WAAW,CAAC,CAAC,CAAC;AAChD,gBAAA,WAAW,EAAE,CAAC;AACf,aAAA;AAAM,iBAAA;AACL,gBAAA,qBAAqB,CAAC,IAAI,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;AACxE,aAAA;AACF,SAAA;AAED,QAAA,OAAO,qBAAqB,CAAC;KAC9B;AACF;;;;"}
|
package/dist/cjs/clients/kmd.js
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var base = require('../base-9794b627.js');
|
|
4
|
-
var constants = require('../constants-e76d1f05.js');
|
|
5
|
-
var providers = require('../providers.js');
|
|
6
|
-
|
|
7
|
-
const DefaultKMDConfig = {
|
|
8
|
-
host: constants.KMD_HOST,
|
|
9
|
-
token: constants.KMD_TOKEN,
|
|
10
|
-
port: constants.KMD_PORT,
|
|
11
|
-
};
|
|
12
|
-
class KMDWallet extends base.BaseWallet {
|
|
13
|
-
#client;
|
|
14
|
-
walletId;
|
|
15
|
-
id;
|
|
16
|
-
provider;
|
|
17
|
-
constructor({ client, id, provider, algosdk, algodClient }) {
|
|
18
|
-
super(algosdk, algodClient);
|
|
19
|
-
this.#client = client;
|
|
20
|
-
this.id = id;
|
|
21
|
-
this.provider = provider;
|
|
22
|
-
this.walletId = "";
|
|
23
|
-
}
|
|
24
|
-
static async init() {
|
|
25
|
-
// TODO: allow diff config options?
|
|
26
|
-
const kmdConfig = DefaultKMDConfig;
|
|
27
|
-
const { algosdk, algodClient } = await base.Algod.init();
|
|
28
|
-
const kmdClient = new algosdk.Kmd(kmdConfig.token, kmdConfig.host, kmdConfig.port);
|
|
29
|
-
return new KMDWallet({
|
|
30
|
-
id: constants.PROVIDER_ID.KMD_WALLET,
|
|
31
|
-
client: kmdClient,
|
|
32
|
-
provider: providers.providers[constants.PROVIDER_ID.KMD_WALLET],
|
|
33
|
-
algosdk: algosdk,
|
|
34
|
-
algodClient: algodClient,
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
async connect() {
|
|
38
|
-
// TODO: prompt for wallet and password?
|
|
39
|
-
return {
|
|
40
|
-
...this.provider,
|
|
41
|
-
accounts: await this.listAccounts(constants.KMD_WALLET, await this.requestPassword()),
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
async disconnect() {
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
async reconnect() {
|
|
48
|
-
return {
|
|
49
|
-
...this.provider,
|
|
50
|
-
accounts: await this.listAccounts(constants.KMD_WALLET, await this.requestPassword()),
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
async requestPassword() {
|
|
54
|
-
// TODO: store it locally?
|
|
55
|
-
const pw = prompt("gib password");
|
|
56
|
-
return pw ? pw : "";
|
|
57
|
-
}
|
|
58
|
-
async getWalletToken(walletId, password) {
|
|
59
|
-
const handleResp = await this.#client.initWalletHandle(walletId, password);
|
|
60
|
-
return handleResp.wallet_handle_token;
|
|
61
|
-
}
|
|
62
|
-
async releaseToken(token) {
|
|
63
|
-
await this.#client.releaseWalletHandle(token);
|
|
64
|
-
}
|
|
65
|
-
async listWallets() {
|
|
66
|
-
const walletResponse = await this.#client.listWallets();
|
|
67
|
-
const walletList = walletResponse["wallets"];
|
|
68
|
-
const walletMap = {};
|
|
69
|
-
for (const w of walletList) {
|
|
70
|
-
walletMap[w.name] = w.id;
|
|
71
|
-
}
|
|
72
|
-
return walletMap;
|
|
73
|
-
}
|
|
74
|
-
async listAccounts(wallet, password) {
|
|
75
|
-
const walletMap = await this.listWallets();
|
|
76
|
-
if (!(wallet in walletMap))
|
|
77
|
-
throw Error("No wallet named: " + wallet);
|
|
78
|
-
this.walletId = walletMap[wallet];
|
|
79
|
-
// Get a handle token
|
|
80
|
-
const token = await this.getWalletToken(this.walletId, password);
|
|
81
|
-
// Fetch accounts and format them as lib expects
|
|
82
|
-
const listResponse = await this.#client.listKeys(token);
|
|
83
|
-
const addresses = listResponse["addresses"];
|
|
84
|
-
const mappedAccounts = addresses.map((address, index) => {
|
|
85
|
-
return {
|
|
86
|
-
name: `KMDWallet ${index + 1}`,
|
|
87
|
-
address,
|
|
88
|
-
providerId: this.provider.id,
|
|
89
|
-
};
|
|
90
|
-
});
|
|
91
|
-
// Release handle token
|
|
92
|
-
this.releaseToken(token);
|
|
93
|
-
return mappedAccounts;
|
|
94
|
-
}
|
|
95
|
-
async signTransactions(activeAddress, transactions) {
|
|
96
|
-
// Decode the transactions to access their properties.
|
|
97
|
-
const decodedTxns = transactions.map((txn) => {
|
|
98
|
-
return this.algosdk.decodeObj(txn);
|
|
99
|
-
});
|
|
100
|
-
// Get a handle token
|
|
101
|
-
const pw = await this.requestPassword();
|
|
102
|
-
const token = await this.getWalletToken(this.walletId, pw);
|
|
103
|
-
const signedTxns = [];
|
|
104
|
-
// Sign them with the client.
|
|
105
|
-
const signingPromises = [];
|
|
106
|
-
for (const idx in decodedTxns) {
|
|
107
|
-
const dtxn = decodedTxns[idx];
|
|
108
|
-
// push the incoming txn into signed, we'll overwrite it later
|
|
109
|
-
signedTxns.push(transactions[idx]);
|
|
110
|
-
// Its already signed, skip it
|
|
111
|
-
if (!("snd" in dtxn))
|
|
112
|
-
continue;
|
|
113
|
-
// Not to be signed by our signer, skip it
|
|
114
|
-
if (!(this.algosdk.encodeAddress(dtxn.snd) === activeAddress))
|
|
115
|
-
continue;
|
|
116
|
-
// overwrite with an empty blob
|
|
117
|
-
signedTxns[idx] = new Uint8Array();
|
|
118
|
-
const txn = this.algosdk.Transaction.from_obj_for_encoding(dtxn);
|
|
119
|
-
signingPromises.push(this.#client.signTransaction(token, pw, txn));
|
|
120
|
-
}
|
|
121
|
-
const signingResults = await Promise.all(signingPromises);
|
|
122
|
-
// Restore the newly signed txns in the correct order
|
|
123
|
-
let signedIdx = 0;
|
|
124
|
-
for (const idx in signedTxns) {
|
|
125
|
-
// If its an empty array, infer that it is one of the
|
|
126
|
-
// ones we wanted to have signed and overwrite the empty buff
|
|
127
|
-
if (signedTxns[idx].length === 0) {
|
|
128
|
-
signedTxns[idx] = signingResults[signedIdx];
|
|
129
|
-
signedIdx += 1;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
return signedTxns;
|
|
133
|
-
}
|
|
134
|
-
signEncodedTransactions(transactions) {
|
|
135
|
-
throw new Error("Method not implemented.");
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
module.exports = KMDWallet;
|
|
140
|
-
//# sourceMappingURL=kmd.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"kmd.js","sources":["../../../src/clients/kmd.ts"],"sourcesContent":["import type _algosdk from \"algosdk\";\nimport Algod from \"../algod\";\nimport BaseWallet from \"./base\";\nimport { PROVIDER_ID } from \"../constants\";\nimport { providers } from \"../providers\";\nimport type { Account, Wallet, WalletProvider } from \"../types\";\nimport { TransactionsArray } from \"../types\";\nimport {\n KMD_HOST,\n KMD_TOKEN,\n KMD_PORT,\n KMD_WALLET,\n KMD_PASSWORD as _KMD_PASSWORD,\n} from \"../constants\";\n\ntype KMDConfig = {\n host: string;\n port: string;\n token: string;\n};\n\nconst DefaultKMDConfig = {\n host: KMD_HOST,\n token: KMD_TOKEN,\n port: KMD_PORT,\n} as KMDConfig;\n\ninterface ListWalletResponse {\n id: string;\n name: string;\n driver_name?: string;\n driver_version?: number;\n mnemonic_ux?: boolean;\n supported_txs?: Array<any>;\n}\n\ninterface InitWalletHandle {\n wallet_handle_token: string;\n message?: string;\n error?: boolean;\n}\n\ntype InitWallet = {\n client: _algosdk.Kmd;\n id: PROVIDER_ID;\n provider: WalletProvider;\n algosdk: typeof _algosdk;\n algodClient: _algosdk.Algodv2;\n};\n\nclass KMDWallet extends BaseWallet {\n #client: _algosdk.Kmd;\n walletId: string;\n id: PROVIDER_ID;\n provider: WalletProvider;\n\n constructor({ client, id, provider, algosdk, algodClient }: InitWallet) {\n super(algosdk, algodClient);\n\n this.#client = client;\n this.id = id;\n this.provider = provider;\n this.walletId = \"\";\n }\n\n static async init() {\n // TODO: allow diff config options?\n const kmdConfig: KMDConfig = DefaultKMDConfig;\n\n const { algosdk, algodClient } = await Algod.init();\n const kmdClient = new algosdk.Kmd(\n kmdConfig.token,\n kmdConfig.host,\n kmdConfig.port\n );\n\n return new KMDWallet({\n id: PROVIDER_ID.KMD_WALLET,\n client: kmdClient,\n provider: providers[PROVIDER_ID.KMD_WALLET],\n algosdk: algosdk,\n algodClient: algodClient,\n });\n }\n\n async connect(): Promise<Wallet> {\n // TODO: prompt for wallet and password?\n return {\n ...this.provider,\n accounts: await this.listAccounts(\n KMD_WALLET,\n await this.requestPassword()\n ),\n };\n }\n\n async disconnect() {\n return;\n }\n\n async reconnect(): Promise<Wallet | null> {\n return {\n ...this.provider,\n accounts: await this.listAccounts(\n KMD_WALLET,\n await this.requestPassword()\n ),\n };\n }\n\n async requestPassword(): Promise<string> {\n // TODO: store it locally?\n const pw = prompt(\"gib password\");\n return pw ? pw : \"\";\n }\n\n async getWalletToken(walletId: string, password: string): Promise<string> {\n const handleResp: InitWalletHandle = await this.#client.initWalletHandle(\n walletId,\n password\n );\n return handleResp.wallet_handle_token;\n }\n\n async releaseToken(token: string): Promise<void> {\n await this.#client.releaseWalletHandle(token);\n }\n\n async listWallets(): Promise<Record<string, string>> {\n const walletResponse = await this.#client.listWallets();\n const walletList: Array<ListWalletResponse> = walletResponse[\"wallets\"];\n const walletMap: Record<string, string> = {};\n for (const w of walletList) {\n walletMap[w.name] = w.id;\n }\n return walletMap;\n }\n\n async listAccounts(\n wallet: string,\n password: string\n ): Promise<Array<Account>> {\n const walletMap = await this.listWallets();\n\n if (!(wallet in walletMap)) throw Error(\"No wallet named: \" + wallet);\n\n this.walletId = walletMap[wallet];\n\n // Get a handle token\n const token = await this.getWalletToken(this.walletId, password);\n\n // Fetch accounts and format them as lib expects\n const listResponse = await this.#client.listKeys(token);\n const addresses: Array<string> = listResponse[\"addresses\"];\n const mappedAccounts = addresses.map((address: string, index: number) => {\n return {\n name: `KMDWallet ${index + 1}`,\n address,\n providerId: this.provider.id,\n };\n });\n\n // Release handle token\n this.releaseToken(token);\n\n return mappedAccounts;\n }\n\n async signTransactions(activeAddress: string, transactions: Uint8Array[]) {\n // Decode the transactions to access their properties.\n const decodedTxns = transactions.map((txn) => {\n return this.algosdk.decodeObj(txn);\n }) as Array<\n _algosdk.EncodedTransaction | _algosdk.EncodedSignedTransaction\n >;\n\n // Get a handle token\n const pw = await this.requestPassword();\n const token = await this.getWalletToken(this.walletId, pw);\n\n const signedTxns: Uint8Array[] = [];\n // Sign them with the client.\n const signingPromises: Promise<Uint8Array>[] = [];\n for (const idx in decodedTxns) {\n const dtxn = decodedTxns[idx];\n\n // push the incoming txn into signed, we'll overwrite it later\n signedTxns.push(transactions[idx]);\n\n // Its already signed, skip it\n if (!(\"snd\" in dtxn)) continue;\n // Not to be signed by our signer, skip it\n if (!(this.algosdk.encodeAddress(dtxn.snd) === activeAddress)) continue;\n\n // overwrite with an empty blob\n signedTxns[idx] = new Uint8Array();\n\n const txn = this.algosdk.Transaction.from_obj_for_encoding(dtxn);\n signingPromises.push(this.#client.signTransaction(token, pw, txn));\n }\n\n const signingResults = await Promise.all(signingPromises);\n\n // Restore the newly signed txns in the correct order\n let signedIdx = 0;\n for (const idx in signedTxns) {\n // If its an empty array, infer that it is one of the\n // ones we wanted to have signed and overwrite the empty buff\n if (signedTxns[idx].length === 0) {\n signedTxns[idx] = signingResults[signedIdx];\n signedIdx += 1;\n }\n }\n\n return signedTxns;\n }\n\n signEncodedTransactions(\n transactions: TransactionsArray\n ): Promise<Uint8Array[]> {\n throw new Error(\"Method not implemented.\");\n }\n}\n\nexport default KMDWallet;\n"],"names":["KMD_HOST","KMD_TOKEN","KMD_PORT","BaseWallet","Algod","PROVIDER_ID","providers","KMD_WALLET"],"mappings":";;;;;;AAqBA,MAAM,gBAAgB,GAAG;AACvB,IAAA,IAAI,EAA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|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var base = require('../base-9794b627.js');
|
|
4
|
-
var constants = require('../constants-e76d1f05.js');
|
|
5
|
-
var providers = require('../providers.js');
|
|
6
|
-
|
|
7
|
-
function _interopNamespace(e) {
|
|
8
|
-
if (e && e.__esModule) return e;
|
|
9
|
-
var n = Object.create(null);
|
|
10
|
-
if (e) {
|
|
11
|
-
Object.keys(e).forEach(function (k) {
|
|
12
|
-
if (k !== 'default') {
|
|
13
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
14
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
15
|
-
enumerable: true,
|
|
16
|
-
get: function () { return e[k]; }
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
n["default"] = e;
|
|
22
|
-
return Object.freeze(n);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Helpful resources:
|
|
27
|
-
* https://github.com/randlabs/myalgo-connect
|
|
28
|
-
*/
|
|
29
|
-
class MyAlgoWalletClient extends base.BaseWallet {
|
|
30
|
-
#client;
|
|
31
|
-
id;
|
|
32
|
-
provider;
|
|
33
|
-
constructor({ client, id, provider, algosdk, algodClient }) {
|
|
34
|
-
super(algosdk, algodClient);
|
|
35
|
-
this.#client = client;
|
|
36
|
-
this.id = id;
|
|
37
|
-
this.provider = provider;
|
|
38
|
-
}
|
|
39
|
-
static async init() {
|
|
40
|
-
const { algosdk, algodClient } = await base.Algod.init();
|
|
41
|
-
const MyAlgoConnect = (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('@randlabs/myalgo-connect')); })).default;
|
|
42
|
-
const myAlgo = new MyAlgoConnect({ disableLedgerNano: false });
|
|
43
|
-
return new MyAlgoWalletClient({
|
|
44
|
-
id: constants.PROVIDER_ID.MYALGO_WALLET,
|
|
45
|
-
client: myAlgo,
|
|
46
|
-
provider: providers.providers[constants.PROVIDER_ID.MYALGO_WALLET],
|
|
47
|
-
algosdk: algosdk,
|
|
48
|
-
algodClient: algodClient,
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
async connect() {
|
|
52
|
-
const accounts = await this.#client.connect();
|
|
53
|
-
if (accounts.length === 0) {
|
|
54
|
-
throw new Error(`No accounts found for ${this.provider}`);
|
|
55
|
-
}
|
|
56
|
-
const mappedAccounts = accounts.map((account) => ({
|
|
57
|
-
...account,
|
|
58
|
-
providerId: this.provider.id,
|
|
59
|
-
}));
|
|
60
|
-
return {
|
|
61
|
-
...this.provider,
|
|
62
|
-
accounts: mappedAccounts,
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
async reconnect() {
|
|
66
|
-
return null;
|
|
67
|
-
}
|
|
68
|
-
async disconnect() {
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
async signTransactions(activeAdress, transactions) {
|
|
72
|
-
// Decode the transactions to access their properties.
|
|
73
|
-
const decodedTxns = transactions.map((txn) => {
|
|
74
|
-
return this.algosdk.decodeObj(txn);
|
|
75
|
-
});
|
|
76
|
-
// Get the unsigned transactions.
|
|
77
|
-
const txnsToSign = decodedTxns.reduce((acc, txn, i) => {
|
|
78
|
-
// If the transaction isn't already signed and is to be sent from a connected account,
|
|
79
|
-
// add it to the arrays of transactions to be signed.
|
|
80
|
-
if (!("txn" in txn) &&
|
|
81
|
-
this.algosdk.encodeAddress(txn["snd"]) === activeAdress) {
|
|
82
|
-
acc.push(transactions[i]);
|
|
83
|
-
}
|
|
84
|
-
return acc;
|
|
85
|
-
}, []);
|
|
86
|
-
// Sign them with the client.
|
|
87
|
-
const result = await this.#client.signTransaction(txnsToSign);
|
|
88
|
-
// Join the newly signed transactions with the original group of transactions.
|
|
89
|
-
const signedTxns = decodedTxns.reduce((acc, txn, i) => {
|
|
90
|
-
if (!("txn" in txn)) {
|
|
91
|
-
const signedByUser = result.shift()?.blob;
|
|
92
|
-
signedByUser && acc.push(signedByUser);
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
acc.push(transactions[i]);
|
|
96
|
-
}
|
|
97
|
-
return acc;
|
|
98
|
-
}, []);
|
|
99
|
-
return signedTxns;
|
|
100
|
-
}
|
|
101
|
-
async signEncodedTransactions(transactions) {
|
|
102
|
-
const transactionsToSign = [];
|
|
103
|
-
const signedRawTransactions = [];
|
|
104
|
-
for (const [type, txn] of transactions) {
|
|
105
|
-
if (type === "u") {
|
|
106
|
-
transactionsToSign.push(txn);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
const result = await this.#client.signTransaction(transactionsToSign);
|
|
110
|
-
if (!result) {
|
|
111
|
-
throw new Error("Signing failed.");
|
|
112
|
-
}
|
|
113
|
-
let resultIndex = 0;
|
|
114
|
-
for (const [type, txn] of transactions) {
|
|
115
|
-
if (type === "u") {
|
|
116
|
-
signedRawTransactions.push(result[resultIndex].blob);
|
|
117
|
-
resultIndex++;
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
signedRawTransactions.push(new Uint8Array(Buffer.from(txn, "base64")));
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
return signedRawTransactions;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
module.exports = MyAlgoWalletClient;
|
|
128
|
-
//# sourceMappingURL=myalgowallet.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"myalgowallet.js","sources":["../../../src/clients/myalgowallet.ts"],"sourcesContent":["/**\n * Helpful resources:\n * https://github.com/randlabs/myalgo-connect\n */\nimport BaseWallet from \"./base\";\nimport type MyAlgoConnect from \"@randlabs/myalgo-connect\";\nimport type _algosdk from \"algosdk\";\nimport Algod from \"../algod\";\nimport { PROVIDER_ID } from \"../constants\";\nimport { providers } from \"../providers\";\nimport type { WalletProvider } from \"../types\";\nimport { TransactionsArray } from \"../types\";\nimport { DecodedTransaction, DecodedSignedTransaction } from \"../types\";\n\ntype InitWallet = {\n id: PROVIDER_ID;\n client: MyAlgoConnect;\n provider: WalletProvider;\n algosdk: typeof _algosdk;\n algodClient: _algosdk.Algodv2;\n};\n\nclass MyAlgoWalletClient extends BaseWallet {\n #client: MyAlgoConnect;\n id: PROVIDER_ID;\n provider: WalletProvider;\n\n constructor({ client, id, provider, algosdk, algodClient }: InitWallet) {\n super(algosdk, algodClient);\n\n this.#client = client;\n this.id = id;\n this.provider = provider;\n }\n\n static async init() {\n const { algosdk, algodClient } = await Algod.init();\n const MyAlgoConnect = (await import(\"@randlabs/myalgo-connect\")).default;\n const myAlgo = new MyAlgoConnect({ disableLedgerNano: false });\n\n return new MyAlgoWalletClient({\n id: PROVIDER_ID.MYALGO_WALLET,\n client: myAlgo,\n provider: providers[PROVIDER_ID.MYALGO_WALLET],\n algosdk: algosdk,\n algodClient: algodClient,\n });\n }\n\n async connect() {\n const accounts = await this.#client.connect();\n\n if (accounts.length === 0) {\n throw new Error(`No accounts found for ${this.provider}`);\n }\n\n const mappedAccounts = accounts.map((account) => ({\n ...account,\n providerId: this.provider.id,\n }));\n\n return {\n ...this.provider,\n accounts: mappedAccounts,\n };\n }\n\n async reconnect() {\n return null;\n }\n\n async disconnect() {\n return;\n }\n\n async signTransactions(activeAdress: string, transactions: Uint8Array[]) {\n // Decode the transactions to access their properties.\n const decodedTxns = transactions.map((txn) => {\n return this.algosdk.decodeObj(txn);\n }) as Array<DecodedTransaction | DecodedSignedTransaction>;\n\n // Get the unsigned transactions.\n const txnsToSign = decodedTxns.reduce<Uint8Array[]>((acc, txn, i) => {\n // If the transaction isn't already signed and is to be sent from a connected account,\n // add it to the arrays of transactions to be signed.\n if (\n !(\"txn\" in txn) &&\n this.algosdk.encodeAddress(txn[\"snd\"]) === activeAdress\n ) {\n acc.push(transactions[i]);\n }\n\n return acc;\n }, []);\n\n // Sign them with the client.\n const result = await this.#client.signTransaction(txnsToSign);\n\n // Join the newly signed transactions with the original group of transactions.\n const signedTxns = decodedTxns.reduce<Uint8Array[]>((acc, txn, i) => {\n if (!(\"txn\" in txn)) {\n const signedByUser = result.shift()?.blob;\n signedByUser && acc.push(signedByUser);\n } else {\n acc.push(transactions[i]);\n }\n\n return acc;\n }, []);\n\n return signedTxns;\n }\n\n async signEncodedTransactions(transactions: TransactionsArray) {\n const transactionsToSign: string[] = [];\n const signedRawTransactions: Uint8Array[] = [];\n\n for (const [type, txn] of transactions) {\n if (type === \"u\") {\n transactionsToSign.push(txn);\n }\n }\n\n const result = await this.#client.signTransaction(transactionsToSign);\n\n if (!result) {\n throw new Error(\"Signing failed.\");\n }\n\n let resultIndex = 0;\n\n for (const [type, txn] of transactions) {\n if (type === \"u\") {\n signedRawTransactions.push(result[resultIndex].blob);\n resultIndex++;\n } else {\n signedRawTransactions.push(new Uint8Array(Buffer.from(txn, \"base64\")));\n }\n }\n\n return signedRawTransactions;\n }\n}\n\nexport default MyAlgoWalletClient;\n"],"names":["BaseWallet","Algod","PROVIDER_ID","providers"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;AAGG;AAmBH,MAAM,kBAAmB,SAAQA,eAAU,CAAA;AACzC,IAAA,OAAO,CAAgB;AACvB,IAAA,EAAE,CAAc;AAChB,IAAA,QAAQ,CAAiB;IAEzB,WAAY,CAAA,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,WAAW,EAAc,EAAA;AACpE,QAAA,KAAK,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;AAE5B,QAAA,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;AACtB,QAAA,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;AACb,QAAA,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;KAC1B;IAED,aAAa,IAAI,GAAA;QACf,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,GAAG,MAAMC,UAAK,CAAC,IAAI,EAAE,CAAC;QACpD,MAAM,aAAa,GAAG,CAAC,MAAM,mFAAO,0BAA0B,MA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C9B,aAAA;AACF,SAAA;QAED,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,eAAe,CAAC,kBAAkB,CAAC,CAAC;QAEtE,IAAI,CAAC,MAAM,EAAE;AACX,YAAA,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;AACpC,SAAA;QAED,IAAI,WAAW,GAAG,CAAC,CAAC;QAEpB,KAAK,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,IAAI,YAAY,EAAE;YACtC,IAAI,IAAI,KAAK,GAAG,EAAE;gBAChB,qBAAqB,CAAC,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,CAAC;AACrD,gBAAA,WAAW,EAAE,CAAC;AACf,aAAA;AAAM,iBAAA;AACL,gBAAA,qBAAqB,CAAC,IAAI,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;AACxE,aAAA;AACF,SAAA;AAED,QAAA,OAAO,qBAAqB,CAAC;KAC9B;AACF;;;;"}
|
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var providers = require('../providers.js');
|
|
4
|
-
var base = require('../base-9794b627.js');
|
|
5
|
-
var constants = require('../constants-e76d1f05.js');
|
|
6
|
-
|
|
7
|
-
function _interopNamespace(e) {
|
|
8
|
-
if (e && e.__esModule) return e;
|
|
9
|
-
var n = Object.create(null);
|
|
10
|
-
if (e) {
|
|
11
|
-
Object.keys(e).forEach(function (k) {
|
|
12
|
-
if (k !== 'default') {
|
|
13
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
14
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
15
|
-
enumerable: true,
|
|
16
|
-
get: function () { return e[k]; }
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
n["default"] = e;
|
|
22
|
-
return Object.freeze(n);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Helpful resources:
|
|
27
|
-
* https://github.com/perawallet/connect
|
|
28
|
-
*/
|
|
29
|
-
class PeraWalletClient extends base.BaseWallet {
|
|
30
|
-
#client;
|
|
31
|
-
id;
|
|
32
|
-
provider;
|
|
33
|
-
constructor({ client, id, provider, algosdk, algodClient }) {
|
|
34
|
-
super(algosdk, algodClient);
|
|
35
|
-
this.#client = client;
|
|
36
|
-
this.id = id;
|
|
37
|
-
this.provider = provider;
|
|
38
|
-
}
|
|
39
|
-
static async init() {
|
|
40
|
-
const { algosdk, algodClient } = await base.Algod.init();
|
|
41
|
-
const PeraWalletConnect = (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('@perawallet/connect')); }))
|
|
42
|
-
.PeraWalletConnect;
|
|
43
|
-
const peraWallet = new PeraWalletConnect({ shouldShowSignTxnToast: false });
|
|
44
|
-
return new PeraWalletClient({
|
|
45
|
-
id: constants.PROVIDER_ID.PERA_WALLET,
|
|
46
|
-
client: peraWallet,
|
|
47
|
-
provider: providers.providers[constants.PROVIDER_ID.PERA_WALLET],
|
|
48
|
-
algosdk: algosdk,
|
|
49
|
-
algodClient: algodClient,
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
async connect(onDisconnect) {
|
|
53
|
-
this.keepWCAliveStart();
|
|
54
|
-
const accounts = await this.#client.connect();
|
|
55
|
-
this.keepWCAliveStop();
|
|
56
|
-
this.#client.connector?.on("disconnect", onDisconnect);
|
|
57
|
-
if (accounts.length === 0) {
|
|
58
|
-
throw new Error(`No accounts found for ${this.provider}`);
|
|
59
|
-
}
|
|
60
|
-
const mappedAccounts = accounts.map((address, index) => ({
|
|
61
|
-
name: `Pera Wallet ${index + 1}`,
|
|
62
|
-
address,
|
|
63
|
-
providerId: this.provider.id,
|
|
64
|
-
}));
|
|
65
|
-
return {
|
|
66
|
-
...this.provider,
|
|
67
|
-
accounts: mappedAccounts,
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
async reconnect(onDisconnect) {
|
|
71
|
-
const accounts = await this.#client.reconnectSession();
|
|
72
|
-
this.#client.connector?.on("disconnect", onDisconnect);
|
|
73
|
-
if (!accounts) {
|
|
74
|
-
return null;
|
|
75
|
-
}
|
|
76
|
-
return {
|
|
77
|
-
...this.provider,
|
|
78
|
-
accounts: accounts.map((address, index) => ({
|
|
79
|
-
name: `Pera Wallet ${index + 1}`,
|
|
80
|
-
address,
|
|
81
|
-
providerId: this.provider.id,
|
|
82
|
-
})),
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
async disconnect() {
|
|
86
|
-
await this.#client.disconnect();
|
|
87
|
-
}
|
|
88
|
-
formatTransactionsArray(transactions) {
|
|
89
|
-
const formattedTransactions = [];
|
|
90
|
-
for (const [type, txn] of transactions) {
|
|
91
|
-
if (type === "s") {
|
|
92
|
-
formattedTransactions.push({
|
|
93
|
-
...this.algosdk.decodeSignedTransaction(new Uint8Array(Buffer.from(txn, "base64"))),
|
|
94
|
-
signers: [],
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
formattedTransactions.push({
|
|
99
|
-
txn: this.algosdk.decodeUnsignedTransaction(new Uint8Array(Buffer.from(txn, "base64"))),
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
return formattedTransactions;
|
|
104
|
-
}
|
|
105
|
-
async signTransactions(activeAdress, transactions) {
|
|
106
|
-
// Decode the transactions to access their properties.
|
|
107
|
-
const decodedTxns = transactions.map((txn) => {
|
|
108
|
-
return this.algosdk.decodeObj(txn);
|
|
109
|
-
});
|
|
110
|
-
// Marshal the transactions,
|
|
111
|
-
// and add the signers property if they shouldn't be signed.
|
|
112
|
-
const txnsToSign = decodedTxns.reduce((acc, txn, i) => {
|
|
113
|
-
if (!("txn" in txn) &&
|
|
114
|
-
this.algosdk.encodeAddress(txn["snd"]) === activeAdress) {
|
|
115
|
-
acc.push({
|
|
116
|
-
txn: this.algosdk.decodeUnsignedTransaction(transactions[i]),
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
acc.push({
|
|
121
|
-
txn: this.algosdk.decodeSignedTransaction(transactions[i]).txn,
|
|
122
|
-
signers: [],
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
return acc;
|
|
126
|
-
}, []);
|
|
127
|
-
// Play an audio file to keep Wallet Connect's web socket open on iOS
|
|
128
|
-
// when the user goes into background mode.
|
|
129
|
-
this.keepWCAliveStart();
|
|
130
|
-
// Sign them with the client.
|
|
131
|
-
const result = await this.#client.signTransaction([txnsToSign]);
|
|
132
|
-
this.keepWCAliveStop();
|
|
133
|
-
// Join the newly signed transactions with the original group of transactions.
|
|
134
|
-
const signedTxns = decodedTxns.reduce((acc, txn, i) => {
|
|
135
|
-
if (!("txn" in txn)) {
|
|
136
|
-
const signedByUser = result.shift();
|
|
137
|
-
signedByUser && acc.push(signedByUser);
|
|
138
|
-
}
|
|
139
|
-
else {
|
|
140
|
-
acc.push(transactions[i]);
|
|
141
|
-
}
|
|
142
|
-
return acc;
|
|
143
|
-
}, []);
|
|
144
|
-
return signedTxns;
|
|
145
|
-
}
|
|
146
|
-
async signEncodedTransactions(transactions) {
|
|
147
|
-
const transactionsToSign = this.formatTransactionsArray(transactions);
|
|
148
|
-
this.keepWCAliveStart();
|
|
149
|
-
const result = (await this.#client.signTransaction([
|
|
150
|
-
transactionsToSign,
|
|
151
|
-
]));
|
|
152
|
-
this.keepWCAliveStop();
|
|
153
|
-
const signedTransactions = [];
|
|
154
|
-
let resultIndex = 0;
|
|
155
|
-
for (const [type, txn] of transactions) {
|
|
156
|
-
if (type === "u") {
|
|
157
|
-
signedTransactions.push(result[resultIndex]);
|
|
158
|
-
resultIndex++;
|
|
159
|
-
}
|
|
160
|
-
else {
|
|
161
|
-
signedTransactions.push(new Uint8Array(Buffer.from(txn, "base64")));
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
return signedTransactions;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
module.exports = PeraWalletClient;
|
|
169
|
-
//# sourceMappingURL=perawallet.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"perawallet.js","sources":["../../../src/clients/perawallet.ts"],"sourcesContent":["/**\n * Helpful resources:\n * https://github.com/perawallet/connect\n */\nimport { providers } from \"../providers\";\nimport type _algosdk from \"algosdk\";\nimport Algod from \"../algod\";\nimport type { PeraWalletConnect } from \"@perawallet/connect\";\nimport type { WalletProvider, Wallet } from \"../types\";\nimport { PROVIDER_ID } from \"../constants\";\nimport type { Transaction } from \"algosdk\";\nimport BaseWallet from \"./base\";\nimport { TransactionsArray } from \"../types\";\nimport type { DecodedTransaction, DecodedSignedTransaction } from \"../types\";\n\nexport interface PeraTransaction {\n txn: Transaction;\n /**\n * Optional list of addresses that must sign the transactions.\n * Wallet skips to sign this txn if signers is empty array.\n * If undefined, wallet tries to sign it.\n */\n signers?: string[];\n}\n\ntype InitWallet = {\n id: PROVIDER_ID;\n client: PeraWalletConnect;\n provider: WalletProvider;\n algosdk: typeof _algosdk;\n algodClient: _algosdk.Algodv2;\n};\n\nclass PeraWalletClient extends BaseWallet {\n #client: PeraWalletConnect;\n id: PROVIDER_ID;\n provider: WalletProvider;\n\n constructor({ client, id, provider, algosdk, algodClient }: InitWallet) {\n super(algosdk, algodClient);\n\n this.#client = client;\n this.id = id;\n this.provider = provider;\n }\n\n static async init() {\n const { algosdk, algodClient } = await Algod.init();\n const PeraWalletConnect = (await import(\"@perawallet/connect\"))\n .PeraWalletConnect;\n\n const peraWallet = new PeraWalletConnect({ shouldShowSignTxnToast: false });\n\n return new PeraWalletClient({\n id: PROVIDER_ID.PERA_WALLET,\n client: peraWallet,\n provider: providers[PROVIDER_ID.PERA_WALLET],\n algosdk: algosdk,\n algodClient: algodClient,\n });\n }\n\n async connect(onDisconnect: () => void): Promise<Wallet> {\n this.keepWCAliveStart();\n\n const accounts = await this.#client.connect();\n\n this.keepWCAliveStop();\n\n this.#client.connector?.on(\"disconnect\", onDisconnect);\n\n if (accounts.length === 0) {\n throw new Error(`No accounts found for ${this.provider}`);\n }\n\n const mappedAccounts = accounts.map((address: string, index: number) => ({\n name: `Pera Wallet ${index + 1}`,\n address,\n providerId: this.provider.id,\n }));\n\n return {\n ...this.provider,\n accounts: mappedAccounts,\n };\n }\n\n async reconnect(onDisconnect: () => void) {\n const accounts = await this.#client.reconnectSession();\n this.#client.connector?.on(\"disconnect\", onDisconnect);\n\n if (!accounts) {\n return null;\n }\n\n return {\n ...this.provider,\n accounts: accounts.map((address: string, index: number) => ({\n name: `Pera Wallet ${index + 1}`,\n address,\n providerId: this.provider.id,\n })),\n };\n }\n\n async disconnect() {\n await this.#client.disconnect();\n }\n\n formatTransactionsArray(transactions: TransactionsArray): PeraTransaction[] {\n const formattedTransactions: PeraTransaction[] = [];\n\n for (const [type, txn] of transactions) {\n if (type === \"s\") {\n formattedTransactions.push({\n ...this.algosdk.decodeSignedTransaction(\n new Uint8Array(Buffer.from(txn, \"base64\"))\n ),\n signers: [],\n });\n } else {\n formattedTransactions.push({\n txn: this.algosdk.decodeUnsignedTransaction(\n new Uint8Array(Buffer.from(txn, \"base64\"))\n ),\n });\n }\n }\n\n return formattedTransactions;\n }\n\n async signTransactions(activeAdress: string, transactions: Uint8Array[]) {\n // Decode the transactions to access their properties.\n const decodedTxns = transactions.map((txn) => {\n return this.algosdk.decodeObj(txn);\n }) as Array<DecodedTransaction | DecodedSignedTransaction>;\n\n // Marshal the transactions,\n // and add the signers property if they shouldn't be signed.\n const txnsToSign = decodedTxns.reduce<PeraTransaction[]>((acc, txn, i) => {\n if (\n !(\"txn\" in txn) &&\n this.algosdk.encodeAddress(txn[\"snd\"]) === activeAdress\n ) {\n acc.push({\n txn: this.algosdk.decodeUnsignedTransaction(transactions[i]),\n });\n } else {\n acc.push({\n txn: this.algosdk.decodeSignedTransaction(transactions[i]).txn,\n signers: [],\n });\n }\n\n return acc;\n }, []);\n\n // Play an audio file to keep Wallet Connect's web socket open on iOS\n // when the user goes into background mode.\n this.keepWCAliveStart();\n\n // Sign them with the client.\n const result = await this.#client.signTransaction([txnsToSign]);\n\n this.keepWCAliveStop();\n\n // Join the newly signed transactions with the original group of transactions.\n const signedTxns = decodedTxns.reduce<Uint8Array[]>((acc, txn, i) => {\n if (!(\"txn\" in txn)) {\n const signedByUser = result.shift();\n signedByUser && acc.push(signedByUser);\n } else {\n acc.push(transactions[i]);\n }\n\n return acc;\n }, []);\n\n return signedTxns;\n }\n\n async signEncodedTransactions(transactions: TransactionsArray) {\n const transactionsToSign = this.formatTransactionsArray(transactions);\n\n this.keepWCAliveStart();\n\n const result = (await this.#client.signTransaction([\n transactionsToSign,\n ])) as Uint8Array[];\n\n this.keepWCAliveStop();\n\n const signedTransactions: Uint8Array[] = [];\n let resultIndex = 0;\n\n for (const [type, txn] of transactions) {\n if (type === \"u\") {\n signedTransactions.push(result[resultIndex]);\n resultIndex++;\n } else {\n signedTransactions.push(new Uint8Array(Buffer.from(txn, \"base64\")));\n }\n }\n\n return signedTransactions;\n }\n}\n\nexport default PeraWalletClient;\n"],"names":["BaseWallet","Algod","PROVIDER_ID","providers"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;AAGG;AA8BH,MAAM,gBAAiB,SAAQA,eAAU,CAAA;AACvC,IAAA,OAAO,CAAoB;AAC3B,IAAA,EAAE,CAAc;AAChB,IAAA,QAAQ,CAAiB;IAEzB,WAAY,CAAA,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,WAAW,EAAc,EAAA;AACpE,QAAA,KAAK,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;AAE5B,QAAA,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;AACtB,QAAA,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;AACb,QAAA,IAAI,CAAC,QAAQ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