@mysten/sui.js
Advanced tools
Comparing version 0.0.0-experimental-20220906215147 to 0.0.0-experimental-20220906221008
import { Base64DataBuffer } from '../serialization/base64'; | ||
import { Keypair } from './keypair'; | ||
import { PublicKey } from './publickey'; | ||
import { Ed25519PublicKey } from './ed25519-publickey'; | ||
import { SignatureScheme } from './publickey'; | ||
/** | ||
@@ -17,14 +18,18 @@ * Ed25519 Keypair data | ||
/** | ||
* Create a new keypair instance. | ||
* Create a new Ed25519 keypair instance. | ||
* Generate random keypair if no {@link Ed25519Keypair} is provided. | ||
* | ||
* @param keypair ed25519 keypair | ||
* @param keypair Ed25519 keypair | ||
*/ | ||
constructor(keypair?: Ed25519KeypairData); | ||
/** | ||
* Generate a new random keypair | ||
* Get the key scheme of the keypair ED25519 | ||
*/ | ||
getKeyScheme(): SignatureScheme; | ||
/** | ||
* Generate a new random Ed25519 keypair | ||
*/ | ||
static generate(): Ed25519Keypair; | ||
/** | ||
* Create a keypair from a raw secret key byte array. | ||
* Create a Ed25519 keypair from a raw secret key byte array. | ||
* | ||
@@ -44,3 +49,3 @@ * This method should only be used to recreate a keypair from a previously | ||
/** | ||
* Generate a keypair from a 32 byte seed. | ||
* Generate a Ed25519 keypair from a 32 byte seed. | ||
* | ||
@@ -51,9 +56,9 @@ * @param seed seed byte array | ||
/** | ||
* The public key for this keypair | ||
* The public key for this Ed25519 keypair | ||
*/ | ||
getPublicKey(): PublicKey; | ||
getPublicKey(): Ed25519PublicKey; | ||
/** | ||
* Return the signature for the provided data. | ||
* Return the signature for the provided data using Ed25519. | ||
*/ | ||
signData(data: Base64DataBuffer): Base64DataBuffer; | ||
} |
import { Base64DataBuffer } from '../serialization/base64'; | ||
import { PublicKey } from './publickey'; | ||
import { PublicKey, SignatureScheme } from './publickey'; | ||
/** | ||
@@ -15,2 +15,6 @@ * A keypair used for signing transactions. | ||
signData(data: Base64DataBuffer): Base64DataBuffer; | ||
/** | ||
* Get the key scheme of the keypair: Secp256k1 or ED25519 | ||
*/ | ||
getKeyScheme(): SignatureScheme; | ||
} |
/// <reference types="node" /> | ||
import BN from 'bn.js'; | ||
import { Buffer } from 'buffer'; | ||
/** | ||
* Value to be converted into public key | ||
* Value to be converted into public key. | ||
*/ | ||
export declare type PublicKeyInitData = number | string | Buffer | Uint8Array | Array<number> | PublicKeyData; | ||
/** | ||
* JSON object representation of PublicKey class | ||
*/ | ||
* JSON object representation of PublicKey class. | ||
*/ | ||
export declare type PublicKeyData = { | ||
@@ -15,19 +14,18 @@ /** @internal */ | ||
}; | ||
export declare const PUBLIC_KEY_SIZE = 32; | ||
export declare const TYPE_BYTE = 0; | ||
/** | ||
* A keypair used for signing transactions. | ||
*/ | ||
export declare type SignatureScheme = 'ED25519' | 'Secp256k1'; | ||
export declare const SIGNATURE_SCHEME_TO_FLAG: { | ||
ED25519: number; | ||
Secp256k1: number; | ||
}; | ||
export declare function checkPublicKeyData(value: PublicKeyInitData): value is PublicKeyData; | ||
/** | ||
* A public key | ||
*/ | ||
export declare class PublicKey { | ||
/** @internal */ | ||
_bn: BN; | ||
export interface PublicKey { | ||
/** | ||
* Create a new PublicKey object | ||
* @param value ed25519 public key as buffer or base-64 encoded string | ||
* Checks if two public keys are equal | ||
*/ | ||
constructor(value: PublicKeyInitData); | ||
/** | ||
* Checks if two publicKeys are equal | ||
*/ | ||
equals(publicKey: PublicKey): boolean; | ||
@@ -53,3 +51,3 @@ /** | ||
*/ | ||
toSuiAddress(scheme?: SignatureScheme): string; | ||
toSuiAddress(): string; | ||
} |
export * from './cryptography/ed25519-keypair'; | ||
export * from './cryptography/secp256k1-keypair'; | ||
export * from './cryptography/keypair'; | ||
export * from './cryptography/ed25519-publickey'; | ||
export * from './cryptography/secp256k1-publickey'; | ||
export * from './cryptography/publickey'; | ||
@@ -4,0 +7,0 @@ export * from './providers/provider'; |
@@ -1,2 +0,2 @@ | ||
import { Ed25519Keypair } from '../cryptography/ed25519-keypair'; | ||
import { Keypair } from '../cryptography/keypair'; | ||
import { Provider } from '../providers/provider'; | ||
@@ -10,3 +10,3 @@ import { Base64DataBuffer } from '../serialization/base64'; | ||
private readonly keypair; | ||
constructor(keypair: Ed25519Keypair, provider?: Provider, serializer?: TxnDataSerializer); | ||
constructor(keypair: Keypair, provider?: Provider, serializer?: TxnDataSerializer); | ||
getAddress(): Promise<SuiAddress>; | ||
@@ -13,0 +13,0 @@ signData(data: Base64DataBuffer): Promise<SignaturePubkeyPair>; |
@@ -5,3 +5,3 @@ { | ||
"description": "Sui TypeScript API(Work in Progress)", | ||
"version": "0.0.0-experimental-20220906215147", | ||
"version": "0.0.0-experimental-20220906221008", | ||
"license": "Apache-2.0", | ||
@@ -73,3 +73,5 @@ "files": [ | ||
"dependencies": { | ||
"@mysten/bcs": "0.0.0-experimental-20220906215147", | ||
"@mysten/bcs": "0.0.0-experimental-20220906221008", | ||
"@noble/hashes": "^1.1.2", | ||
"@noble/secp256k1": "^1.6.3", | ||
"bn.js": "^5.2.0", | ||
@@ -76,0 +78,0 @@ "buffer": "^6.0.3", |
@@ -44,2 +44,9 @@ # Sui TypeScript SDK | ||
## Testing | ||
``` | ||
cd sdk/typescript | ||
pnpm run test | ||
``` | ||
## Usage | ||
@@ -80,4 +87,5 @@ | ||
import { Ed25519Keypair, JsonRpcProvider, RawSigner } from '@mysten/sui.js'; | ||
// Generate a new Keypair | ||
// Generate a new Ed25519 Keypair | ||
const keypair = new Ed25519Keypair(); | ||
const signer = new RawSigner( | ||
@@ -179,1 +187,15 @@ keypair, | ||
``` | ||
Alternatively, a Secp256k1 can be initiated: | ||
```typescript | ||
import { Secp256k1Keypair, JsonRpcProvider, RawSigner } from '@mysten/sui.js'; | ||
// Generate a new Secp256k1 Keypair | ||
const keypair = new Secp256k1Keypair(); | ||
const signer = new RawSigner( | ||
keypair, | ||
new JsonRpcProvider('https://gateway.devnet.sui.io:443') | ||
); | ||
``` |
@@ -7,3 +7,4 @@ // Copyright (c) 2022, Mysten Labs, Inc. | ||
import { Keypair } from './keypair'; | ||
import { PublicKey } from './publickey'; | ||
import { Ed25519PublicKey } from './ed25519-publickey'; | ||
import { SignatureScheme } from './publickey'; | ||
@@ -25,6 +26,6 @@ /** | ||
/** | ||
* Create a new keypair instance. | ||
* Create a new Ed25519 keypair instance. | ||
* Generate random keypair if no {@link Ed25519Keypair} is provided. | ||
* | ||
* @param keypair ed25519 keypair | ||
* @param keypair Ed25519 keypair | ||
*/ | ||
@@ -38,5 +39,12 @@ constructor(keypair?: Ed25519KeypairData) { | ||
} | ||
/** | ||
* Get the key scheme of the keypair ED25519 | ||
*/ | ||
getKeyScheme(): SignatureScheme { | ||
return 'ED25519'; | ||
} | ||
/** | ||
* Generate a new random keypair | ||
* Generate a new random Ed25519 keypair | ||
*/ | ||
@@ -48,3 +56,3 @@ static generate(): Ed25519Keypair { | ||
/** | ||
* Create a keypair from a raw secret key byte array. | ||
* Create a Ed25519 keypair from a raw secret key byte array. | ||
* | ||
@@ -77,3 +85,3 @@ * This method should only be used to recreate a keypair from a previously | ||
/** | ||
* Generate a keypair from a 32 byte seed. | ||
* Generate a Ed25519 keypair from a 32 byte seed. | ||
* | ||
@@ -87,10 +95,10 @@ * @param seed seed byte array | ||
/** | ||
* The public key for this keypair | ||
* The public key for this Ed25519 keypair | ||
*/ | ||
getPublicKey(): PublicKey { | ||
return new PublicKey(this.keypair.publicKey); | ||
getPublicKey(): Ed25519PublicKey { | ||
return new Ed25519PublicKey(this.keypair.publicKey); | ||
} | ||
/** | ||
* Return the signature for the provided data. | ||
* Return the signature for the provided data using Ed25519. | ||
*/ | ||
@@ -97,0 +105,0 @@ signData(data: Base64DataBuffer): Base64DataBuffer { |
@@ -5,3 +5,3 @@ // Copyright (c) 2022, Mysten Labs, Inc. | ||
import { Base64DataBuffer } from '../serialization/base64'; | ||
import { PublicKey } from './publickey'; | ||
import { PublicKey, SignatureScheme } from './publickey'; | ||
@@ -21,2 +21,7 @@ /** | ||
signData(data: Base64DataBuffer): Base64DataBuffer; | ||
/** | ||
* Get the key scheme of the keypair: Secp256k1 or ED25519 | ||
*/ | ||
getKeyScheme(): SignatureScheme; | ||
} |
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import BN from 'bn.js'; | ||
import { Buffer } from 'buffer'; | ||
import { sha3_256 } from 'js-sha3'; | ||
/** | ||
* Value to be converted into public key | ||
* Value to be converted into public key. | ||
*/ | ||
export type PublicKeyInitData = | ||
| number | ||
| string | ||
| Buffer | ||
| Uint8Array | ||
| Array<number> | ||
| PublicKeyData; | ||
export type PublicKeyInitData = | ||
| number | ||
| string | ||
| Buffer | ||
| Uint8Array | ||
| Array<number> | ||
| PublicKeyData; | ||
/** | ||
* JSON object representation of PublicKey class | ||
*/ | ||
* JSON object representation of PublicKey class. | ||
*/ | ||
export type PublicKeyData = { | ||
/** @internal */ | ||
_bn: BN; | ||
/** @internal */ | ||
_bn: BN; | ||
}; | ||
export const PUBLIC_KEY_SIZE = 32; | ||
export const TYPE_BYTE = 0x00; | ||
/** | ||
* A keypair used for signing transactions. | ||
*/ | ||
export type SignatureScheme = 'ED25519' | 'Secp256k1'; | ||
const SIGNATURE_SCHEME_TO_FLAG = { | ||
export const SIGNATURE_SCHEME_TO_FLAG = { | ||
ED25519: 0x00, | ||
@@ -37,3 +34,3 @@ Secp256k1: 0x01, | ||
function isPublicKeyData(value: PublicKeyInitData): value is PublicKeyData { | ||
export function checkPublicKeyData(value: PublicKeyInitData): value is PublicKeyData { | ||
return (value as PublicKeyData)._bn !== undefined; | ||
@@ -45,51 +42,17 @@ } | ||
*/ | ||
export class PublicKey { | ||
/** @internal */ | ||
_bn: BN; | ||
export interface PublicKey { | ||
/** | ||
* Create a new PublicKey object | ||
* @param value ed25519 public key as buffer or base-64 encoded string | ||
* Checks if two public keys are equal | ||
*/ | ||
constructor(value: PublicKeyInitData) { | ||
if (isPublicKeyData(value)) { | ||
this._bn = value._bn; | ||
} else { | ||
if (typeof value === 'string') { | ||
const buffer = Buffer.from(value, 'base64'); | ||
if (buffer.length !== 32) { | ||
throw new Error( | ||
`Invalid public key input. Expected 32 bytes, got ${buffer.length}` | ||
); | ||
} | ||
this._bn = new BN(buffer); | ||
} else { | ||
this._bn = new BN(value); | ||
} | ||
if (this._bn.byteLength() > PUBLIC_KEY_SIZE) { | ||
throw new Error(`Invalid public key input`); | ||
} | ||
} | ||
} | ||
equals(publicKey: PublicKey): boolean; | ||
/** | ||
* Checks if two publicKeys are equal | ||
*/ | ||
equals(publicKey: PublicKey): boolean { | ||
return this._bn.eq(publicKey._bn); | ||
} | ||
/** | ||
* Return the base-64 representation of the public key | ||
*/ | ||
toBase64(): string { | ||
return this.toBuffer().toString('base64'); | ||
} | ||
toBase64(): string; | ||
/** | ||
* Return the byte array representation of the public key | ||
*/ | ||
toBytes(): Uint8Array { | ||
return this.toBuffer(); | ||
} | ||
toBytes(): Uint8Array; | ||
@@ -99,19 +62,8 @@ /** | ||
*/ | ||
toBuffer(): Buffer { | ||
const b = this._bn.toArrayLike(Buffer); | ||
if (b.length === PUBLIC_KEY_SIZE) { | ||
return b; | ||
} | ||
const zeroPad = Buffer.alloc(PUBLIC_KEY_SIZE); | ||
b.copy(zeroPad, PUBLIC_KEY_SIZE - b.length); | ||
return zeroPad; | ||
} | ||
toBuffer(): Buffer; | ||
/** | ||
* Return the base-64 representation of the public key | ||
*/ | ||
toString(): string { | ||
return this.toBase64(); | ||
} | ||
toString(): string; | ||
@@ -121,20 +73,3 @@ /** | ||
*/ | ||
toSuiAddress(scheme: SignatureScheme = 'ED25519'): string { | ||
let tmp = new Uint8Array(PUBLIC_KEY_SIZE + 1); | ||
tmp.set([SIGNATURE_SCHEME_TO_FLAG[scheme]]); | ||
tmp.set(this.toBytes(), 1); | ||
const hexHash = sha3_256(tmp); | ||
const publicKeyBytes = new BN(hexHash, 16).toArray(undefined, 32); | ||
// Only take the first 20 bytes | ||
const addressBytes = publicKeyBytes.slice(0, 20); | ||
return toHexString(addressBytes); | ||
} | ||
toSuiAddress(): string | ||
} | ||
// https://stackoverflow.com/questions/34309988/byte-array-to-hex-string-conversion-in-javascript | ||
function toHexString(byteArray: number[]) { | ||
return byteArray.reduce( | ||
(output, elem) => output + ('0' + elem.toString(16)).slice(-2), | ||
'' | ||
); | ||
} |
@@ -5,3 +5,6 @@ // Copyright (c) 2022, Mysten Labs, Inc. | ||
export * from './cryptography/ed25519-keypair'; | ||
export * from './cryptography/secp256k1-keypair'; | ||
export * from './cryptography/keypair'; | ||
export * from './cryptography/ed25519-publickey'; | ||
export * from './cryptography/secp256k1-publickey'; | ||
export * from './cryptography/publickey'; | ||
@@ -8,0 +11,0 @@ |
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { Ed25519Keypair } from '../cryptography/ed25519-keypair'; | ||
import { Keypair } from '../cryptography/keypair'; | ||
import { Provider } from '../providers/provider'; | ||
@@ -13,6 +13,6 @@ import { Base64DataBuffer } from '../serialization/base64'; | ||
export class RawSigner extends SignerWithProvider { | ||
private readonly keypair: Ed25519Keypair; | ||
private readonly keypair: Keypair; | ||
constructor( | ||
keypair: Ed25519Keypair, | ||
keypair: Keypair, | ||
provider?: Provider, | ||
@@ -31,3 +31,3 @@ serializer?: TxnDataSerializer | ||
return { | ||
signatureScheme: 'ED25519', | ||
signatureScheme: this.keypair.getKeyScheme(), | ||
signature: this.keypair.signData(data), | ||
@@ -34,0 +34,0 @@ pubKey: this.keypair.getPublicKey(), |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
851591
71
10983
198
10
+ Added@noble/hashes@^1.1.2
+ Added@noble/secp256k1@^1.6.3
+ Added@mysten/bcs@0.0.0-experimental-20220906221008(transitive)
+ Added@noble/hashes@1.7.1(transitive)
+ Added@noble/secp256k1@1.7.1(transitive)
- Removed@mysten/bcs@0.0.0-experimental-20220906215147(transitive)