Skip to content

Commit

Permalink
feat: 🎸 export polkadot utils
Browse files Browse the repository at this point in the history
  • Loading branch information
polymath-eric committed Oct 22, 2024
1 parent 889dee3 commit aee3aae
Show file tree
Hide file tree
Showing 7 changed files with 591 additions and 49 deletions.
50 changes: 6 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,12 @@
[![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg?style=flat-square)](https://github.com/standard/semistandard)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)

# Polymesh Typescript Template Repo
# Polymesh Signing Manager Types

This is a template repository for typescript projects. It includes some initial typescript config and tooling to make our lives easier
This defines the types needed to implement a [Polymesh SDK](https://github.com/PolymeshAssociation/polymesh-sdk) signing manager, along with exporting some commonly needed utilities for implementations

**NOTE**: This repo uses `yarn` instead of `npm` for dependencies
A signing manager abstracts the cryptographic signing of transactions so the SDK is indifferent to where and how private keys are stored.

Things included in the repo:

- Typescript (duh)
- Absolute imports (allows you to `import { foo } from ~/bar;` instead of `import { foo } from ../../../../bar;`. The default character is `~` but it can be changed in `tsconfig.json`)
- Eslint to enforce code style rules (extending standard JS with enforced semicolons and typescript-eslint)
- Prettier to format code on save
- Semantic release for automatic versioning
- Commitizen
- Husky to enforce conventional commits and format the code using prettier before committing
- Github actions for CI (runs linter, tests, build and semantic-release)

## Scripts

- `yarn build:ts` compiles typescript files into javascript and type declarations. Outputs to `dist/` directory
- `yarn build:docs` builds a documentation page from TSDoc comments in the code. Outputs to `docs/` directory
- `yarn test` runs tests and outputs the coverage report
- `yarn commit` runs the commit formatting tool (should replace normal commits)
- `yarn semantic-release` runs semantic release to calculate version numbers based on the nature of changes since the last version (used in CI pipelines)
- `yarn lint` runs the linter on all .ts(x) and .js(x) files and outputs all errors
- `yarn format` runs prettier on all .ts(x) and .js(x) files and formats them according to the project standards

## Notes

- All tools are configured via their respective config files instead of adding the config in `package.json`. There is also some vscode project config in `.vscode/settings.json`
- eslint: `.eslintrc`
- lint-staged: `.lintstagedrc`
- prettier: `.prettierrc`
- commitlint: `commitlint.config.js`
- husky: `.husky`
- jest: `jest.config.js`
- semantic-release: `release.config.js`
- typedoc: `typedoc.json`
- github actions: `.github/main.yml`
- The CI config assumes a `master` branch for stable releases and a `beta` branch for beta releases. Every time something gets pushed to either of those branches (or any time a pull request is opened to any branch), github actions will run. Semantic-release config makes it so that actual releases are only made on pushes to `master` or `beta`
- The CI config also adds an extra couple of steps to flatten the file structure that actually gets published. This means that your published package will have the built files at the root level instead of inside a `dist` folder. Those steps are:
- copy `package.json` into the `dist` folder after building
- `cd` into the `dist` folder
- install deps into the `dist` folder
- run `semantic-release` from there
- In order for automated NPM releases to actually work, you need to add an NPM auth token as a secret in your repo. To do that, go to your repo's `settings -> secrets -> add a new secret` input `NPM_TOKEN` as the secret name and the token you generated on your NPM account in the text area
- If you don't need automated NPM releases, you might want to uninstall `semantic-release` and tweak the github actions yaml file to skip the release step
Implementations:
- [Local](https://github.com/PolymeshAssociation/local-signing-manager) Private keys are typically encoded as 12 word mnemonics
- [Hashicorp Vault](https://github.com/PolymeshAssociation/hashicorp-vault-signing-manager) Private keys are stored in a [Vault transit engine](https://developer.hashicorp.com/vault/docs/secrets/transit)
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "typescript-repo-template",
"name": "@polymeshassociation/signing-manager-types",
"version": "0.0.0",
"description": "Template repository for typescript projects",
"description": "Types defining the Polymesh SDK signing manager interface",
"types": "dist/index.d.ts",
"author": "Polymesh Association",
"license": "ISC",
Expand All @@ -14,6 +14,9 @@
"lint": "eslint src --ext .js,.jsx,.ts,.tsx",
"format": "cross-env prettier-eslint $PWD\"/src/**/*.{ts,tsx,js,jsx,json,css,md}\" --write"
},
"dependencies": {
"@polkadot/api": "^11.2.1"
},
"devDependencies": {
"@commitlint/cli": "17.5.1",
"@commitlint/config-conventional": "17.4.4",
Expand Down
6 changes: 5 additions & 1 deletion release.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module.exports = {
repositoryUrl: 'https://github.com/PolymeshAssociation/typescript-boilerplate.git',
repositoryUrl: 'https://github.com/PolymeshAssociation/signing-manager-types',
branches: [
'master',
{
name: 'alpha',
prerelease: true,
},
{
name: 'beta',
prerelease: true,
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Happy Coding!
console.log('Polymesh Rocks!');
export * from './signing-manager';
export * from './utils';
23 changes: 23 additions & 0 deletions src/signing-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Signer } from '@polkadot/types/types';

/**
* `signPayload` and `signRaw` must be implemented. Only `update` is optional
*/
export type PolkadotSigner = Required<Omit<Signer, 'update'>> & Signer;

export interface SigningManager {
/**
* Fetch and return all managed Accounts
*/
getAccounts(): Promise<string[]>;

/**
* Return the external signer object that manages all signing logic
*/
getExternalSigner(): PolkadotSigner;

/**
* Set the format in which addresses returned by the Signing Manager are encoded
*/
setSs58Format(ss58Format: number): void;
}
18 changes: 18 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export { SignerPayloadJSON, SignerPayloadRaw, SignerResult } from '@polkadot/types/types';
export { TypeRegistry } from '@polkadot/types';
export { hexToU8a, u8aToHex } from '@polkadot/util';
export { blake2AsU8a, decodeAddress, encodeAddress } from '@polkadot/util-crypto';
export { HexString } from '@polkadot/util/types';

/**
* Expected chain signed extensions any internal `TypeRegistry` should use
*/
export const signedExtensions = [
'CheckSpecVersion',
'CheckTxVersion',
'CheckGenesis',
'CheckMortality',
'CheckNonce',
'CheckWeight',
'ChargeTransactionPayment',
];
Loading

0 comments on commit aee3aae

Please sign in to comment.