Skip to content
Snippets Groups Projects
Commit de154a65 authored by Mateusz Tyszczak's avatar Mateusz Tyszczak :scroll: Committed by Bartek Wrona
Browse files

Add signers-keychain package

parent 09141025
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
# 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
../.npmrc
\ No newline at end of file
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.
# @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
{
"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
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;
{
"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
......@@ -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':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment