Skip to content

Commit

Permalink
Merge pull request #686 from o1-labs/mina-signer/14
Browse files Browse the repository at this point in the history
Publish the new mina-signer
  • Loading branch information
mitschabaude committed Jan 11, 2023
2 parents c397c40 + 2b0b038 commit f2b316a
Show file tree
Hide file tree
Showing 20 changed files with 185 additions and 21 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/build-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,29 @@ jobs:
token: ${{ secrets.NPM_TOKEN }}
env:
INPUT_TOKEN: ${{ secrets.NPM_TOKEN }}

Release-mina-signer-on-NPM:
timeout-minutes: 180
runs-on: ubuntu-latest
needs: [Build-And-Test-Server, Build-And-Test-Web]
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: '18'
- name: Build mina-signer
run: |
npm ci
cd src/mina-signer
npm ci
npm run prepublishOnly
- name: Publish to NPM if version has changed
uses: JS-DevTools/npm-publish@v1
if: github.ref == 'refs/heads/main'
with:
token: ${{ secrets.NPM_TOKEN }}
package: './src/mina-signer/package.json'
env:
INPUT_TOKEN: ${{ secrets.NPM_TOKEN }}
56 changes: 56 additions & 0 deletions src/mina-signer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Mina Signer

This is a NodeJS SDK that allows you to sign strings, payments, and delegations using Mina's key pairs for various specified networks.

# Install

```bash
yarn add mina-signer
# or with npm:
npm install --save mina-signer
```

# Usage

```js
import Client from 'mina-signer';
const client = new Client({ network: 'mainnet' });

// Generate keys
let keypair = client.genKeys();

// Sign and verify message
let signed = client.signMessage('hello', keypair);
if (client.verifyMessage(signed)) {
console.log('Message was verified successfully');
}

// Sign and verify a payment
let signedPayment = client.signPayment(
{
to: keypair.publicKey,
from: keypair.publicKey,
amount: 1,
fee: 1,
nonce: 0,
},
keypair.privateKey
);
if (client.verifyPayment(signedPayment)) {
console.log('Payment was verified successfully');
}

// Sign and verify a stake delegation
const signedDelegation = client.signStakeDelegation(
{
to: keypair.publicKey,
from: keypair.publicKey,
fee: '1',
nonce: '0',
},
keypair.privateKey
);
if (client.verifyStakeDelegation(signedDelegation)) {
console.log('Delegation was verified successfully');
}
```
7 changes: 7 additions & 0 deletions src/mina-signer/build-cjs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
set -e

cp index.cjs dist/node/mina-signer/index.cjs
cp index.d.ts dist/node/mina-signer/index.d.ts

npx esbuild --bundle --minify dist/node/mina-signer/index.cjs --outfile=./dist/node/mina-signer/index.cjs --format=cjs --target=es2021 --platform=node --allow-overwrite=true
7 changes: 7 additions & 0 deletions src/mina-signer/build-web.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
set -e

tsc -p ../../tsconfig.mina-signer-web.json
node moveWebFiles.js
npx esbuild --bundle --minify dist/tmp/mina-signer/MinaSigner.js --outfile=./dist/web/index.js --format=esm --target=es2021
npx rimraf dist/tmp
5 changes: 5 additions & 0 deletions src/mina-signer/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// this file is a wrapper for supporting commonjs imports

let Client = require('./MinaSigner.js');

module.exports = Client.default;
5 changes: 5 additions & 0 deletions src/mina-signer/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// this file is a wrapper for supporting types in both commonjs and esm projects

import Client from './MinaSigner.js';

export = Client;
10 changes: 10 additions & 0 deletions src/mina-signer/moveWebFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import glob from 'glob';
import { move } from 'fs-extra';

let webFiles = glob.sync('./dist/tmp/**/*.web.js');

await Promise.all(
webFiles.map((file) =>
move(file, file.replace('.web.js', '.js'), { overwrite: true })
)
);
27 changes: 27 additions & 0 deletions src/mina-signer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 20 additions & 6 deletions src/mina-signer/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"name": "mina-signer",
"description": "Node API for signing transactions on various networks for Mina Protocol",
"version": "1.3.0",
"version": "1.7.0",
"type": "module",
"scripts": {
"build": "tsc -p ../../tsconfig.mina-signer.json",
"test": "for f in ./**/*.test.ts; do NODE_OPTIONS=--experimental-vm-modules npx jest $f || exit 1; done"
"build:cjs": "./build-cjs.sh",
"build:web": "./build-web.sh",
"test": "for f in ./**/*.test.ts; do NODE_OPTIONS=--experimental-vm-modules npx jest $f || exit 1; done",
"prepublishOnly": "npx rimraf ./dist && npm run build && npm run build:cjs && npm run build:web"
},
"keywords": [
"mina",
Expand All @@ -17,9 +20,20 @@
"homepage": "https://minaprotocol.com/",
"repository": "https://github.com/MinaProtocol/mina",
"bugs": "https://github.com/MinaProtocol/mina/issues",
"main": "dist/src/MinaSigner.js",
"types": "dist/src/MinaSigner.d.ts",
"main": "dist/node/mina-signer/MinaSigner.js",
"types": "dist/node/mina-signer/index.d.ts",
"exports": {
"web": "./dist/web/index.js",
"require": "./dist/node/mina-signer/index.cjs",
"node": "./dist/node/mina-signer/MinaSigner.js",
"default": "./dist/web/index.js"
},
"files": [
"dist"
]
"dist",
"README.md"
],
"dependencies": {
"blakejs": "^1.2.1",
"js-sha256": "^0.9.0"
}
}
2 changes: 1 addition & 1 deletion src/mina-signer/src/rosetta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function fieldFromHex<T extends Field | Scalar>(
return [binable.fromBytes(bytes), paddingBit];
}

// TODO: clean up this logic, was copied over from Ocaml code
// TODO: clean up this logic, was copied over from OCaml code
function rosettaTransactionToSignedCommand({
signature,
payment,
Expand Down
2 changes: 1 addition & 1 deletion src/mina-signer/tests/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Client from '../dist/mina-signer/MinaSigner.js';
import Client from '../dist/node/mina-signer/MinaSigner.js';

describe('Client Class Initialization', () => {
let client;
Expand Down
2 changes: 1 addition & 1 deletion src/mina-signer/tests/keypair.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Client from '../dist/mina-signer/MinaSigner.js';
import Client from '../dist/node/mina-signer/MinaSigner.js';

describe('Keypair', () => {
let client: Client;
Expand Down
4 changes: 2 additions & 2 deletions src/mina-signer/tests/message.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Client from '../dist/mina-signer/MinaSigner.js';
import Client from '../dist/node/mina-signer/MinaSigner.js';
import type {
Keypair,
Signed,
Message,
} from '../dist/mina-signer/src/TSTypes.js';
} from '../dist/node/mina-signer/src/TSTypes.js';

describe('Message', () => {
describe('Mainnet network', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/mina-signer/tests/party.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ZkappCommand } from '../dist/provable/gen/transaction-bigint.js';
import * as TransactionJson from '../dist/provable/gen/transaction-json.js';
import Client from '../dist/mina-signer/MinaSigner.js';
import { ZkappCommand } from '../dist/node/provable/gen/transaction-bigint.js';
import * as TransactionJson from '../dist/node/provable/gen/transaction-json.js';
import Client from '../dist/node/mina-signer/MinaSigner.js';

let dummyFeePayer = ZkappCommand.toJSON(ZkappCommand.emptyValue()).feePayer;

Expand Down
4 changes: 2 additions & 2 deletions src/mina-signer/tests/payment.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Client from '../dist/mina-signer/MinaSigner.js';
import Client from '../dist/node/mina-signer/MinaSigner.js';
import type {
Keypair,
Signed,
Payment,
} from '../dist/mina-signer/src/TSTypes.js';
} from '../dist/node/mina-signer/src/TSTypes.js';

describe('Payment', () => {
describe('Mainnet network', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/mina-signer/tests/rosetta.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Client from '../dist/mina-signer/MinaSigner.js';
import Client from '../dist/node/mina-signer/MinaSigner.js';

describe('Rosetta', () => {
let client: Client;
Expand Down
4 changes: 2 additions & 2 deletions src/mina-signer/tests/stake-delegation.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Client from '../dist/mina-signer/MinaSigner.js';
import Client from '../dist/node/mina-signer/MinaSigner.js';
import type {
Keypair,
Signed,
StakeDelegation,
} from '../dist/mina-signer/src/TSTypes.js';
} from '../dist/node/mina-signer/src/TSTypes.js';

describe('Stake Delegation', () => {
describe('Mainnet network', () => {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"declaration": true, // declaration files are how library consumers get our types
"noEmitOnError": false, // avoid accidentally shipping with type errors
"allowJs": true, // to use JSDoc in some places where TS would be too cumbersome
"sourceMap": true,
"sourceMap": true
}
}
7 changes: 7 additions & 0 deletions tsconfig.mina-signer-web.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.mina-signer.json",
"include": ["./src/mina-signer/MinaSigner.ts", "./src/**/*.web.ts"],
"compilerOptions": {
"outDir": "src/mina-signer/dist/tmp"
}
}
2 changes: 1 addition & 1 deletion tsconfig.mina-signer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"exclude": ["./src/**/*.unit-test.ts"],
"compilerOptions": {
"rootDir": "./src",
"outDir": "src/mina-signer/dist",
"outDir": "src/mina-signer/dist/node",
"baseUrl": ".", // affects where output files end up
"target": "es2020", // goal: ship *the most modern syntax* that is supported by *all* browsers that support our Wasm
"module": "es2022", // allow top-level await
Expand Down

0 comments on commit f2b316a

Please sign in to comment.