-
Notifications
You must be signed in to change notification settings - Fork 11.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ts-sdk: add secp256k1 keypair (#4410)
- Loading branch information
Showing
16 changed files
with
604 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// 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'; | ||
import { checkPublicKeyData, PublicKeyInitData, SIGNATURE_SCHEME_TO_FLAG } from './publickey'; | ||
|
||
const PUBLIC_KEY_SIZE = 32; | ||
|
||
/** | ||
* An Ed25519 public key | ||
*/ | ||
export class Ed25519PublicKey { | ||
/** @internal */ | ||
_bn: BN; | ||
|
||
/** | ||
* Create a new Ed25519PublicKey object | ||
* @param value ed25519 public key as buffer or base-64 encoded string | ||
*/ | ||
constructor(value: PublicKeyInitData) { | ||
if (checkPublicKeyData(value)) { | ||
this._bn = value._bn; | ||
} else { | ||
if (typeof value === 'string') { | ||
const buffer = Buffer.from(value, 'base64'); | ||
if (buffer.length !== PUBLIC_KEY_SIZE) { | ||
throw new Error( | ||
`Invalid public key input. Expected ${PUBLIC_KEY_SIZE} bytes, got ${buffer.length}` | ||
); | ||
} | ||
this._bn = new BN(buffer); | ||
} else { | ||
this._bn = new BN(value); | ||
} | ||
let length = this._bn.byteLength(); | ||
if (length != PUBLIC_KEY_SIZE) { | ||
throw new Error( | ||
`Invalid public key input. Expected ${PUBLIC_KEY_SIZE} bytes, got ${length}` | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Checks if two Ed25519 public keys are equal | ||
*/ | ||
equals(publicKey: Ed25519PublicKey): boolean { | ||
return this._bn.eq(publicKey._bn); | ||
} | ||
|
||
/** | ||
* Return the base-64 representation of the Ed25519 public key | ||
*/ | ||
toBase64(): string { | ||
return this.toBuffer().toString('base64'); | ||
} | ||
|
||
/** | ||
* Return the byte array representation of the Ed25519 public key | ||
*/ | ||
toBytes(): Uint8Array { | ||
return this.toBuffer(); | ||
} | ||
|
||
/** | ||
* Return the Buffer representation of the Ed25519 public key | ||
*/ | ||
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; | ||
} | ||
|
||
/** | ||
* Return the base-64 representation of the Ed25519 public key | ||
*/ | ||
toString(): string { | ||
return this.toBase64(); | ||
} | ||
|
||
/** | ||
* Return the Sui address associated with this Ed25519 public key | ||
*/ | ||
toSuiAddress(): string { | ||
let tmp = new Uint8Array(PUBLIC_KEY_SIZE + 1); | ||
tmp.set([SIGNATURE_SCHEME_TO_FLAG['ED25519']]); | ||
tmp.set(this.toBytes(), 1); | ||
return sha3_256(tmp).slice(0, 40); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.