Skip to content

Commit

Permalink
feat: improve signers section (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: Carlo Sala <[email protected]>
  • Loading branch information
peetzweg and carlosala authored Aug 12, 2024
1 parent 6d59f10 commit b179873
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
2 changes: 2 additions & 0 deletions docs/pages/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Now you can create a PolkadotClient with a provider of your choice and start int

// [!include ~/snippets/gettingStarted.ts:usage]
```

</Tabs.Content>
<Tabs.Content value="snmw">
```ts
Expand Down Expand Up @@ -85,6 +86,7 @@ Now you can create a PolkadotClient with a provider of your choice and start int
// [!include ~/snippets/gettingStarted.ts:usage]
```

</Tabs.Content>
<Tabs.Content value="sm">
```ts
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/recipes/upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function performTransfer() {
// check if we're running on the next version to run that first
if (
await nextApi.tx.Balances.new_fancy_transfer.isCompatible(
CompatibilityLevel.Partial
CompatibilityLevel.Partial,
)
) {
nextApi.tx.Balances.new_fancy_transfer({
Expand Down
14 changes: 8 additions & 6 deletions docs/pages/signers.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Signers

## `PolkadotSigner`

For transactions, the generated descriptors and its corresponding typed API are needed to create the transaction extrinsics, but for these transactions to be signed, we also need a signer, which is the responsible of taking it the call data and signing it.

Every method on Polkadot-API that needs to sign something, takes in a signer with the following interface:
Every method on Polkadot-API that needs to sign something, requires a `PolkadotSigner` with the following interface:

```ts
interface PolkadotSigner {
Expand All @@ -26,7 +28,7 @@ interface PolkadotSigner {

This interface is generic to signing transactions for the chain.

## `PolkadotSigner` from a browser extension
### From a browser extension

If you want to use a compatible extension as a signer, Polkadot-API has a subpath with a couple of utilities to help with this: `polkadot-api/pjs-signer`.

Expand All @@ -51,7 +53,7 @@ const accounts: InjectedPolkadotAccount[] = selectedExtension.getAccounts()
const polkadotSigner = accounts[0].polkadotSigner
```

## `PolkadotSigner` from generic signing function
### From a generic signing function

If you have a signer which takes some arbitrary data and just signs it with one of the supported algorithms, you can create a `PolkadotSigner` with the function `getPolkadotSigner` from `polkadot-api/signer`:

Expand All @@ -68,16 +70,16 @@ For example, using hdkd from `@polkadot-labs`:
```ts
import { sr25519CreateDerive } from "@polkadot-labs/hdkd"
import {
sr25519,
DEV_PHRASE,
entropyToMiniSecret,
mnemonicToEntropy,
} from "@polkadot-labs/hdkd-helpers"
import { getPolkadotSigner } from "polkadot-api/signer"
const entropy = mnemonicToEntropy(MNEMONIC)
const entropy = mnemonicToEntropy(DEV_PHRASE)
const miniSecret = entropyToMiniSecret(entropy)
const derive = sr25519CreateDerive(miniSecret)
const keypair = derive("//Alice")
const hdkdKeyPair = derive("//Alice")
const polkadotSigner = getPolkadotSigner(
hdkdKeyPair.publicKey,
Expand Down
11 changes: 8 additions & 3 deletions docs/pages/typed.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,15 @@ There's also a small utility next to `.getCompatibilityLevel()` to directly chec
```ts
interface IsCompatible {
(threshold: CompatibilityLevel): Promise<boolean>
(threshold: CompatibilityLevel, compatibilityToken: CompatibilityToken): boolean
(
threshold: CompatibilityLevel,
compatibilityToken: CompatibilityToken,
): boolean
}

// Possible "pseudocode" implementation, to show the equivalence
function isCompatible(threshold, token) {
return getCompatibilityLevel(token) >= threshold;
return getCompatibilityLevel(token) >= threshold
}
```

Expand All @@ -84,7 +87,9 @@ if (await query.isCompatible(CompatibilityLevel.BackwardsCompatible)) {
const compatibilityToken = await typedApi.compatibilityToken

// And later on we can use it, so that `getCompatibilityLevel` is sync
if (query.isCompatible(CompatibilityLevel.BackwardsCompatible, compatibilityToken)) {
if (
query.isCompatible(CompatibilityLevel.BackwardsCompatible, compatibilityToken)
) {
// do your stuff, the query is compatible
} else {
// the call is not compatible!
Expand Down

0 comments on commit b179873

Please sign in to comment.