-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for BCS transaction (#111)
* support bcs transaction * add aptos wallet plugin example * update pnpm * add changeset
- Loading branch information
Showing
13 changed files
with
758 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@aptos-labs/wallet-adapter-nextjs-example": major | ||
"@aptos-labs/aptos-wallet-plugin": major | ||
"@aptos-labs/wallet-adapter-core": major | ||
"@aptos-labs/wallet-adapter-react": major | ||
--- | ||
|
||
Add support to submit BCS transaction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
root: true, | ||
extends: ["adapter"], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
.turbo | ||
.eslintrc.js | ||
tsconfig.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "@aptos-labs/aptos-wallet-plugin", | ||
"version": "0.1.3", | ||
"description": "Aptos plugin to use with Aptos Wallet Adapter", | ||
"author": "Aptos", | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"license": "Apache-2.0", | ||
"exports": { | ||
"require": "./dist/index.js", | ||
"import": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts" | ||
}, | ||
"scripts": { | ||
"build": "tsup src/index.ts --format esm,cjs --dts", | ||
"dev": "tsup src/index.ts --format esm,cjs --watch --dts", | ||
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist", | ||
"test": "jest" | ||
}, | ||
"dependencies": { | ||
"@aptos-labs/wallet-adapter-core": "workspace:*", | ||
"aptos": "^1.4.0" | ||
}, | ||
"devDependencies": { | ||
"@aptos-labs/eslint-config-adapter": "workspace:*", | ||
"@aptos-labs/wallet-adapter-tsconfig": "workspace:*", | ||
"@swc/core": "^1.3.47", | ||
"@types/jest": "^29.2.3", | ||
"jest": "^29.3.1", | ||
"ts-jest": "^29.0.3", | ||
"tsup": "^6.5.0", | ||
"typescript": "^4.9.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import type { | ||
AptosWalletErrorResult, | ||
NetworkName, | ||
PluginProvider, | ||
AccountInfo, | ||
AdapterPlugin, | ||
NetworkInfo, | ||
SignMessagePayload, | ||
SignMessageResponse, | ||
WalletName, | ||
TransactionPayload, | ||
} from "@aptos-labs/wallet-adapter-core"; | ||
import { Types } from "aptos"; | ||
|
||
interface AptosWindow extends Window { | ||
aptos?: PluginProvider; | ||
} | ||
|
||
declare const window: AptosWindow; | ||
|
||
export const AptosWalletName = "Aptos" as WalletName<"Aptos">; | ||
|
||
export class AptosWallet implements AdapterPlugin { | ||
readonly name = AptosWalletName; | ||
readonly url = "https://chrome.google.com/webstore/detail/aptos-wallet"; | ||
readonly icon = | ||
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAWbSURBVHgB7Z09c9NYFIaPlFSpUqQNK6rQhbSkWJghLZP9BesxfwAqytg1xe7+AY+3go5ACzObBkpwSqrVQkuRCiqkva8UZW1je22wpHPveZ8ZRU6wwwznueee+6FLJCuSdzrb7nZTNjaOJc9/ctdNiaJESPPkeeq+phLH5/L162k0HJ7JikTLvtEFPnFBf+D+0l/dt9tCNJK6xnjmZOg7GdJlPvC/AhQtPo5P3MsHQvwhiobLiLBQABf82y74z4Qt3ldSybKHToLTeW+I5/1B3u2euOD/JQy+zyRowEUs5zAzA1x+oCckJHrRYNCf/uE3AjD4QfONBBMC5PfvY2j3TEi4ZNmd8eHilQDFMK/s8xMhIXPhJLjuJLjAN/8VgRsbPWHwLbAtm5tXRWGRAS5b/99C7FBmgbTMAGXrJ5aIomJir8wA3S5afyLEEkUtEBezfQy+RYpFvdilgmMhNnGxRw2wL8QqScy1fMNE0T4yQCLEKkksxDQUwDj2BNjbK69pdndn/zxwNsUCCOyNGyJ374psbYkMBiLv30++59o1kW5X5NMnkdFI5OXL8nXghCsAAn10NL/Fz2NnpxQFFyR5/bq8BypDWAIg6AcHIoeH60nn4/K8e1deECIgwhAAQULQEXxIUAf43bju3ZvMDJ7jrwDT/XpToIvABeECqBf8EuB7+/W6CKBe0C/Auvv1uvC0XtArQBP9el14VC/oEqCtfr0uPKgX2hdAW79eF0rrhfYFQPCRKi1RyY4ZyZYF4GKQcSiAcSiAcSiAcSiAcSiAcSiAcSiAcSiAcSiAcSiAcSiAcSiAcShAm3z+LG1DAdqEAhjn40dpGwrQFtgIwgxgGAWtH1CAtsC2cQVQgLZQsk2cAr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provider: PluginProvider | undefined = | ||
typeof window !== "undefined" ? window.aptos : undefined; | ||
|
||
async connect(): Promise<AccountInfo> { | ||
try { | ||
const addressInfo = await this.provider?.connect(); | ||
if (!addressInfo) throw `${AptosWalletName} Address Info Error`; | ||
return addressInfo; | ||
} catch (error: any) { | ||
throw error; | ||
} | ||
} | ||
|
||
async account(): Promise<AccountInfo> { | ||
const response = await this.provider?.account(); | ||
if (!response) throw `${AptosWalletName} Account Error`; | ||
return response; | ||
} | ||
|
||
async disconnect(): Promise<void> { | ||
try { | ||
await this.provider?.disconnect(); | ||
} catch (error: any) { | ||
throw error; | ||
} | ||
} | ||
|
||
async signAndSubmitTransaction( | ||
transaction: TransactionPayload, | ||
options?: any | ||
): Promise<{ hash: Types.HexEncodedBytes }> { | ||
try { | ||
const response = await this.provider?.signAndSubmitTransaction( | ||
transaction, | ||
options | ||
); | ||
if ((response as AptosWalletErrorResult).code) { | ||
throw new Error((response as AptosWalletErrorResult).message); | ||
} | ||
return response as { hash: Types.HexEncodedBytes }; | ||
} catch (error: any) { | ||
const errMsg = error.message; | ||
throw errMsg; | ||
} | ||
} | ||
|
||
async signMessage(message: SignMessagePayload): Promise<SignMessageResponse> { | ||
try { | ||
if (typeof message !== "object" || !message.nonce) { | ||
throw `${AptosWalletName} Invalid signMessage Payload`; | ||
} | ||
const response = await this.provider?.signMessage(message); | ||
if (response) { | ||
return response; | ||
} else { | ||
throw `${AptosWalletName} Sign Message failed`; | ||
} | ||
} catch (error: any) { | ||
const errMsg = error.message; | ||
throw errMsg; | ||
} | ||
} | ||
|
||
async onNetworkChange(callback: any): Promise<void> { | ||
try { | ||
const handleNetworkChange = async (newNetwork: { | ||
networkName: NetworkInfo; | ||
}): Promise<void> => { | ||
callback({ | ||
name: newNetwork.networkName, | ||
chainId: undefined, | ||
api: undefined, | ||
}); | ||
}; | ||
await this.provider?.onNetworkChange(handleNetworkChange); | ||
} catch (error: any) { | ||
const errMsg = error.message; | ||
throw errMsg; | ||
} | ||
} | ||
|
||
async onAccountChange(callback: any): Promise<void> { | ||
try { | ||
const handleAccountChange = async ( | ||
newAccount: AccountInfo | ||
): Promise<void> => { | ||
if (newAccount?.publicKey) { | ||
callback({ | ||
publicKey: newAccount.publicKey, | ||
address: newAccount.address, | ||
}); | ||
} else { | ||
const response = await this.connect(); | ||
callback({ | ||
address: response?.address, | ||
publicKey: response?.publicKey, | ||
}); | ||
} | ||
}; | ||
await this.provider?.onAccountChange(handleAccountChange); | ||
} catch (error: any) { | ||
console.log(error); | ||
const errMsg = error.message; | ||
throw errMsg; | ||
} | ||
} | ||
|
||
async network(): Promise<NetworkInfo> { | ||
try { | ||
const response = await this.provider?.network(); | ||
if (!response) throw `${AptosWalletName} Network Error`; | ||
return { | ||
name: response as NetworkName, | ||
}; | ||
} catch (error: any) { | ||
throw error; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "@aptos-labs/wallet-adapter-tsconfig/base.json", | ||
"include": ["."], | ||
"exclude": ["dist", "build", "node_modules"], | ||
"compilerOptions": { | ||
"target": "es5", | ||
"lib": ["ES2015", "dom", "es2017", "ES2019.Array"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.