Skip to content
Snippets Groups Projects
Verified Commit d6a6d5ba authored by Mateusz Tyszczak's avatar Mateusz Tyszczak :scroll:
Browse files

Add signers-keychain package

parent 1f7242b9
No related branches found
No related tags found
No related merge requests found
Pipeline #116668 failed
../npm-common-config/pnpm-config/.npmrc
\ No newline at end of file
# 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 run publish`. Note: All of the packages will be saved under [`dist`](./dist) directory
{
"scripts": {
"build": "pnpm run -r build",
"pack": "pnpm run -r pack",
"publish": "pnpm run -r publish"
},
"dependencies": {
"@hiveio/wax-signers-keychain": "workspace:./signers/keychain"
},
"pnpm": {
"requiredScripts": ["build", "pack", "publish"]
}
}
\ No newline at end of file
This diff is collapsed.
../npm-common-config/pnpm-config/pnpm-workspace.yaml
\ No newline at end of file
../../npm-common-config/pnpm-config/.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": "Run-Prepack-First",
"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",
"publish": "pnpm publish --access public --tag $(jq -r '.publishConfig.tag' ../../package.json)"
},
"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 { WaxError } from "@hiveio/wax";
import { KeychainKeyTypes, KeychainSDK } from "keychain-sdk";
const keychain = new KeychainSDK(window);
const mapRoles: Record<TRole, KeychainKeyTypes | undefined> = {
active: KeychainKeyTypes.active,
posting: KeychainKeyTypes.posting,
owner: undefined,
memo: KeychainKeyTypes.memo
};
export class WaxKeychainProviderError extends WaxError {}
/**
* 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 constructor(
private readonly accountName: TAccountName,
role: TRole
) {
if (!mapRoles[role])
throw new WaxKeychainProviderError(`Role ${role} is not supported by the Wax signature provider: ${KeychainProvider.name}`);
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 keychain.signTx({
method: mapRoles[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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment