-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Browser extension signing example #1067
Merged
tadeohepperle
merged 35 commits into
master
from
tadeo-hepperle-browser-extension-signing-example
Aug 2, 2023
Merged
Changes from 32 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
d4c8158
routing and signing example
tadeohepperle e2ffe65
cliipy fix
tadeohepperle 0efc233
submitting extrinsics
tadeohepperle b40026d
change order of lines
tadeohepperle 526803c
Skip call variants if there aren't any (#980)
jsdw b5ae185
Tidy up some metadata accessing (#978)
jsdw 58813bf
Bump tokio from 1.28.1 to 1.28.2 (#984)
dependabot[bot] cb223d7
Bump regex from 1.8.2 to 1.8.3 (#986)
dependabot[bot] 5e58805
Bump quote from 1.0.27 to 1.0.28 (#983)
dependabot[bot] a493369
Bump proc-macro2 from 1.0.58 to 1.0.59 (#985)
dependabot[bot] add2339
restrict sign_with_address_and_signature interface (#988)
tadeohepperle bd1f29d
changing js bridge
tadeohepperle 2f54934
dryrunresult ok
tadeohepperle a7b3433
submitting extrinsic working
tadeohepperle 37be177
tiny up code and ui
tadeohepperle a0a1cc6
Merge branch 'master' into tadeo-hepperle-browser-extension-signing-e…
tadeohepperle 4e5c107
formatting
tadeohepperle ef9d739
remove todos
tadeohepperle 8f6658b
Merge branch 'master' into tadeo-hepperle-browser-extension-signing-e…
tadeohepperle 9f0de42
Merge branch 'master' into tadeo-hepperle-browser-extension-signing-e…
tadeohepperle 66dd32d
support tip and mortality
tadeohepperle 402db95
Prevent bug when reusing type ids in hashing (#1075)
tadeohepperle 953d7c0
small adjustment
tadeohepperle c0a63ef
Merge branch 'master' into tadeo-hepperle-browser-extension-signing-e…
tadeohepperle d5c969a
Merge branch 'master' into tadeo-hepperle-browser-extension-signing-e…
tadeohepperle 1c6e248
Merge branch 'master' into tadeo-hepperle-browser-extension-signing-e…
tadeohepperle b0a8bd1
Merge branch 'master' into tadeo-hepperle-browser-extension-signing-e…
tadeohepperle 23ac08b
fix lock file
tadeohepperle 32cb9b7
Merge branch 'master' into tadeo-hepperle-browser-extension-signing-e…
tadeohepperle 99abc29
Merge branch 'master' into tadeo-hepperle-browser-extension-signing-e…
tadeohepperle b776032
Merge branch 'master' into tadeo-hepperle-browser-extension-signing-e…
jsdw 1fbb0f9
tell users how to add Alice account to run signing example
jsdw ea6f8be
adjust to PR comments
tadeohepperle eb01971
Merge branch 'master' into tadeo-hepperle-browser-extension-signing-e…
tadeohepperle 0e64607
fmt
tadeohepperle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,92 @@ | ||
/// The `@polkadot/extension-dapp` package can be dynamically imported. | ||
/// Usually it is wise to use a package manager like npm or yarn to install it as a dependency. | ||
/// The `getPolkadotJsExtensionMod` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not super familiar with js docs, but is this line complete here? Or should we remove it? |
||
|
||
/** | ||
* The `@polkadot/extension-dapp` package can be dynamically imported. | ||
* Usually it is wise to use a package manager like npm or yarn to install it as a dependency. | ||
* | ||
* The `getPolkadotJsExtensionMod` closure returns the `@polkadot/extension-dapp` module on demand. | ||
*/ | ||
let getPolkadotJsExtensionMod = (() => { | ||
let mod = null; | ||
|
||
// initialize `@polkadot/extension-dapp` module on page load | ||
let initPromise = (async () => { | ||
mod = await import( | ||
"https://cdn.jsdelivr.net/npm/@polkadot/[email protected]/+esm" | ||
); | ||
})(); | ||
|
||
// return a function that waits for initialization to be finished, in case mod is not initialized yet. | ||
return async () => { | ||
if (mod == null) { | ||
await initPromise; | ||
} | ||
return mod; | ||
}; | ||
})(); | ||
|
||
/** | ||
* Queries wallets from browser extensions like Talisman and the Polkadot.js extension for user accounts. | ||
* | ||
* @returns a json string that contains all the accounts that were found. | ||
*/ | ||
async function getAccounts() { | ||
const extensionMod = await getPolkadotJsExtensionMod(); | ||
await extensionMod.web3Enable("Subxt Example App"); | ||
const allAccounts = await extensionMod.web3Accounts(); | ||
const accountObjects = allAccounts.map((account) => ({ | ||
name: account.meta.name, // e.g. "Alice" | ||
source: account.meta.source, // e.g. "talisman", "polkadot-js" | ||
ty: account.type, // e.g. "sr25519" | ||
address: account.address // e.g. "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" | ||
})); | ||
return JSON.stringify(accountObjects); | ||
} | ||
|
||
/** | ||
* Signs a payload via browser extension | ||
* | ||
* @param payloadAsStr a string representing a JSON object like this: | ||
* let payload = { | ||
* "specVersion": "0x000024d6", | ||
* "transactionVersion": "0x00000018", | ||
* "address": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", | ||
* "blockHash": "0xd7aad6185db012b7ffbce710b55234d6c9589170566b925ee50cfa3d7f1e6f8f", | ||
* "blockNumber": "0x00000000", | ||
* "era": "0x0000", | ||
* "genesisHash": "0xd7aad6185db012b7ffbce710b55234d6c9589170566b925ee50cfa3d7f1e6f8f", | ||
* "method": "0x0503001cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c0b00c465f14670", | ||
* "nonce": "0x00000000", | ||
* "signedExtensions": [ | ||
* "CheckNonZeroSender", | ||
* "CheckSpecVersion", | ||
* "CheckTxVersion", | ||
* "CheckGenesis", | ||
* "CheckMortality", | ||
* "CheckNonce", | ||
* "CheckWeight", | ||
* "ChargeTransactionPayment", | ||
* "PrevalidateAttests" | ||
* ], | ||
* "tip": "0x00000000000000000000000000000000", | ||
* "version": 4 | ||
* }; | ||
* @param source the extension used for signing as a string | ||
* @param address the ss58 encoded address as a string | ||
* @returns {Promise<*>} | ||
*/ | ||
async function signPayload(payloadAsStr, source, address) { | ||
let payload = JSON.parse(payloadAsStr); | ||
const extensionMod = await getPolkadotJsExtensionMod(); | ||
const injector = await extensionMod.web3FromSource(source); | ||
const signPayload = injector?.signer?.signPayload; | ||
if (!!signPayload) { | ||
const {signature} = await signPayload(payload); | ||
console.log("signature js:", signature) | ||
return signature; | ||
} else { | ||
throw "The extension's injector does not have a `signPayload` function on its `signer`"; | ||
} | ||
} |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: we could add a new line here and below