Skip to content
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

feat: development wallet #289

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/new-points-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@reactive-dot/wallet-development": minor
"@reactive-dot/core": minor
---

Added development wallet.
2 changes: 1 addition & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
"cache": true
}
},
"parallel": 9
"parallel": 10
}
4 changes: 2 additions & 2 deletions packages/core/src/wallets/local-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { Wallet, type WalletOptions } from "./wallet.js";
*/
export abstract class LocalWallet<
TAccount extends Pick<PolkadotSignerAccount, "id">,
TOptions extends WalletOptions,
TStorageKey extends string,
TOptions extends WalletOptions = WalletOptions,
TStorageKey extends string = string,
> extends Wallet<TOptions, TStorageKey> {
/**
* @experimental
Expand Down
1 change: 1 addition & 0 deletions packages/wallet-development/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
4 changes: 4 additions & 0 deletions packages/wallet-development/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import recommended from "@reactive-dot/eslint-config";
import tseslint from "typescript-eslint";

export default tseslint.config(...recommended);
43 changes: 43 additions & 0 deletions packages/wallet-development/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "@reactive-dot/wallet-development",
"version": "0.16.12",
"description": "Development wallet adapter for Reactive DOT",
"keywords": [
"substrate",
"polkadot"
],
"homepage": "https://reactivedot.dev/",
"bugs": {
"url": "https://github.com/tien/reactive-dot/issues",
"email": "[email protected]"
},
"license": "LGPL-3.0-or-later",
"author": "Tiến Nguyễn Khắc <[email protected]> (https://tien.zone/)",
"repository": {
"type": "git",
"url": "https://github.com/tien/reactive-dot.git",
"directory": "packages/wallet-development"
},
"type": "module",
"files": [
"src",
"build"
],
"exports": "./build/index.js",
"scripts": {
"dev": "tsc --build --watch",
"build": "rm -rf build && tsc --build",
"lint": "eslint src"
},
"dependencies": {
"@polkadot-labs/hdkd": "^0.0.8",
"@reactive-dot/core": "workspace:^"
},
"devDependencies": {
"@reactive-dot/eslint-config": "workspace:^",
"@tsconfig/recommended": "^1.0.7",
"@tsconfig/strictest": "^2.0.5",
"eslint": "^9.13.0",
"typescript": "^5.6.3"
}
}
1 change: 1 addition & 0 deletions packages/wallet-development/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { DevelopmentWallet } from "./wallet.js";
83 changes: 83 additions & 0 deletions packages/wallet-development/src/wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { sr25519CreateDerive } from "@polkadot-labs/hdkd";
import {
DEV_PHRASE,
entropyToMiniSecret,
mnemonicToEntropy,
} from "@polkadot-labs/hdkd-helpers";
import {
type PolkadotSignerAccount,
Wallet,
type WalletOptions,
} from "@reactive-dot/core/wallets.js";
import { getPolkadotSigner } from "polkadot-api/signer";
import { BehaviorSubject } from "rxjs";

type DevelopmentWalletOptions = WalletOptions & {
accounts?: ReadonlyArray<{ mnemonic: string; path: string; name?: string }>;
includesWellknownDevAccounts?: boolean;
};

export class DevelopmentWallet extends Wallet<DevelopmentWalletOptions> {
id = "development";

name = "Development";

initialize() {
const accounts = [
...(this.options?.accounts ?? []),
...(!this.options?.includesWellknownDevAccounts
? []
: [
"Alice",
"Bob",
"Charlie",
"Dave",
"Eve",
"Ferdie",
"AliceStash",
"BobStash",
"CharlieStash",
"DaveStash",
"EveStash",
"FerdieStash",
].map((name) => ({ mnemonic: DEV_PHRASE, path: `//${name}`, name }))),
];

this.accounts$.next(
accounts.map(({ mnemonic, path, name }) => {
const entropy = mnemonicToEntropy(mnemonic);
const miniSecret = entropyToMiniSecret(entropy);
const derive = sr25519CreateDerive(miniSecret);
const hdkdKeyPair = derive(path);

const polkadotSigner = getPolkadotSigner(
hdkdKeyPair.publicKey,
"Sr25519",
hdkdKeyPair.sign,
);

return {
id: globalThis.crypto.randomUUID(),
polkadotSigner,
...(name === undefined ? undefined : { name }),
};
}),
);
}

connected$ = new BehaviorSubject(true);

connect() {
this.connected$.next(true);
}

disconnect() {
this.connected$.next(false);
}

accounts$ = new BehaviorSubject<PolkadotSignerAccount[]>([]);

getAccounts() {
return this.accounts$.value;
}
}
16 changes: 16 additions & 0 deletions packages/wallet-development/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": [
"@tsconfig/recommended/tsconfig.json",
"@tsconfig/strictest/tsconfig.json"
],
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "build"
},
"include": ["src"]
}
67 changes: 67 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4266,6 +4266,22 @@ __metadata:
languageName: node
linkType: hard

"@noble/curves@npm:^1.6.0":
version: 1.6.0
resolution: "@noble/curves@npm:1.6.0"
dependencies:
"@noble/hashes": "npm:1.5.0"
checksum: 10c0/f3262aa4d39148e627cd82b5ac1c93f88c5bb46dd2566b5e8e52ffac3a0fc381ad30c2111656fd2bd3b0d37d43d540543e0d93a5ff96a6cb184bc3bfe10d1cd9
languageName: node
linkType: hard

"@noble/hashes@npm:1.5.0, @noble/hashes@npm:^1.5.0":
version: 1.5.0
resolution: "@noble/hashes@npm:1.5.0"
checksum: 10c0/1b46539695fbfe4477c0822d90c881a04d4fa2921c08c552375b444a48cac9930cb1ee68de0a3c7859e676554d0f3771999716606dc4d8f826e414c11692cdd9
languageName: node
linkType: hard

"@noble/hashes@npm:^1.4.0":
version: 1.4.0
resolution: "@noble/hashes@npm:1.4.0"
Expand Down Expand Up @@ -4853,6 +4869,36 @@ __metadata:
languageName: node
linkType: hard

"@polkadot-labs/hdkd-helpers@npm:0.0.8":
version: 0.0.8
resolution: "@polkadot-labs/hdkd-helpers@npm:0.0.8"
dependencies:
"@noble/curves": "npm:^1.6.0"
"@noble/hashes": "npm:^1.5.0"
"@polkadot-labs/schnorrkel-wasm": "npm:0.0.5"
"@scure/base": "npm:^1.1.8"
scale-ts: "npm:^1.6.0"
checksum: 10c0/031c30b6bbaf8d5091e651efc836aa6952f3464762ea537a2318bf9737c70d8044a97f62c7cedb27e543528c9f10d1ce7fe0453646921f4ba0dab3116a2853b2
languageName: node
linkType: hard

"@polkadot-labs/hdkd@npm:^0.0.8":
version: 0.0.8
resolution: "@polkadot-labs/hdkd@npm:0.0.8"
dependencies:
"@polkadot-labs/hdkd-helpers": "npm:0.0.8"
"@polkadot-labs/schnorrkel-wasm": "npm:0.0.5"
checksum: 10c0/b38c684b73463aae4ea042b5bd044879a84e891d8cdba15ae0e05faa1bb16d78e1c0ed111ed4eea0b7cfefd675b465c4972b8a41b8d2dee03d275844c93eea13
languageName: node
linkType: hard

"@polkadot-labs/schnorrkel-wasm@npm:0.0.5":
version: 0.0.5
resolution: "@polkadot-labs/schnorrkel-wasm@npm:0.0.5"
checksum: 10c0/d0dcb873ad1078b6aab2a073eb13a6605561decba65fb6b9f65d0ad58549f7cc4ffafa7b5128da51a7e96a4f7b0e4468e7d30e9a1b23d15abe7402fccfb5be03
languageName: node
linkType: hard

"@reactive-dot/core@workspace:^, @reactive-dot/core@workspace:packages/core":
version: 0.0.0-use.local
resolution: "@reactive-dot/core@workspace:packages/core"
Expand Down Expand Up @@ -5001,6 +5047,20 @@ __metadata:
languageName: unknown
linkType: soft

"@reactive-dot/wallet-development@workspace:packages/wallet-development":
version: 0.0.0-use.local
resolution: "@reactive-dot/wallet-development@workspace:packages/wallet-development"
dependencies:
"@polkadot-labs/hdkd": "npm:^0.0.8"
"@reactive-dot/core": "workspace:^"
"@reactive-dot/eslint-config": "workspace:^"
"@tsconfig/recommended": "npm:^1.0.7"
"@tsconfig/strictest": "npm:^2.0.5"
eslint: "npm:^9.13.0"
typescript: "npm:^5.6.3"
languageName: unknown
linkType: soft

"@reactive-dot/wallet-ledger@workspace:^, @reactive-dot/wallet-ledger@workspace:packages/wallet-ledger":
version: 0.0.0-use.local
resolution: "@reactive-dot/wallet-ledger@workspace:packages/wallet-ledger"
Expand Down Expand Up @@ -5163,6 +5223,13 @@ __metadata:
languageName: node
linkType: hard

"@scure/base@npm:^1.1.8":
version: 1.1.9
resolution: "@scure/base@npm:1.1.9"
checksum: 10c0/77a06b9a2db8144d22d9bf198338893d77367c51b58c72b99df990c0a11f7cadd066d4102abb15e3ca6798d1529e3765f55c4355742465e49aed7a0c01fe76e8
languageName: node
linkType: hard

"@sec-ant/readable-stream@npm:^0.4.1":
version: 0.4.1
resolution: "@sec-ant/readable-stream@npm:0.4.1"
Expand Down