@mysten/sui.js
Advanced tools
Comparing version 0.0.0-experimental-20240125183206 to 0.0.0-experimental-20240130153923
# @mysten/sui.js | ||
## 0.0.0-experimental-20240125183206 | ||
## 0.0.0-experimental-20240130153923 | ||
### Major Changes | ||
- a34f1cb67d: Use Bech32 instead of Hex for private key encoding for import and export | ||
### Patch Changes | ||
- a34f1cb67d: deprecate ExportedKeypair | ||
- 9a14e61db4: Allow signer in signAndExecuteTransactionBlock to be a Signer rather than a Keypair | ||
@@ -8,0 +13,0 @@ - 13e922d9b1: Fix multiple shared objects not respecting mutable correctly |
@@ -7,2 +7,8 @@ import { IntentScope } from './intent.js'; | ||
export declare const LEGACY_PRIVATE_KEY_SIZE = 64; | ||
export declare const SUI_PRIVATE_KEY_PREFIX = "suiprivkey"; | ||
export type ParsedKeypair = { | ||
schema: SignatureScheme; | ||
secretKey: Uint8Array; | ||
}; | ||
/** @deprecated use string instead. See {@link Keypair.getSecretKey} */ | ||
export type ExportedKeypair = { | ||
@@ -49,7 +55,26 @@ schema: SignatureScheme; | ||
} | ||
export declare abstract class Keypair extends BaseSigner { | ||
/** | ||
* This returns the Bech32 secret key string for this keypair. | ||
*/ | ||
abstract getSecretKey(): string; | ||
/** | ||
* @deprecated use {@link Keypair.getSecretKey} instead | ||
* This returns an exported keypair object, schema is the signature | ||
* scheme name, and the private key field is a Bech32 encoded string | ||
* of 33-byte `flag || private_key` that starts with `suiprivkey`. | ||
*/ | ||
export(): ExportedKeypair; | ||
} | ||
/** | ||
* TODO: Document | ||
* This returns an ParsedKeypair object based by validating the | ||
* 33-byte Bech32 encoded string starting with `suiprivkey`, and | ||
* parse out the signature scheme and the private key in bytes. | ||
*/ | ||
export declare abstract class Keypair extends BaseSigner { | ||
abstract export(): ExportedKeypair; | ||
} | ||
export declare function decodeSuiPrivateKey(value: string): ParsedKeypair; | ||
/** | ||
* This returns a Bech32 encoded string starting with `suiprivkey`, | ||
* encoding 33-byte `flag || bytes` for the given the 32-byte private | ||
* key and its signature scheme. | ||
*/ | ||
export declare function encodeSuiPrivateKey(bytes: Uint8Array, scheme: SignatureScheme): string; |
@@ -24,3 +24,6 @@ "use strict"; | ||
LEGACY_PRIVATE_KEY_SIZE: () => LEGACY_PRIVATE_KEY_SIZE, | ||
PRIVATE_KEY_SIZE: () => PRIVATE_KEY_SIZE | ||
PRIVATE_KEY_SIZE: () => PRIVATE_KEY_SIZE, | ||
SUI_PRIVATE_KEY_PREFIX: () => SUI_PRIVATE_KEY_PREFIX, | ||
decodeSuiPrivateKey: () => decodeSuiPrivateKey, | ||
encodeSuiPrivateKey: () => encodeSuiPrivateKey | ||
}); | ||
@@ -30,7 +33,9 @@ module.exports = __toCommonJS(keypair_exports); | ||
var import_blake2b = require("@noble/hashes/blake2b"); | ||
var import_bcs2 = require("../bcs/index.js"); | ||
var import_bech32 = require("bech32"); | ||
var import_intent = require("./intent.js"); | ||
var import_signature_scheme = require("./signature-scheme.js"); | ||
var import_signature = require("./signature.js"); | ||
const PRIVATE_KEY_SIZE = 32; | ||
const LEGACY_PRIVATE_KEY_SIZE = 64; | ||
const SUI_PRIVATE_KEY_PREFIX = "suiprivkey"; | ||
class BaseSigner { | ||
@@ -65,3 +70,3 @@ /** | ||
return this.signWithIntent( | ||
import_bcs2.bcs.vector(import_bcs2.bcs.u8()).serialize(bytes).toBytes(), | ||
import_bcs.bcs.vector(import_bcs.bcs.u8()).serialize(bytes).toBytes(), | ||
import_intent.IntentScope.PersonalMessage | ||
@@ -75,3 +80,38 @@ ); | ||
class Keypair extends BaseSigner { | ||
/** | ||
* @deprecated use {@link Keypair.getSecretKey} instead | ||
* This returns an exported keypair object, schema is the signature | ||
* scheme name, and the private key field is a Bech32 encoded string | ||
* of 33-byte `flag || private_key` that starts with `suiprivkey`. | ||
*/ | ||
export() { | ||
return { | ||
schema: this.getKeyScheme(), | ||
privateKey: this.getSecretKey() | ||
}; | ||
} | ||
} | ||
function decodeSuiPrivateKey(value) { | ||
const { prefix, words } = import_bech32.bech32.decode(value); | ||
if (prefix !== SUI_PRIVATE_KEY_PREFIX) { | ||
throw new Error("invalid private key prefix"); | ||
} | ||
const extendedSecretKey = new Uint8Array(import_bech32.bech32.fromWords(words)); | ||
const secretKey = extendedSecretKey.slice(1); | ||
const signatureScheme = import_signature_scheme.SIGNATURE_FLAG_TO_SCHEME[extendedSecretKey[0]]; | ||
return { | ||
schema: signatureScheme, | ||
secretKey | ||
}; | ||
} | ||
function encodeSuiPrivateKey(bytes, scheme) { | ||
if (bytes.length !== PRIVATE_KEY_SIZE) { | ||
throw new Error("Invalid bytes length"); | ||
} | ||
const flag = import_signature_scheme.SIGNATURE_SCHEME_TO_FLAG[scheme]; | ||
const privKeyBytes = new Uint8Array(bytes.length + 1); | ||
privKeyBytes.set([flag]); | ||
privKeyBytes.set(bytes, 1); | ||
return import_bech32.bech32.encode(SUI_PRIVATE_KEY_PREFIX, import_bech32.bech32.toWords(privKeyBytes)); | ||
} | ||
//# sourceMappingURL=keypair.js.map |
@@ -1,2 +0,1 @@ | ||
import type { ExportedKeypair } from '../../cryptography/keypair.js'; | ||
import { Keypair } from '../../cryptography/keypair.js'; | ||
@@ -40,14 +39,2 @@ import type { SignatureScheme } from '../../cryptography/signature-scheme.js'; | ||
* | ||
* The sui.keystore key is a list of Base64 encoded `flag || privkey`. To import | ||
* a key from sui.keystore to typescript, decode from base64 and remove the first | ||
* flag byte after checking it is indeed the Ed25519 scheme flag 0x00 (See more | ||
* on flag for signature scheme: https://github.com/MystenLabs/sui/blob/818406c5abdf7de1b80915a0519071eec3a5b1c7/crates/sui-types/src/crypto.rs#L1650): | ||
* ``` | ||
* import { Ed25519Keypair, fromB64 } from '@mysten/sui.js'; | ||
* const raw = fromB64(t[1]); | ||
* if (raw[0] !== 0 || raw.length !== PRIVATE_KEY_SIZE + 1) { | ||
* throw new Error('invalid key'); | ||
* } | ||
* const imported = Ed25519Keypair.fromSecretKey(raw.slice(1)) | ||
* ``` | ||
* @throws error if the provided secret key is invalid and validation is not skipped. | ||
@@ -65,2 +52,6 @@ * | ||
getPublicKey(): Ed25519PublicKey; | ||
/** | ||
* The Bech32 secret key string for this Ed25519 keypair | ||
*/ | ||
getSecretKey(): string; | ||
sign(data: Uint8Array): Promise<Uint8Array>; | ||
@@ -86,6 +77,2 @@ /** | ||
static deriveKeypairFromSeed(seedHex: string, path?: string): Ed25519Keypair; | ||
/** | ||
* This returns an exported keypair object, the private key field is the pure 32-byte seed. | ||
*/ | ||
export(): ExportedKeypair; | ||
} |
@@ -35,3 +35,2 @@ "use strict"; | ||
module.exports = __toCommonJS(keypair_exports); | ||
var import_bcs = require("@mysten/bcs"); | ||
var import_tweetnacl = __toESM(require("tweetnacl")); | ||
@@ -75,14 +74,2 @@ var import_keypair = require("../../cryptography/keypair.js"); | ||
* | ||
* The sui.keystore key is a list of Base64 encoded `flag || privkey`. To import | ||
* a key from sui.keystore to typescript, decode from base64 and remove the first | ||
* flag byte after checking it is indeed the Ed25519 scheme flag 0x00 (See more | ||
* on flag for signature scheme: https://github.com/MystenLabs/sui/blob/818406c5abdf7de1b80915a0519071eec3a5b1c7/crates/sui-types/src/crypto.rs#L1650): | ||
* ``` | ||
* import { Ed25519Keypair, fromB64 } from '@mysten/sui.js'; | ||
* const raw = fromB64(t[1]); | ||
* if (raw[0] !== 0 || raw.length !== PRIVATE_KEY_SIZE + 1) { | ||
* throw new Error('invalid key'); | ||
* } | ||
* const imported = Ed25519Keypair.fromSecretKey(raw.slice(1)) | ||
* ``` | ||
* @throws error if the provided secret key is invalid and validation is not skipped. | ||
@@ -117,2 +104,11 @@ * | ||
} | ||
/** | ||
* The Bech32 secret key string for this Ed25519 keypair | ||
*/ | ||
getSecretKey() { | ||
return (0, import_keypair.encodeSuiPrivateKey)( | ||
this.keypair.secretKey.slice(0, import_keypair.PRIVATE_KEY_SIZE), | ||
this.getKeyScheme() | ||
); | ||
} | ||
async sign(data) { | ||
@@ -160,12 +156,3 @@ return this.signData(data); | ||
} | ||
/** | ||
* This returns an exported keypair object, the private key field is the pure 32-byte seed. | ||
*/ | ||
export() { | ||
return { | ||
schema: "ED25519", | ||
privateKey: (0, import_bcs.toB64)(this.keypair.secretKey.slice(0, import_keypair.PRIVATE_KEY_SIZE)) | ||
}; | ||
} | ||
} | ||
//# sourceMappingURL=keypair.js.map |
@@ -1,2 +0,1 @@ | ||
import type { ExportedKeypair } from '../../cryptography/keypair.js'; | ||
import { Keypair } from '../../cryptography/keypair.js'; | ||
@@ -58,2 +57,6 @@ import type { PublicKey } from '../../cryptography/publickey.js'; | ||
getPublicKey(): PublicKey; | ||
/** | ||
* The Bech32 secret key string for this Secp256k1 keypair | ||
*/ | ||
getSecretKey(): string; | ||
sign(data: Uint8Array): Promise<Uint8Array>; | ||
@@ -72,3 +75,2 @@ /** | ||
static deriveKeypair(mnemonics: string, path?: string): Secp256k1Keypair; | ||
export(): ExportedKeypair; | ||
} |
@@ -25,3 +25,2 @@ "use strict"; | ||
module.exports = __toCommonJS(keypair_exports); | ||
var import_bcs = require("@mysten/bcs"); | ||
var import_secp256k1 = require("@noble/curves/secp256k1"); | ||
@@ -105,2 +104,8 @@ var import_blake2b = require("@noble/hashes/blake2b"); | ||
} | ||
/** | ||
* The Bech32 secret key string for this Secp256k1 keypair | ||
*/ | ||
getSecretKey() { | ||
return (0, import_keypair.encodeSuiPrivateKey)(this.keypair.secretKey, this.getKeyScheme()); | ||
} | ||
async sign(data) { | ||
@@ -142,9 +147,3 @@ return this.signData(data); | ||
} | ||
export() { | ||
return { | ||
schema: "Secp256k1", | ||
privateKey: (0, import_bcs.toB64)(this.keypair.secretKey) | ||
}; | ||
} | ||
} | ||
//# sourceMappingURL=keypair.js.map |
@@ -1,2 +0,1 @@ | ||
import type { ExportedKeypair } from '../../cryptography/keypair.js'; | ||
import { Keypair } from '../../cryptography/keypair.js'; | ||
@@ -58,2 +57,6 @@ import type { PublicKey } from '../../cryptography/publickey.js'; | ||
getPublicKey(): PublicKey; | ||
/** | ||
* The Bech32 secret key string for this Secp256r1 keypair | ||
*/ | ||
getSecretKey(): string; | ||
sign(data: Uint8Array): Promise<Uint8Array>; | ||
@@ -72,3 +75,2 @@ /** | ||
static deriveKeypair(mnemonics: string, path?: string): Secp256r1Keypair; | ||
export(): ExportedKeypair; | ||
} |
@@ -25,3 +25,2 @@ "use strict"; | ||
module.exports = __toCommonJS(keypair_exports); | ||
var import_bcs = require("@mysten/bcs"); | ||
var import_p256 = require("@noble/curves/p256"); | ||
@@ -105,2 +104,8 @@ var import_blake2b = require("@noble/hashes/blake2b"); | ||
} | ||
/** | ||
* The Bech32 secret key string for this Secp256r1 keypair | ||
*/ | ||
getSecretKey() { | ||
return (0, import_keypair.encodeSuiPrivateKey)(this.keypair.secretKey, this.getKeyScheme()); | ||
} | ||
async sign(data) { | ||
@@ -136,9 +141,3 @@ return this.signData(data); | ||
} | ||
export() { | ||
return { | ||
schema: "Secp256r1", | ||
privateKey: (0, import_bcs.toB64)(this.keypair.secretKey) | ||
}; | ||
} | ||
} | ||
//# sourceMappingURL=keypair.js.map |
@@ -1,2 +0,2 @@ | ||
export declare const PACKAGE_VERSION = "0.0.0-experimental-20240125183206"; | ||
export declare const PACKAGE_VERSION = "0.0.0-experimental-20240130153923"; | ||
export declare const TARGETED_RPC_VERSION = "1.18.0"; |
@@ -25,4 +25,4 @@ "use strict"; | ||
module.exports = __toCommonJS(version_exports); | ||
const PACKAGE_VERSION = "0.0.0-experimental-20240125183206"; | ||
const PACKAGE_VERSION = "0.0.0-experimental-20240130153923"; | ||
const TARGETED_RPC_VERSION = "1.18.0"; | ||
//# sourceMappingURL=version.js.map |
@@ -7,2 +7,8 @@ import { IntentScope } from './intent.js'; | ||
export declare const LEGACY_PRIVATE_KEY_SIZE = 64; | ||
export declare const SUI_PRIVATE_KEY_PREFIX = "suiprivkey"; | ||
export type ParsedKeypair = { | ||
schema: SignatureScheme; | ||
secretKey: Uint8Array; | ||
}; | ||
/** @deprecated use string instead. See {@link Keypair.getSecretKey} */ | ||
export type ExportedKeypair = { | ||
@@ -49,7 +55,26 @@ schema: SignatureScheme; | ||
} | ||
export declare abstract class Keypair extends BaseSigner { | ||
/** | ||
* This returns the Bech32 secret key string for this keypair. | ||
*/ | ||
abstract getSecretKey(): string; | ||
/** | ||
* @deprecated use {@link Keypair.getSecretKey} instead | ||
* This returns an exported keypair object, schema is the signature | ||
* scheme name, and the private key field is a Bech32 encoded string | ||
* of 33-byte `flag || private_key` that starts with `suiprivkey`. | ||
*/ | ||
export(): ExportedKeypair; | ||
} | ||
/** | ||
* TODO: Document | ||
* This returns an ParsedKeypair object based by validating the | ||
* 33-byte Bech32 encoded string starting with `suiprivkey`, and | ||
* parse out the signature scheme and the private key in bytes. | ||
*/ | ||
export declare abstract class Keypair extends BaseSigner { | ||
abstract export(): ExportedKeypair; | ||
} | ||
export declare function decodeSuiPrivateKey(value: string): ParsedKeypair; | ||
/** | ||
* This returns a Bech32 encoded string starting with `suiprivkey`, | ||
* encoding 33-byte `flag || bytes` for the given the 32-byte private | ||
* key and its signature scheme. | ||
*/ | ||
export declare function encodeSuiPrivateKey(bytes: Uint8Array, scheme: SignatureScheme): string; |
@@ -1,8 +0,10 @@ | ||
import { toB64 } from "@mysten/bcs"; | ||
import { bcs, toB64 } from "@mysten/bcs"; | ||
import { blake2b } from "@noble/hashes/blake2b"; | ||
import { bcs } from "../bcs/index.js"; | ||
import { bech32 } from "bech32"; | ||
import { IntentScope, messageWithIntent } from "./intent.js"; | ||
import { SIGNATURE_FLAG_TO_SCHEME, SIGNATURE_SCHEME_TO_FLAG } from "./signature-scheme.js"; | ||
import { toSerializedSignature } from "./signature.js"; | ||
const PRIVATE_KEY_SIZE = 32; | ||
const LEGACY_PRIVATE_KEY_SIZE = 64; | ||
const SUI_PRIVATE_KEY_PREFIX = "suiprivkey"; | ||
class BaseSigner { | ||
@@ -46,3 +48,38 @@ /** | ||
class Keypair extends BaseSigner { | ||
/** | ||
* @deprecated use {@link Keypair.getSecretKey} instead | ||
* This returns an exported keypair object, schema is the signature | ||
* scheme name, and the private key field is a Bech32 encoded string | ||
* of 33-byte `flag || private_key` that starts with `suiprivkey`. | ||
*/ | ||
export() { | ||
return { | ||
schema: this.getKeyScheme(), | ||
privateKey: this.getSecretKey() | ||
}; | ||
} | ||
} | ||
function decodeSuiPrivateKey(value) { | ||
const { prefix, words } = bech32.decode(value); | ||
if (prefix !== SUI_PRIVATE_KEY_PREFIX) { | ||
throw new Error("invalid private key prefix"); | ||
} | ||
const extendedSecretKey = new Uint8Array(bech32.fromWords(words)); | ||
const secretKey = extendedSecretKey.slice(1); | ||
const signatureScheme = SIGNATURE_FLAG_TO_SCHEME[extendedSecretKey[0]]; | ||
return { | ||
schema: signatureScheme, | ||
secretKey | ||
}; | ||
} | ||
function encodeSuiPrivateKey(bytes, scheme) { | ||
if (bytes.length !== PRIVATE_KEY_SIZE) { | ||
throw new Error("Invalid bytes length"); | ||
} | ||
const flag = SIGNATURE_SCHEME_TO_FLAG[scheme]; | ||
const privKeyBytes = new Uint8Array(bytes.length + 1); | ||
privKeyBytes.set([flag]); | ||
privKeyBytes.set(bytes, 1); | ||
return bech32.encode(SUI_PRIVATE_KEY_PREFIX, bech32.toWords(privKeyBytes)); | ||
} | ||
export { | ||
@@ -52,4 +89,7 @@ BaseSigner, | ||
LEGACY_PRIVATE_KEY_SIZE, | ||
PRIVATE_KEY_SIZE | ||
PRIVATE_KEY_SIZE, | ||
SUI_PRIVATE_KEY_PREFIX, | ||
decodeSuiPrivateKey, | ||
encodeSuiPrivateKey | ||
}; | ||
//# sourceMappingURL=keypair.js.map |
@@ -1,2 +0,1 @@ | ||
import type { ExportedKeypair } from '../../cryptography/keypair.js'; | ||
import { Keypair } from '../../cryptography/keypair.js'; | ||
@@ -40,14 +39,2 @@ import type { SignatureScheme } from '../../cryptography/signature-scheme.js'; | ||
* | ||
* The sui.keystore key is a list of Base64 encoded `flag || privkey`. To import | ||
* a key from sui.keystore to typescript, decode from base64 and remove the first | ||
* flag byte after checking it is indeed the Ed25519 scheme flag 0x00 (See more | ||
* on flag for signature scheme: https://github.com/MystenLabs/sui/blob/818406c5abdf7de1b80915a0519071eec3a5b1c7/crates/sui-types/src/crypto.rs#L1650): | ||
* ``` | ||
* import { Ed25519Keypair, fromB64 } from '@mysten/sui.js'; | ||
* const raw = fromB64(t[1]); | ||
* if (raw[0] !== 0 || raw.length !== PRIVATE_KEY_SIZE + 1) { | ||
* throw new Error('invalid key'); | ||
* } | ||
* const imported = Ed25519Keypair.fromSecretKey(raw.slice(1)) | ||
* ``` | ||
* @throws error if the provided secret key is invalid and validation is not skipped. | ||
@@ -65,2 +52,6 @@ * | ||
getPublicKey(): Ed25519PublicKey; | ||
/** | ||
* The Bech32 secret key string for this Ed25519 keypair | ||
*/ | ||
getSecretKey(): string; | ||
sign(data: Uint8Array): Promise<Uint8Array>; | ||
@@ -86,6 +77,2 @@ /** | ||
static deriveKeypairFromSeed(seedHex: string, path?: string): Ed25519Keypair; | ||
/** | ||
* This returns an exported keypair object, the private key field is the pure 32-byte seed. | ||
*/ | ||
export(): ExportedKeypair; | ||
} |
@@ -1,4 +0,3 @@ | ||
import { toB64 } from "@mysten/bcs"; | ||
import nacl from "tweetnacl"; | ||
import { Keypair, PRIVATE_KEY_SIZE } from "../../cryptography/keypair.js"; | ||
import { encodeSuiPrivateKey, Keypair, PRIVATE_KEY_SIZE } from "../../cryptography/keypair.js"; | ||
import { isValidHardenedPath, mnemonicToSeedHex } from "../../cryptography/mnemonics.js"; | ||
@@ -40,14 +39,2 @@ import { derivePath } from "./ed25519-hd-key.js"; | ||
* | ||
* The sui.keystore key is a list of Base64 encoded `flag || privkey`. To import | ||
* a key from sui.keystore to typescript, decode from base64 and remove the first | ||
* flag byte after checking it is indeed the Ed25519 scheme flag 0x00 (See more | ||
* on flag for signature scheme: https://github.com/MystenLabs/sui/blob/818406c5abdf7de1b80915a0519071eec3a5b1c7/crates/sui-types/src/crypto.rs#L1650): | ||
* ``` | ||
* import { Ed25519Keypair, fromB64 } from '@mysten/sui.js'; | ||
* const raw = fromB64(t[1]); | ||
* if (raw[0] !== 0 || raw.length !== PRIVATE_KEY_SIZE + 1) { | ||
* throw new Error('invalid key'); | ||
* } | ||
* const imported = Ed25519Keypair.fromSecretKey(raw.slice(1)) | ||
* ``` | ||
* @throws error if the provided secret key is invalid and validation is not skipped. | ||
@@ -82,2 +69,11 @@ * | ||
} | ||
/** | ||
* The Bech32 secret key string for this Ed25519 keypair | ||
*/ | ||
getSecretKey() { | ||
return encodeSuiPrivateKey( | ||
this.keypair.secretKey.slice(0, PRIVATE_KEY_SIZE), | ||
this.getKeyScheme() | ||
); | ||
} | ||
async sign(data) { | ||
@@ -125,11 +121,2 @@ return this.signData(data); | ||
} | ||
/** | ||
* This returns an exported keypair object, the private key field is the pure 32-byte seed. | ||
*/ | ||
export() { | ||
return { | ||
schema: "ED25519", | ||
privateKey: toB64(this.keypair.secretKey.slice(0, PRIVATE_KEY_SIZE)) | ||
}; | ||
} | ||
} | ||
@@ -136,0 +123,0 @@ export { |
@@ -1,2 +0,1 @@ | ||
import type { ExportedKeypair } from '../../cryptography/keypair.js'; | ||
import { Keypair } from '../../cryptography/keypair.js'; | ||
@@ -58,2 +57,6 @@ import type { PublicKey } from '../../cryptography/publickey.js'; | ||
getPublicKey(): PublicKey; | ||
/** | ||
* The Bech32 secret key string for this Secp256k1 keypair | ||
*/ | ||
getSecretKey(): string; | ||
sign(data: Uint8Array): Promise<Uint8Array>; | ||
@@ -72,3 +75,2 @@ /** | ||
static deriveKeypair(mnemonics: string, path?: string): Secp256k1Keypair; | ||
export(): ExportedKeypair; | ||
} |
@@ -1,2 +0,1 @@ | ||
import { toB64 } from "@mysten/bcs"; | ||
import { secp256k1 } from "@noble/curves/secp256k1"; | ||
@@ -7,3 +6,3 @@ import { blake2b } from "@noble/hashes/blake2b"; | ||
import { HDKey } from "@scure/bip32"; | ||
import { Keypair } from "../../cryptography/keypair.js"; | ||
import { encodeSuiPrivateKey, Keypair } from "../../cryptography/keypair.js"; | ||
import { isValidBIP32Path, mnemonicToSeed } from "../../cryptography/mnemonics.js"; | ||
@@ -81,2 +80,8 @@ import { Secp256k1PublicKey } from "./publickey.js"; | ||
} | ||
/** | ||
* The Bech32 secret key string for this Secp256k1 keypair | ||
*/ | ||
getSecretKey() { | ||
return encodeSuiPrivateKey(this.keypair.secretKey, this.getKeyScheme()); | ||
} | ||
async sign(data) { | ||
@@ -118,8 +123,2 @@ return this.signData(data); | ||
} | ||
export() { | ||
return { | ||
schema: "Secp256k1", | ||
privateKey: toB64(this.keypair.secretKey) | ||
}; | ||
} | ||
} | ||
@@ -126,0 +125,0 @@ export { |
@@ -1,2 +0,1 @@ | ||
import type { ExportedKeypair } from '../../cryptography/keypair.js'; | ||
import { Keypair } from '../../cryptography/keypair.js'; | ||
@@ -58,2 +57,6 @@ import type { PublicKey } from '../../cryptography/publickey.js'; | ||
getPublicKey(): PublicKey; | ||
/** | ||
* The Bech32 secret key string for this Secp256r1 keypair | ||
*/ | ||
getSecretKey(): string; | ||
sign(data: Uint8Array): Promise<Uint8Array>; | ||
@@ -72,3 +75,2 @@ /** | ||
static deriveKeypair(mnemonics: string, path?: string): Secp256r1Keypair; | ||
export(): ExportedKeypair; | ||
} |
@@ -1,2 +0,1 @@ | ||
import { toB64 } from "@mysten/bcs"; | ||
import { secp256r1 } from "@noble/curves/p256"; | ||
@@ -7,3 +6,3 @@ import { blake2b } from "@noble/hashes/blake2b"; | ||
import { HDKey } from "@scure/bip32"; | ||
import { Keypair } from "../../cryptography/keypair.js"; | ||
import { encodeSuiPrivateKey, Keypair } from "../../cryptography/keypair.js"; | ||
import { isValidBIP32Path, mnemonicToSeed } from "../../cryptography/mnemonics.js"; | ||
@@ -81,2 +80,8 @@ import { Secp256r1PublicKey } from "./publickey.js"; | ||
} | ||
/** | ||
* The Bech32 secret key string for this Secp256r1 keypair | ||
*/ | ||
getSecretKey() { | ||
return encodeSuiPrivateKey(this.keypair.secretKey, this.getKeyScheme()); | ||
} | ||
async sign(data) { | ||
@@ -112,8 +117,2 @@ return this.signData(data); | ||
} | ||
export() { | ||
return { | ||
schema: "Secp256r1", | ||
privateKey: toB64(this.keypair.secretKey) | ||
}; | ||
} | ||
} | ||
@@ -120,0 +119,0 @@ export { |
@@ -1,2 +0,2 @@ | ||
export declare const PACKAGE_VERSION = "0.0.0-experimental-20240125183206"; | ||
export declare const PACKAGE_VERSION = "0.0.0-experimental-20240130153923"; | ||
export declare const TARGETED_RPC_VERSION = "1.18.0"; |
@@ -1,2 +0,2 @@ | ||
const PACKAGE_VERSION = "0.0.0-experimental-20240125183206"; | ||
const PACKAGE_VERSION = "0.0.0-experimental-20240130153923"; | ||
const TARGETED_RPC_VERSION = "1.18.0"; | ||
@@ -3,0 +3,0 @@ export { |
@@ -6,3 +6,3 @@ { | ||
"homepage": "https://sdk.mystenlabs.com", | ||
"version": "0.0.0-experimental-20240125183206", | ||
"version": "0.0.0-experimental-20240130153923", | ||
"license": "Apache-2.0", | ||
@@ -111,2 +111,3 @@ "sideEffects": false, | ||
"@suchipi/femver": "^1.0.0", | ||
"bech32": "^2.0.0", | ||
"superstruct": "^1.0.3", | ||
@@ -113,0 +114,0 @@ "tweetnacl": "^1.0.3", |
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { toB64 } from '@mysten/bcs'; | ||
import { bcs, toB64 } from '@mysten/bcs'; | ||
import { blake2b } from '@noble/hashes/blake2b'; | ||
import { bech32 } from 'bech32'; | ||
import { bcs } from '../bcs/index.js'; | ||
import { IntentScope, messageWithIntent } from './intent.js'; | ||
import type { PublicKey } from './publickey.js'; | ||
import { SIGNATURE_FLAG_TO_SCHEME, SIGNATURE_SCHEME_TO_FLAG } from './signature-scheme.js'; | ||
import type { SignatureScheme } from './signature-scheme.js'; | ||
@@ -16,3 +17,10 @@ import type { SerializedSignature } from './signature.js'; | ||
export const LEGACY_PRIVATE_KEY_SIZE = 64; | ||
export const SUI_PRIVATE_KEY_PREFIX = 'suiprivkey'; | ||
export type ParsedKeypair = { | ||
schema: SignatureScheme; | ||
secretKey: Uint8Array; | ||
}; | ||
/** @deprecated use string instead. See {@link Keypair.getSecretKey} */ | ||
export type ExportedKeypair = { | ||
@@ -89,7 +97,56 @@ schema: SignatureScheme; | ||
export abstract class Keypair extends BaseSigner { | ||
/** | ||
* This returns the Bech32 secret key string for this keypair. | ||
*/ | ||
abstract getSecretKey(): string; | ||
/** | ||
* @deprecated use {@link Keypair.getSecretKey} instead | ||
* This returns an exported keypair object, schema is the signature | ||
* scheme name, and the private key field is a Bech32 encoded string | ||
* of 33-byte `flag || private_key` that starts with `suiprivkey`. | ||
*/ | ||
export(): ExportedKeypair { | ||
return { | ||
schema: this.getKeyScheme(), | ||
privateKey: this.getSecretKey(), | ||
}; | ||
} | ||
} | ||
/** | ||
* TODO: Document | ||
* This returns an ParsedKeypair object based by validating the | ||
* 33-byte Bech32 encoded string starting with `suiprivkey`, and | ||
* parse out the signature scheme and the private key in bytes. | ||
*/ | ||
export abstract class Keypair extends BaseSigner { | ||
abstract export(): ExportedKeypair; | ||
export function decodeSuiPrivateKey(value: string): ParsedKeypair { | ||
const { prefix, words } = bech32.decode(value); | ||
if (prefix !== SUI_PRIVATE_KEY_PREFIX) { | ||
throw new Error('invalid private key prefix'); | ||
} | ||
const extendedSecretKey = new Uint8Array(bech32.fromWords(words)); | ||
const secretKey = extendedSecretKey.slice(1); | ||
const signatureScheme = | ||
SIGNATURE_FLAG_TO_SCHEME[extendedSecretKey[0] as keyof typeof SIGNATURE_FLAG_TO_SCHEME]; | ||
return { | ||
schema: signatureScheme, | ||
secretKey: secretKey, | ||
}; | ||
} | ||
/** | ||
* This returns a Bech32 encoded string starting with `suiprivkey`, | ||
* encoding 33-byte `flag || bytes` for the given the 32-byte private | ||
* key and its signature scheme. | ||
*/ | ||
export function encodeSuiPrivateKey(bytes: Uint8Array, scheme: SignatureScheme): string { | ||
if (bytes.length !== PRIVATE_KEY_SIZE) { | ||
throw new Error('Invalid bytes length'); | ||
} | ||
const flag = SIGNATURE_SCHEME_TO_FLAG[scheme]; | ||
const privKeyBytes = new Uint8Array(bytes.length + 1); | ||
privKeyBytes.set([flag]); | ||
privKeyBytes.set(bytes, 1); | ||
return bech32.encode(SUI_PRIVATE_KEY_PREFIX, bech32.toWords(privKeyBytes)); | ||
} |
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { toB64 } from '@mysten/bcs'; | ||
import nacl from 'tweetnacl'; | ||
import type { ExportedKeypair } from '../../cryptography/keypair.js'; | ||
import { Keypair, PRIVATE_KEY_SIZE } from '../../cryptography/keypair.js'; | ||
import { encodeSuiPrivateKey, Keypair, PRIVATE_KEY_SIZE } from '../../cryptography/keypair.js'; | ||
import { isValidHardenedPath, mnemonicToSeedHex } from '../../cryptography/mnemonics.js'; | ||
@@ -66,14 +64,2 @@ import type { SignatureScheme } from '../../cryptography/signature-scheme.js'; | ||
* | ||
* The sui.keystore key is a list of Base64 encoded `flag || privkey`. To import | ||
* a key from sui.keystore to typescript, decode from base64 and remove the first | ||
* flag byte after checking it is indeed the Ed25519 scheme flag 0x00 (See more | ||
* on flag for signature scheme: https://github.com/MystenLabs/sui/blob/818406c5abdf7de1b80915a0519071eec3a5b1c7/crates/sui-types/src/crypto.rs#L1650): | ||
* ``` | ||
* import { Ed25519Keypair, fromB64 } from '@mysten/sui.js'; | ||
* const raw = fromB64(t[1]); | ||
* if (raw[0] !== 0 || raw.length !== PRIVATE_KEY_SIZE + 1) { | ||
* throw new Error('invalid key'); | ||
* } | ||
* const imported = Ed25519Keypair.fromSecretKey(raw.slice(1)) | ||
* ``` | ||
* @throws error if the provided secret key is invalid and validation is not skipped. | ||
@@ -113,2 +99,12 @@ * | ||
/** | ||
* The Bech32 secret key string for this Ed25519 keypair | ||
*/ | ||
getSecretKey(): string { | ||
return encodeSuiPrivateKey( | ||
this.keypair.secretKey.slice(0, PRIVATE_KEY_SIZE), | ||
this.getKeyScheme(), | ||
); | ||
} | ||
async sign(data: Uint8Array) { | ||
@@ -161,12 +157,2 @@ return this.signData(data); | ||
} | ||
/** | ||
* This returns an exported keypair object, the private key field is the pure 32-byte seed. | ||
*/ | ||
export(): ExportedKeypair { | ||
return { | ||
schema: 'ED25519', | ||
privateKey: toB64(this.keypair.secretKey.slice(0, PRIVATE_KEY_SIZE)), | ||
}; | ||
} | ||
} |
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { toB64 } from '@mysten/bcs'; | ||
import { secp256k1 } from '@noble/curves/secp256k1'; | ||
@@ -11,4 +10,3 @@ import { blake2b } from '@noble/hashes/blake2b'; | ||
import type { ExportedKeypair } from '../../cryptography/keypair.js'; | ||
import { Keypair } from '../../cryptography/keypair.js'; | ||
import { encodeSuiPrivateKey, Keypair } from '../../cryptography/keypair.js'; | ||
import { isValidBIP32Path, mnemonicToSeed } from '../../cryptography/mnemonics.js'; | ||
@@ -113,2 +111,8 @@ import type { PublicKey } from '../../cryptography/publickey.js'; | ||
} | ||
/** | ||
* The Bech32 secret key string for this Secp256k1 keypair | ||
*/ | ||
getSecretKey(): string { | ||
return encodeSuiPrivateKey(this.keypair.secretKey, this.getKeyScheme()); | ||
} | ||
@@ -153,9 +157,2 @@ async sign(data: Uint8Array) { | ||
} | ||
export(): ExportedKeypair { | ||
return { | ||
schema: 'Secp256k1', | ||
privateKey: toB64(this.keypair.secretKey), | ||
}; | ||
} | ||
} |
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { toB64 } from '@mysten/bcs'; | ||
import { secp256r1 } from '@noble/curves/p256'; | ||
@@ -11,4 +10,3 @@ import { blake2b } from '@noble/hashes/blake2b'; | ||
import type { ExportedKeypair } from '../../cryptography/keypair.js'; | ||
import { Keypair } from '../../cryptography/keypair.js'; | ||
import { encodeSuiPrivateKey, Keypair } from '../../cryptography/keypair.js'; | ||
import { isValidBIP32Path, mnemonicToSeed } from '../../cryptography/mnemonics.js'; | ||
@@ -114,2 +112,9 @@ import type { PublicKey } from '../../cryptography/publickey.js'; | ||
/** | ||
* The Bech32 secret key string for this Secp256r1 keypair | ||
*/ | ||
getSecretKey(): string { | ||
return encodeSuiPrivateKey(this.keypair.secretKey, this.getKeyScheme()); | ||
} | ||
async sign(data: Uint8Array) { | ||
@@ -148,9 +153,2 @@ return this.signData(data); | ||
} | ||
export(): ExportedKeypair { | ||
return { | ||
schema: 'Secp256r1', | ||
privateKey: toB64(this.keypair.secretKey), | ||
}; | ||
} | ||
} |
@@ -6,3 +6,3 @@ // Copyright (c) Mysten Labs, Inc. | ||
export const PACKAGE_VERSION = '0.0.0-experimental-20240125183206'; | ||
export const PACKAGE_VERSION = '0.0.0-experimental-20240130153923'; | ||
export const TARGETED_RPC_VERSION = '1.18.0'; |
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
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
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
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2379494
42708
9
+ Addedbech32@^2.0.0
+ Addedbech32@2.0.0(transitive)