forked from aptos-labs/aptos-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[web-wallet] Add scripts to pages for dapps
Closes: aptos-labs#758
- Loading branch information
1 parent
a1b0ea0
commit 7984743
Showing
13 changed files
with
1,771 additions
and
1,320 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
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,16 @@ | ||
// Copyright (c) Aptos | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
function injectScript () { | ||
try { | ||
const container = document.head || document.documentElement | ||
const scriptTag = document.createElement('script') | ||
scriptTag.src = chrome.runtime.getURL('inpage.js') | ||
container.insertBefore(scriptTag, container.children[0]) | ||
container.removeChild(scriptTag) | ||
} catch (error) { | ||
console.error('Aptos injection failed.', error) | ||
} | ||
} | ||
|
||
injectScript() |
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,34 @@ | ||
// Copyright (c) Aptos | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
const extensionId = 'jmmhmakollmjgkjgnbeppkccgddmjnib' | ||
|
||
class Web3 { | ||
account () { | ||
return new Promise(function (resolve, reject) { | ||
chrome.runtime.sendMessage(extensionId, { method: 'getAccountAddress' }, function (response) { | ||
if (response.address) { | ||
resolve(response.address) | ||
} else { | ||
reject(response.error ?? 'Error') | ||
} | ||
return true | ||
}) | ||
}) | ||
} | ||
|
||
signTransaction (transaction, completion) { | ||
return new Promise(function (resolve, reject) { | ||
chrome.runtime.sendMessage(extensionId, { method: 'signTransaction', transaction }, function (response) { | ||
if (response.transaction) { | ||
resolve(response.transaction) | ||
} else { | ||
reject(response.error ?? 'Error') | ||
} | ||
return true | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
window.aptos = new Web3() |
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,45 @@ | ||
// Copyright (c) Aptos | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { AptosAccount, AptosClient, Types } from 'aptos' | ||
import { devnetNodeUrl } from './constants' | ||
import { MessageMethod } from './types' | ||
import { getAptosAccountState } from './utils/account' | ||
|
||
chrome.runtime.onMessageExternal.addListener(async function (request, _sender, sendResponse) { | ||
const account = getAptosAccountState() | ||
if (account === undefined) { | ||
sendResponse({ error: 'No Accounts' }) | ||
return | ||
} | ||
switch (request.method) { | ||
case MessageMethod.GET_ACCOUNT_ADDRESS: | ||
getAccountAddress(account, sendResponse) | ||
break; | ||
case MessageMethod.SIGN_TRANSACTION: | ||
signTransaction(account, request.transaction, sendResponse) | ||
break; | ||
default: | ||
throw(request.method + ' method is not supported') | ||
} | ||
}) | ||
|
||
function getAccountAddress( account: AptosAccount, sendResponse: (response?: any) => void) { | ||
if (account.address()) { | ||
sendResponse({ address: account.address().hex() }) | ||
} else { | ||
sendResponse({ error: 'No accounts signed in' }) | ||
} | ||
} | ||
|
||
async function signTransaction (account: AptosAccount, transaction: Types.UserTransactionRequest, sendResponse: (response?: any) => void) { | ||
const client = new AptosClient(devnetNodeUrl) | ||
const message = await client.createSigningMessage(transaction) | ||
const signatureHex = account.signHexString(message.substring(2)) | ||
const transactionSignature = { | ||
type: 'ed25519_signature', | ||
public_key: account.pubKey().hex(), | ||
signature: signatureHex.hex() | ||
} | ||
sendResponse({ transaction: { signature: transactionSignature, ...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
13 changes: 13 additions & 0 deletions
13
ecosystem/web-wallet/src/types.tsx → ecosystem/web-wallet/src/types.ts
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
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,10 @@ | ||
module.exports = { | ||
resolve: { | ||
extensions: ['.ts', '.tsx', '.js', '.json'] | ||
}, | ||
module: { | ||
rules: [ | ||
{ test: /\.tsx?$/, loader: "ts-loader" } | ||
] | ||
} | ||
}; |
Oops, something went wrong.