@aptos-labs/wallet-adapter-core
Advanced tools
Comparing version 0.1.7 to 0.2.0
@@ -33,7 +33,10 @@ import { Types } from 'aptos'; | ||
declare type NetworkInfo = { | ||
name: NetworkName | undefined; | ||
name: NetworkName; | ||
chainId?: string; | ||
url?: string; | ||
}; | ||
declare type AccountInfo = { | ||
address: string; | ||
publicKey: string; | ||
publicKey: string | string[]; | ||
minKeysRequired?: number; | ||
}; | ||
@@ -100,5 +103,5 @@ interface AptosWalletErrorResult { | ||
interface SignMessageResponse { | ||
address: string; | ||
application: string; | ||
chainId: number; | ||
address?: string; | ||
application?: string; | ||
chainId?: number; | ||
fullMessage: string; | ||
@@ -108,3 +111,4 @@ message: string; | ||
prefix: "APTOS"; | ||
signature: string; | ||
signature: string | string[]; | ||
bitmap?: Uint8Array; | ||
} | ||
@@ -121,3 +125,3 @@ | ||
private scopePollingDetectionStrategy; | ||
private isWalletExists; | ||
private doesWalletExist; | ||
private clearData; | ||
@@ -198,4 +202,5 @@ setWallet(wallet: Wallet | null): void; | ||
onNetworkChange(): Promise<void>; | ||
signMessageAndVerify(message: SignMessagePayload): Promise<boolean>; | ||
} | ||
export { AccountInfo, AdapterPlugin, AdapterPluginEvents, AdapterPluginProps, AptosWalletErrorResult, NetworkInfo, NetworkName, PluginProvider, SignMessagePayload, SignMessageResponse, Wallet, WalletCore, WalletCoreEvents, WalletInfo, WalletName, WalletReadyState }; |
@@ -36,3 +36,6 @@ "use strict"; | ||
// src/WalletCore.ts | ||
var import_aptos = require("aptos"); | ||
var import_eventemitter3 = __toESM(require("eventemitter3")); | ||
var import_tweetnacl = require("tweetnacl"); | ||
var import_buffer = require("buffer"); | ||
@@ -121,2 +124,8 @@ // src/constants.ts | ||
}; | ||
var WalletSignMessageAndVerifyError = class extends WalletError { | ||
constructor() { | ||
super(...arguments); | ||
this.name = "WalletSignMessageAndVerifyError"; | ||
} | ||
}; | ||
var WalletSignAndSubmitMessageError = class extends WalletError { | ||
@@ -204,3 +213,3 @@ constructor() { | ||
} | ||
isWalletExists() { | ||
doesWalletExist() { | ||
if (!this._connected || this._connecting || !this._wallet) | ||
@@ -293,3 +302,3 @@ throw new WalletNotConnectedError().name; | ||
try { | ||
this.isWalletExists(); | ||
this.doesWalletExist(); | ||
await ((_a = this._wallet) == null ? void 0 : _a.disconnect()); | ||
@@ -305,3 +314,3 @@ this.clearData(); | ||
try { | ||
this.isWalletExists(); | ||
this.doesWalletExist(); | ||
const response = await ((_a = this._wallet) == null ? void 0 : _a.signAndSubmitTransaction( | ||
@@ -320,3 +329,3 @@ transaction | ||
return null; | ||
this.isWalletExists(); | ||
this.doesWalletExist(); | ||
const response = await this._wallet.signTransaction(transaction); | ||
@@ -332,3 +341,3 @@ return response; | ||
try { | ||
this.isWalletExists(); | ||
this.doesWalletExist(); | ||
if (!this._wallet) | ||
@@ -346,3 +355,3 @@ return null; | ||
try { | ||
this.isWalletExists(); | ||
this.doesWalletExist(); | ||
await ((_a = this._wallet) == null ? void 0 : _a.onAccountChange((data) => { | ||
@@ -360,3 +369,3 @@ this.setAccount({ ...data }); | ||
try { | ||
this.isWalletExists(); | ||
this.doesWalletExist(); | ||
await ((_a = this._wallet) == null ? void 0 : _a.onNetworkChange((data) => { | ||
@@ -371,2 +380,30 @@ this.setNetwork({ ...data }); | ||
} | ||
async signMessageAndVerify(message) { | ||
var _a; | ||
try { | ||
this.doesWalletExist(); | ||
if (!this._account) | ||
throw new Error("No account found!"); | ||
const response = await ((_a = this._wallet) == null ? void 0 : _a.signMessage(message)); | ||
if (!response) | ||
throw new WalletSignMessageAndVerifyError("Failed to sign a message").message; | ||
let verified = false; | ||
if (Array.isArray(response.signature)) { | ||
} else { | ||
const currentAccountPublicKey = new import_aptos.HexString( | ||
this._account.publicKey | ||
); | ||
const signature = new import_aptos.HexString(response.signature); | ||
verified = import_tweetnacl.sign.detached.verify( | ||
import_buffer.Buffer.from(response.fullMessage), | ||
import_buffer.Buffer.from(signature.noPrefix(), "hex"), | ||
import_buffer.Buffer.from(currentAccountPublicKey.noPrefix(), "hex") | ||
); | ||
} | ||
return verified; | ||
} catch (error) { | ||
const errMsg = typeof error == "object" && "message" in error ? error.message : error; | ||
throw new WalletSignMessageAndVerifyError(errMsg).message; | ||
} | ||
} | ||
}; | ||
@@ -373,0 +410,0 @@ // Annotate the CommonJS export names for ESM import in node: |
{ | ||
"name": "@aptos-labs/wallet-adapter-core", | ||
"version": "0.1.7", | ||
"version": "0.2.0", | ||
"description": "Aptos Wallet Adapter Core", | ||
@@ -30,19 +30,25 @@ "main": "./dist/index.js", | ||
], | ||
"scripts": { | ||
"build": "tsup src/index.ts --format esm,cjs --dts", | ||
"dev": "tsup src/index.ts --format esm,cjs --watch --dts", | ||
"lint": "TIMING=1 eslint \"src/**/*.ts*\"", | ||
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist" | ||
}, | ||
"devDependencies": { | ||
"@aptos-labs/wallet-adapter-tsconfig": "workspace:*", | ||
"@types/jest": "^29.2.4", | ||
"eslint": "^8.15.0", | ||
"@aptos-labs/eslint-config-adapter": "workspace:*", | ||
"jest": "^29.3.1", | ||
"ts-jest": "^29.0.3", | ||
"tsup": "^5.10.1", | ||
"typescript": "^4.5.3" | ||
"typescript": "^4.5.3", | ||
"@aptos-labs/eslint-config-adapter": "0.0.0", | ||
"@aptos-labs/wallet-adapter-tsconfig": "0.0.0" | ||
}, | ||
"dependencies": { | ||
"aptos": "^1.3.17", | ||
"eventemitter3": "^4.0.7" | ||
"buffer": "^6.0.3", | ||
"eventemitter3": "^4.0.7", | ||
"tweetnacl": "^1.0.3" | ||
}, | ||
"scripts": { | ||
"build": "tsup src/index.ts --format esm,cjs --dts", | ||
"dev": "tsup src/index.ts --format esm,cjs --watch --dts", | ||
"test": "jest", | ||
"lint": "TIMING=1 eslint \"src/**/*.ts*\"", | ||
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist" | ||
} | ||
} | ||
} |
@@ -74,2 +74,6 @@ export class WalletError extends Error { | ||
export class WalletSignMessageAndVerifyError extends WalletError { | ||
name = "WalletSignMessageAndVerifyError"; | ||
} | ||
export class WalletSignAndSubmitMessageError extends WalletError { | ||
@@ -76,0 +80,0 @@ name = "WalletSignAndSubmitMessageError"; |
@@ -9,3 +9,5 @@ import { Types } from "aptos"; | ||
export type NetworkInfo = { | ||
name: NetworkName | undefined; | ||
name: NetworkName; | ||
chainId?: string; | ||
url?: string; | ||
}; | ||
@@ -15,3 +17,4 @@ | ||
address: string; | ||
publicKey: string; | ||
publicKey: string | string[]; | ||
minKeysRequired?: number | ||
}; | ||
@@ -95,5 +98,5 @@ | ||
export interface SignMessageResponse { | ||
address: string; | ||
application: string; | ||
chainId: number; | ||
address?: string; | ||
application?: string; | ||
chainId?: number; | ||
fullMessage: string; // The message that was generated to sign | ||
@@ -103,3 +106,4 @@ message: string; // The message passed in by the user | ||
prefix: "APTOS"; // Should always be APTOS | ||
signature: string; // The signed full message | ||
signature: string | string[]; // The signed full message | ||
bitmap?: Uint8Array; // a 4-byte (32 bits) bit-vector of length N | ||
} |
@@ -1,3 +0,5 @@ | ||
import { Types } from "aptos"; | ||
import { HexString, Types } from "aptos"; | ||
import EventEmitter from "eventemitter3"; | ||
import { sign } from "tweetnacl"; | ||
import { Buffer } from "buffer"; | ||
@@ -16,2 +18,3 @@ import { WalletReadyState } from "./constants"; | ||
WalletSignAndSubmitMessageError, | ||
WalletSignMessageAndVerifyError, | ||
WalletSignMessageError, | ||
@@ -71,3 +74,3 @@ WalletSignTransactionError, | ||
private isWalletExists(): boolean | WalletNotConnectedError { | ||
private doesWalletExist(): boolean | WalletNotConnectedError { | ||
if (!this._connected || this._connecting || !this._wallet) | ||
@@ -202,3 +205,3 @@ throw new WalletNotConnectedError().name; | ||
try { | ||
this.isWalletExists(); | ||
this.doesWalletExist(); | ||
await this._wallet?.disconnect(); | ||
@@ -222,3 +225,3 @@ this.clearData(); | ||
try { | ||
this.isWalletExists(); | ||
this.doesWalletExist(); | ||
const response = await this._wallet?.signAndSubmitTransaction( | ||
@@ -246,3 +249,3 @@ transaction | ||
if (this._wallet && !("signTransaction" in this._wallet)) return null; | ||
this.isWalletExists(); | ||
this.doesWalletExist(); | ||
const response = await (this._wallet as any).signTransaction(transaction); | ||
@@ -267,3 +270,3 @@ return response; | ||
try { | ||
this.isWalletExists(); | ||
this.doesWalletExist(); | ||
if (!this._wallet) return null; | ||
@@ -286,3 +289,3 @@ const response = await this._wallet?.signMessage(message); | ||
try { | ||
this.isWalletExists(); | ||
this.doesWalletExist(); | ||
await this._wallet?.onAccountChange((data: AccountInfo) => { | ||
@@ -306,3 +309,3 @@ this.setAccount({ ...data }); | ||
try { | ||
this.isWalletExists(); | ||
this.doesWalletExist(); | ||
await this._wallet?.onNetworkChange((data: NetworkInfo) => { | ||
@@ -318,2 +321,37 @@ this.setNetwork({ ...data }); | ||
} | ||
async signMessageAndVerify(message: SignMessagePayload): Promise<boolean> { | ||
try { | ||
this.doesWalletExist(); | ||
if (!this._account) throw new Error("No account found!"); | ||
const response = await this._wallet?.signMessage(message); | ||
if (!response) | ||
throw new WalletSignMessageAndVerifyError("Failed to sign a message") | ||
.message; | ||
// Verify that the bytes were signed using the private key that matches the known public key | ||
let verified = false; | ||
if (Array.isArray(response.signature)) { | ||
// multi sig wallets | ||
// TODO - implement multi sig wallets | ||
} else { | ||
// single sig wallets | ||
// support for when address doesnt have hex prefix (0x) | ||
const currentAccountPublicKey = new HexString( | ||
this._account.publicKey as string | ||
); | ||
// support for when address doesnt have hex prefix (0x) | ||
const signature = new HexString(response.signature); | ||
verified = sign.detached.verify( | ||
Buffer.from(response.fullMessage), | ||
Buffer.from(signature.noPrefix(), "hex"), | ||
Buffer.from(currentAccountPublicKey.noPrefix(), "hex") | ||
); | ||
} | ||
return verified; | ||
} catch (error: any) { | ||
const errMsg = | ||
typeof error == "object" && "message" in error ? error.message : error; | ||
throw new WalletSignMessageAndVerifyError(errMsg).message; | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
54468
16
1630
4
8
+ Addedbuffer@^6.0.3
+ Addedtweetnacl@^1.0.3
+ Addedbase64-js@1.5.1(transitive)
+ Addedbuffer@6.0.3(transitive)
+ Addedieee754@1.2.1(transitive)