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

Add signers-peakvault package

parent de154a65
No related branches found
No related tags found
No related merge requests found
../.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-peakvault
Wax signer library extending transaction signing possibilities by a 3rd party Web-only extension - Peak Vault
## Prerequisites
- Configured [@hiveio/wax](https://www.npmjs.com/package/@hiveio/wax) library. Wax Peak Vault signer is an extension the base Wax library, extending its signing possibilities by using 3rd party wallet
- Configured [Peak Vault browser extension](https://vault.peakd.com/peakvault/releases.html) with imported keys
## Example usage
```ts
import { createHiveChain } from "@hiveio/wax";
import PeakVaultProvider from "@hiveio/wax-signers-peakvault";
const chain = await createHiveChain();
const provider = PeakVaultProvider.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-peakvault",
"version": "0.0.0-Run-Prepack",
"description": "Wax signer library extending transaction signing possibilities by a 3rd party Web-only extension - Peak Vault",
"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:../..",
"@peakd/hive-wallet-sdk": "^0.2.3"
},
"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 { getWallet } from '@peakd/hive-wallet-sdk'
// @peakd/hive-wallet-sdk fails to provide this type import
type KeyRole = Parameters<typeof PeakVaultProvider['peakVaultWallet']['signTx']>[2];
const mapRoles: Record<TRole, KeyRole | undefined> = {
active: 'active',
posting: 'posting',
owner: undefined,
memo: 'memo'
};
const PEAKVAULT_WALLET_ID: Parameters<typeof getWallet>[0] = 'peakvault';
// We do not extend from WaxError to avoid runtime dependencies, such as: /vite or /web - without it we can import only types
export class WaxPeakVaultProviderError extends Error {}
/**
* Wax transaction signature provider using the Peak Vault.
*
* @example
* ```
* const provider = PeakVaultProvider.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 PeakVaultProvider implements IOnlineSignatureProvider {
private readonly role: KeyRole;
private static peakVaultWallet: Awaited<ReturnType<typeof getWallet>>;
private constructor(
private readonly accountName: TAccountName,
role: TRole
) {
if (!mapRoles[role])
throw new Error(`Role ${role} is not supported by the Wax signature provider: ${PeakVaultProvider.name}`);
this.role = mapRoles[role];
}
public static for(accountName: TAccountName, role: TRole): PeakVaultProvider {
return new PeakVaultProvider(accountName, role);
}
public async signTransaction(transaction: ITransaction): Promise<void> {
if (!PeakVaultProvider.peakVaultWallet)
PeakVaultProvider.peakVaultWallet = await getWallet(PEAKVAULT_WALLET_ID);
const data = await PeakVaultProvider.peakVaultWallet.signTx(this.accountName, JSON.parse(transaction.toLegacyApi()), this.role);
for(const sig of data.result.signatures)
transaction.sign(sig);
}
}
export default PeakVaultProvider;
{
"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
This diff is collapsed.
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