Skip to content

Commit

Permalink
fix elrond
Browse files Browse the repository at this point in the history
  • Loading branch information
XueMoMo committed Oct 17, 2024
1 parent e1c152f commit fd29cc2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 32 deletions.
41 changes: 17 additions & 24 deletions lib/wallet/Elrond.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseWallet, LoginUser } from "./types";
import { Address, SignableMessage, ExtensionProvider } from "@elrondnetwork/erdjs";
import { ExtensionProvider } from "@multiversx/sdk-extension-provider";
import { Message, MessageComputer } from "@multiversx/sdk-core";

export class Elrond extends BaseWallet {
name = "Elrond";
Expand All @@ -16,45 +17,37 @@ export class Elrond extends BaseWallet {
this.provider = provider;
}
this.isInit = true;
await super.init(old)
await super.init(old);
}

async fetchAccounts(): Promise<string[]> {
if(this.provider?.account?.address){
return [this.provider?.account?.address]
try {
const { address } = this.provider.getAccount();
return [address];
} catch (error) {
return [];
}
return []
}
async connect(): Promise<LoginUser> {
if (!this.isConnected) {
if (!this.provider) throw "Elrond (Maiar Wallet) not installed";
await this.provider.login({
callbackUrl: encodeURIComponent(`${window.location.origin}/#/files`),
});
const { address } = this.provider.account;
if (!this.provider) throw "Elrond (MultiversX Wallet) not installed";
const { address } = await this.provider.login();
this.account = address;
this.accounts = await this.fetchAccounts();
}
return { account: this.account, wallet: "elrond" };
}
async sign(): Promise<string> {
const address = this.provider.account.address;
const signableMessage = new SignableMessage({
address: new Address(address),
message: Buffer.from("0x" + Buffer.from(address).toString("hex"), "ascii"),
const mc = new MessageComputer();
const message = new Message({ data: Buffer.from("0x" + Buffer.from(this.account).toString("hex"), "ascii") as any });
const msg = Buffer.from(mc.computeBytesForSigning(message)).toString("hex");
return this.provider.signMessage(message).then((data) => {
return `${msg}:${Buffer.from(data.signature).toString("hex")}`;
});
return this.provider
.signMessage(signableMessage)
.then((message) => {
return `elrond-${address}-${signableMessage.serializeForSigning().toString("hex")}:${message.signature.hex()}`;
})
.catch((err) => {
console.error("Elrond wallet signMessage error", err);
return "";
});
}

disconnect() {
super.disconnect();
this.provider?.logout()
this.provider?.logout();
}
}
7 changes: 1 addition & 6 deletions lib/wallet/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ export const getPerfix = (user: LoginUser): string => {
if (user.wallet.startsWith("metamask") || user.wallet === "metax" || user.wallet === "wallet-connect") {
return "eth";
}

// if (user.wallet === "near") {
// return "near";
// }

if (user.wallet === "flow") {
return "flow";
}
Expand Down Expand Up @@ -91,7 +86,7 @@ export async function updateAuth(u: LoginUser, w: BaseWallet = WALLETMAP[u.walle
const signature = await w.sign(data, u.account);
u.signature = signature;
const prefix = getPerfix(u);
const authdata = u.wallet === "elrond" ? signature : `${prefix}-${data}${signature.includes(":") ? "-" + signature : ":" + signature}`;
const authdata = `${prefix}-${data}${signature.includes(":") ? "-" + signature : ":" + signature}`;
const base64Auth = window.btoa(authdata);
const authBasic = `${base64Auth}`;
const authBearer = `${base64Auth}`;
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"@mdx-js/loader": "^1.6.22",
"@mimirdev/apps-inject": "^0.2.1",
"@mimirdev/apps-sdk": "^0.1.1",
"@multiversx/sdk-core": "^13.9.0",
"@multiversx/sdk-extension-provider": "^4.0.0",
"@next/mdx": "^12.0.7",
"@onflow/fcl": "^1.12.1",
"@onflow/types": "^0.0.5",
Expand Down Expand Up @@ -76,14 +78,14 @@
"web3": "^1.8.0"
},
"devDependencies": {
"@walletconnect/types": "2.x",
"@next/bundle-analyzer": "11.x",
"@types/axios": "^0.14.0",
"@types/node": "18.x",
"@types/react": "^18.3.11",
"@types/react-dom": "18.x",
"@typescript-eslint/eslint-plugin": "4.26.1",
"@typescript-eslint/parser": "4.26.1",
"@walletconnect/types": "2.x",
"babel-plugin-styled-components": "^1.12.0",
"eslint": "7.28.0",
"eslint-plugin-react": "7.24.0",
Expand Down
31 changes: 30 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,30 @@
"@motionone/dom" "^10.16.4"
tslib "^2.3.1"

"@multiversx/sdk-core@^13.9.0":
version "13.9.0"
resolved "https://registry.npmjs.org/@multiversx/sdk-core/-/sdk-core-13.9.0.tgz#9a4cce2d355d376917fa78d05a515ee0bd8f3e96"
integrity sha512-WYA5fssMt/jTRTT+dkZW6V0ykCrkczhJ3BbZni6LhHjwFti7lMboVdoUMCIyPbecWZIEdmLq0OTcjOLy9tZX0g==
dependencies:
"@multiversx/sdk-transaction-decoder" "1.0.2"
bech32 "1.1.4"
blake2b "2.1.3"
buffer "6.0.3"
json-bigint "1.0.0"
keccak "3.0.2"

"@multiversx/sdk-extension-provider@^4.0.0":
version "4.0.0"
resolved "https://registry.npmjs.org/@multiversx/sdk-extension-provider/-/sdk-extension-provider-4.0.0.tgz#77a7d45bf09ae22e569267270f17ac01c5f97d17"
integrity sha512-zFt4jUS7tu+MlDPou9wmzoW1dcvQuz0nBEYY75ynt82wC7aYjSDhsYh3U4DKtj5TgmyoOSZ31CLJN0wcoNYVaw==

"@multiversx/[email protected]":
version "1.0.2"
resolved "https://registry.npmjs.org/@multiversx/sdk-transaction-decoder/-/sdk-transaction-decoder-1.0.2.tgz#83ded4f6d4b877b4421234856eb19709be2af31b"
integrity sha512-j43QsKquu8N51WLmVlJ7dV2P3A1448R7/ktvl8r3i6wRMpfdtzDPNofTdHmMRT7DdQdvs4+RNgz8hVKL11Etsw==
dependencies:
bech32 "^2.0.0"

"@next/[email protected]":
version "11.1.4"
resolved "https://registry.npmjs.org/@next/bundle-analyzer/-/bundle-analyzer-11.1.4.tgz#cb1ebc6a7452a021cc34341e778c8c7219066f9a"
Expand Down Expand Up @@ -4312,6 +4336,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/bech32/-/bech32-1.1.4.tgz#e38c9f37bf179b8eb16ae3a772b40c356d4832e9"
integrity sha512-s0IrSOzLlbvX7yp4WBfPITzpAU8sqQcpsmwXDiKwrG4r491vwCO/XpejasRNl0piBMe/DvP4Tz0mIS/X1DPJBQ==

bech32@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/bech32/-/bech32-2.0.0.tgz#078d3686535075c8c79709f054b1b226a133b355"
integrity sha512-LcknSilhIGatDAsY1ak2I8VtGaHNhgMSYVxFrGLXv+xLHytaKZKcaUJJUE7qmBr7h33o5YQwP55pMI0xmkpJwg==

before-after-hook@^2.2.0:
version "2.2.2"
resolved "https://registry.yarnpkg.com/before-after-hook/-/before-after-hook-2.2.2.tgz#a6e8ca41028d90ee2c24222f201c90956091613e"
Expand Down Expand Up @@ -7917,7 +7946,7 @@ jsprim@^1.2.2:
array-includes "^3.1.3"
object.assign "^4.1.2"

keccak@^3.0.0, keccak@^3.0.1:
keccak@3.0.2, keccak@^3.0.0, keccak@^3.0.1:
version "3.0.2"
resolved "https://registry.yarnpkg.com/keccak/-/keccak-3.0.2.tgz#4c2c6e8c54e04f2670ee49fa734eb9da152206e0"
integrity sha512-PyKKjkH53wDMLGrvmRGSNWgmSxZOUqbnXwKL9tmgbFYA1iAYqW21kfR7mZXV0MlESiefxQQE9X9fTa3X+2MPDQ==
Expand Down

0 comments on commit fd29cc2

Please sign in to comment.