diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 2c653fa17ee87f84544f3eba209c47ffba337bf5..63da409066233468cc63ac8aeef8264537e973d0 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -79,6 +79,7 @@ wax_wasm_proto_tsc_generation: when: always paths: - "${DIST_DIR}/*.tgz" # Built package + - "${SOURCE_DIR}/packages/*/dist" - "${REPLACE_FILE_PATH}" # Modified README - "${CI_PROJECT_DIR}/ts/wasm/lib/proto" # For documentation generator - "${CI_PROJECT_DIR}/ts/wasm/lib/build_wasm" # For documentation generator diff --git a/ts/packages/README.md b/ts/packages/README.md new file mode 100644 index 0000000000000000000000000000000000000000..9464f17021c6484756fc87a1da9745cd9813703f --- /dev/null +++ b/ts/packages/README.md @@ -0,0 +1,9 @@ +# Wax packages + +This Wax subdirectory is responsible for maintaining Wax "extensions", e.g. signers using the pnpm's workspaces strategy + +## Building + +1. Install dependencies using `pnpm install` - this will automatically install dependencies for all of the package directories from this directory. +2. Build all the packages using `pnpm run build` +3. Now you can either pack: `pnpm run pack` or publish: `pnpm publish -r`. Note: All of the packages will be saved under [`dist`](./dist) directory diff --git a/ts/packages/signers-keychain/.npmrc b/ts/packages/signers-keychain/.npmrc new file mode 120000 index 0000000000000000000000000000000000000000..a4ec98145691e88f368cc309edde67b5a9fa6035 --- /dev/null +++ b/ts/packages/signers-keychain/.npmrc @@ -0,0 +1 @@ +../.npmrc \ No newline at end of file diff --git a/ts/packages/signers-keychain/LICENSE.md b/ts/packages/signers-keychain/LICENSE.md new file mode 100644 index 0000000000000000000000000000000000000000..ec43082c9c9794ba0c1cb53df89a8852c857ed1f --- /dev/null +++ b/ts/packages/signers-keychain/LICENSE.md @@ -0,0 +1,7 @@ +Copyright (c) 2024 Hive community + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/ts/packages/signers-keychain/README.md b/ts/packages/signers-keychain/README.md new file mode 100644 index 0000000000000000000000000000000000000000..4d70a6a72c9ab80e38e818e952c32d420a005a57 --- /dev/null +++ b/ts/packages/signers-keychain/README.md @@ -0,0 +1,42 @@ +# @hiveio/wax-signers-keychain + +Wax signer library extending transaction signing possibilities by a 3rd party Web-only extension - Keychain + +## Prerequisites + +- Configured [@hiveio/wax](https://www.npmjs.com/package/@hiveio/wax) library. Wax Keychain signer is an extension the base Wax library, extending its signing possibilities by using 3rd party wallet +- Configured [Keychain browser extension](https://hive-keychain.com/) with imported keys + +## Example usage + +```ts +import { createHiveChain } from "@hiveio/wax"; +import KeychainProvider from "@hiveio/wax-signers-keychain"; + +const chain = await createHiveChain(); + +const provider = KeychainProvider.for("myaccount", "active"); + +// Create a transaction using the Wax Hive chain instance +const tx = await chain.createTransaction(); + +// Perform some operations, e.g. push the vote operation: +tx.pushOperation({ + vote: { + voter: "alice", + author: "bob", + permlink: "example-post", + weight: 10000 + } +}); + +// Wait for the keychain to sign the transaction +await tx.sign(provider); + +// broadcast the transaction +await chain.broadcast(tx); +``` + +## License + +See license in [LICENSE.md](LICENSE.md) file diff --git a/ts/packages/signers-keychain/package.json b/ts/packages/signers-keychain/package.json new file mode 100644 index 0000000000000000000000000000000000000000..847388532a0323915f83a0cbc36827d50e8fbfcd --- /dev/null +++ b/ts/packages/signers-keychain/package.json @@ -0,0 +1,42 @@ +{ + "name": "@hiveio/wax-signers-keychain", + "version": "0.0.0-Run-Prepack", + "description": "Wax signer library extending transaction signing possibilities by a 3rd party Web-only extension - Keychain", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "type": "module", + "private": false, + "scripts": { + "build": "tsc", + "prepack": "jq --argfile source ../../package.json '.version = $source.version | .publishConfig.registry = $source.publishConfig.registry | .publishConfig.tag = $source.publishConfig.tag' package.json > package.json.tmp && mv package.json.tmp package.json", + "pack": "pnpm pack --pack-destination ../dist", + "prepublish": "pnpm run prepack" + }, + "devDependencies": { + "typescript": "catalog:typescript-toolset" + }, + "dependencies": { + "@hiveio/wax": "workspace:../..", + "keychain-sdk": "^0.8.5" + }, + "files": [ + "dist/index.d.ts", + "dist/index.js", + "README.md", + "LICENSE.md" + ], + "license": "SEE LICENSE IN LICENSE.md", + "keywords": [ + "wax", + "blockchain", + "hive" + ], + "repository": { + "type": "git", + "url": "https://gitlab.syncad.com/hive/wax.git" + }, + "publishConfig": { + "registry": "https://RegistryPlaceholder", + "tag": "DistTagPlaceholder" + } +} \ No newline at end of file diff --git a/ts/packages/signers-keychain/src/index.ts b/ts/packages/signers-keychain/src/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..b997ac21bf5d997c1d458b0d8f39f0061cc63bf1 --- /dev/null +++ b/ts/packages/signers-keychain/src/index.ts @@ -0,0 +1,68 @@ +import type { IOnlineSignatureProvider, ITransaction, TAccountName, TRole } from "@hiveio/wax"; + +import { KeychainKeyTypes, KeychainSDK } from "keychain-sdk"; + +const mapRoles: Record<TRole, KeychainKeyTypes | undefined> = { + active: KeychainKeyTypes.active, + posting: KeychainKeyTypes.posting, + owner: undefined, + memo: KeychainKeyTypes.memo +}; + +// We do not extend from WaxError to avoid runtime dependencies, such as: /vite or /web - without it we can import only types +export class WaxKeychainProviderError extends Error {} + +/** + * Wax transaction signature provider using the Keychain SDK. + * + * @example + * ``` + * const provider = KeychainProvider.for("myaccount", "active"); + * + * // Create a transaction using the Wax Hive chain instance + * const tx = await chain.createTransaction(); + * + * // Perform some operations, e.g. pushing operations... + * + * // Sign the transaction + * await tx.sign(provider); + * + * // broadcast + * await chain.broadcast(tx); + * ``` + */ +class KeychainProvider implements IOnlineSignatureProvider { + private readonly role: KeychainKeyTypes; + + private static keychain: KeychainSDK; + + private constructor( + private readonly accountName: TAccountName, + role: TRole + ) { + if (!mapRoles[role]) + throw new Error(`Role ${role} is not supported by the Wax signature provider: ${KeychainProvider.name}`); + + if(!KeychainProvider.keychain) + KeychainProvider.keychain = new KeychainSDK(window); + + this.role = mapRoles[role]; + } + + public static for(accountName: TAccountName, role: TRole): KeychainProvider { + return new KeychainProvider(accountName, role); + } + + public async signTransaction(transaction: ITransaction): Promise<void> { + const data = await KeychainProvider.keychain.signTx({ + method: this.role, + username: this.accountName, + tx: JSON.parse(transaction.toLegacyApi()) + }); + + for(const sig of data.result.signatures) + transaction.sign(sig); + } +} + +export default KeychainProvider; diff --git a/ts/packages/signers-keychain/tsconfig.json b/ts/packages/signers-keychain/tsconfig.json new file mode 100644 index 0000000000000000000000000000000000000000..25ef87095e9b8b92046b3cc99f25155cc9e81fcc --- /dev/null +++ b/ts/packages/signers-keychain/tsconfig.json @@ -0,0 +1,14 @@ +{ + "extends": "../../npm-common-config/ts-common/tsconfig.base.json", + "compilerOptions": { + "rootDir": "./src", + "baseUrl": ".", + "outDir": "./dist", + "incremental": true, + "tsBuildInfoFile": "./dist/.tsbuildinfo", + "skipLibCheck": true + }, + "include": [ + "./src" + ] +} \ No newline at end of file diff --git a/ts/pnpm-lock.yaml b/ts/pnpm-lock.yaml index 392e10a266680b557b45c35bd57409da431956ab..15d134e036a5c1955c9414402bae225cfea3b333 100644 --- a/ts/pnpm-lock.yaml +++ b/ts/pnpm-lock.yaml @@ -194,6 +194,13 @@ importers: specifier: catalog:typescript-toolset version: 5.7.3 + packages: + + dependencies: + '@hiveio/wax-signers-keychain': + specifier: workspace:./signers-keychain + version: link:signers-keychain + packages: '@babel/code-frame@7.26.2':