# starkware-crypto

> Starkware Crypto Library

Latest version **1.9.7** (published 2020-08-17) · Apache-2.0 license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install starkware-crypto
pnpm add starkware-crypto
yarn add starkware-crypto
bun add starkware-crypto
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.9.7 |
| Published | 2020-08-17 |
| First published | 2020-02-26 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 9 |
| Unpacked size | 1.4 MB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 17 |
| Author | Pedro Gomes |
| Maintainers | pedrouid |
| Keywords | stark, starkware, crypto, elliptic, curve, cryptography |

## Links

- npm: https://www.npmjs.com/package/starkware-crypto
- Repository: https://github.com/pedrouid/starkware-crypto
- Issues: https://github.com/pedrouid/starkware-crypto/issues
- npm.io page: https://npm.io/package/starkware-crypto

## Dependencies (9)

- [bip39](https://npm.io/package/bip39.md) ^3.0.2
- [bn.js](https://npm.io/package/bn.js.md) ^5.1.2
- [hash.js](https://npm.io/package/hash.js.md) ^1.1.7
- [js-sha3](https://npm.io/package/js-sha3.md) ^0.8.0
- [elliptic](https://npm.io/package/elliptic.md) ^6.5.3
- [enc-utils](https://npm.io/package/enc-utils.md) ^2.2.2
- [rsv-signature](https://npm.io/package/rsv-signature.md) ^1.1.0
- [starkware-types](https://npm.io/package/starkware-types.md) ^1.1.0
- [ethereumjs-wallet](https://npm.io/package/ethereumjs-wallet.md) ^1.0.0

## Alternatives

- [@gemini-wallet/core](https://npm.io/package/@gemini-wallet/core.md) — 515.6K weekly downloads
- [utility](https://npm.io/package/utility.md) — 416.6K weekly downloads
- [@primno/dpapi](https://npm.io/package/@primno/dpapi.md) — 7.2K weekly downloads
- [pi-readseek](https://npm.io/package/pi-readseek.md) — 3.7K weekly downloads
- [@emilia-protocol/verify](https://npm.io/package/@emilia-protocol/verify.md) — 1.1K weekly downloads

## Recent versions

- 1.9.7 (latest) — 2020-08-17
- 1.9.6 — 2020-08-17
- 1.9.5 — 2020-08-17
- 1.9.4 — 2020-08-13
- 1.9.3 — 2020-08-13
- 1.9.2 — 2020-08-03
- 1.9.1 — 2020-07-11
- 1.9.0 — 2020-07-11
- 1.8.0 — 2020-07-05
- 1.7.0 — 2020-07-01
- 1.6.0 — 2020-05-24
- 1.5.0 — 2020-04-15
- 1.4.0 — 2020-04-10
- 1.3.0 — 2020-04-06
- 1.2.0 — 2020-02-26
- … 2 more at https://npm.io/package/starkware-crypto/versions

## README

# starkware-crypto [![npm version](https://badge.fury.io/js/starkware-crypto.svg)](https://badge.fury.io/js/starkware-crypto)

Starkware Crypto Library

## Description

This library is a port from [starkex-resources/\*\*/signature.js](https://github.com/starkware-libs/starkex-resources/blob/master/crypto/starkware/crypto/signature/signature.js).

## Example

```typescript
import * as starkwareCrypto from 'starkware-crypto';

const mnemonic = '';

const path = getAccountPath(
  'starkex',
  'starkexdvf',
  '0x0000000000000000000000000000000000000000',
  '0'
);

const keyPair = starkwareCrypto.getKeyPairFromPath(mnemonic, path);

const publicKey = starkwareCrypto.getPublic(keyPair);

const starkPublicKey = starkwareCrypto.getStarkPublicKey(publicKey);

const msgParams = {
  amount: '2154549703648910716',
  nonce: '1',
  senderVaultId: '34',
  token: {
    type: 'ETH' as starkwareCrypto.TokenTypes,
    data: {
      quantum: '1',
      tokenAddress: '0x89b94e8C299235c00F97E6B0D7368E82d640E848',
    },
  },
  receiverVaultId: '21',
  receiverPublicKey:
    '0x5fa3383597691ea9d827a79e1a4f0f7949435ced18ca9619de8ab97e661020',
  expirationTimestamp: '438953',
};

const message = starkwareCrypto.getTransferMsg(
  msgParams.amount,
  msgParams.nonce,
  msgParams.senderVaultId,
  msgParams.token,
  msgParams.receiverVaultId,
  msgParams.receiverPublicKey,
  msgParams.expirationTimestamp
);

const signature = starkwareCrypto.sign(keyPair, message);

const verified = starkwareCrypto.verify(keyPair, message, signature);
```

## API

```typescript
interface StarkwareCrypto {
  getAccountPath(
    layer: string,
    application: string,
    ethereumAddress: string,
    index: string
  ): string;

  getKeyPairFromPath(mnemonic: string, path: string): KeyPair;

  getKeyPair(privateKey: string): KeyPair;

  getKeyPairFromPublicKey(publicKey: string): KeyPair;

  getPrivate(keyPair: KeyPair): string;

  getPublic(keyPair: KeyPair, compressed: bolean): string;

  getStarkPublicKey(keyPair: KeyPair): string;

  getXCoordinate(publicKey: string): string;

  getYCoordinate(publicKey: string): string;

  hashTokenId(token: Token);

  hashMessage(w1: string, w2: string, w3: string);

  deserializeMessage(serialized: string): MessageParams;

  serializeMessage(
    instructionTypeBn: BN,
    vault0Bn: BN,
    vault1Bn: BN,
    amount0Bn: BN,
    amount1Bn: BN,
    nonceBn: BN,
    expirationTimestampBn: BN
  ): string;

  formatMessage(
    instruction: 'transfer' | 'order',
    vault0: string,
    vault1: string,
    amount0: string,
    amount1: string,
    nonce: string,
    expirationTimestamp: string
  ): string;

  getLimitOrderMsg(
    vaultSell: string,
    vaultBuy: string,
    amountSell: string,
    amountBuy: string,
    tokenSell: Token,
    tokenBuy: Token,
    nonce: string,
    expirationTimestamp: string
  ): string;

  getTransferMsg(
    amount: string,
    nonce: string,
    senderVaultId: string,
    token: Token,
    receiverVaultId: string,
    receiverPublicKey: string,
    expirationTimestamp: string
  ): string;

  sign(keyPair: KeyPair, msg: string): Signature;

  verify(keyPair: KeyPair, msg: string, sig: SignatureInput): boolean;

  verifyStarkPublicKey(
    starkPublicKey: string,
    msg: string,
    sig: SignatureInput
  ): boolean;

  compress(publicKey: string): string;

  decompress(publicKey: string): string;

  exportRecoveryParam(recoveryParam: number | null): string;

  importRecoveryParam(v: string): number;

  serializeSignature(sig: Signature): string;

  deserializeSignature(sig: string): SignatureOptions;
}
```

## Typings

```typescript
type KeyPair = elliptic.ec.KeyPair;

type MessageParams = {
  instructionTypeBn: BN;
  vault0Bn: BN;
  vault1Bn: BN;
  amount0Bn: BN;
  amount1Bn: BN;
  nonceBn: BN;
  expirationTimestampBn: BN;
};

class Signature {
  r: BN;
  s: BN;
  recoveryParam: number | null;

  constructor(options: SignatureInput, enc?: string);

  toDER(enc?: string | null): any;
}

interface SignatureOptions {
  r: BNInput;
  s: BNInput;
  recoveryParam?: number;
}

type BNInput =
  | string
  | BN
  | number
  | Buffer
  | Uint8Array
  | ReadonlyArray<number>;

type SignatureInput =
  | Signature
  | SignatureOptions
  | Uint8Array
  | ReadonlyArray<number>
  | string;
```

## License

[Apache License 2.0](LICENSE.md)

---
_Source: https://npm.io/package/starkware-crypto · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
