1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+ import { NetworkType } from '../../model/blockchain/NetworkType' ;
1617import { Convert as convert } from '../format' ;
1718import { SignSchema } from './SignSchema' ;
1819import * as Utility from './Utilities' ;
@@ -21,18 +22,15 @@ export class KeyPair {
2122 /**
2223 * Creates a key pair from a private key string.
2324 * @param {string } privateKeyString A hex encoded private key string.
24- * @param {SignSchema } signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3)
25+ * @param {SignSchema } signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult) )
2526 * @returns {module:crypto/keyPair~KeyPair } The key pair.
2627 */
27- public static createKeyPairFromPrivateKeyString = ( privateKeyString , signSchema = SignSchema . SHA3 ) => {
28+ public static createKeyPairFromPrivateKeyString = ( privateKeyString : string , signSchema : SignSchema ) => {
2829 const privateKey = convert . hexToUint8 ( privateKeyString ) ;
29-
30- // KECCAK_REVERSED_KEY uses reversed private key.
31- const secretKey = signSchema === SignSchema . SHA3 ? privateKey : convert . hexToUint8Reverse ( privateKeyString ) ;
3230 if ( Utility . Key_Size !== privateKey . length ) {
3331 throw Error ( `private key has unexpected size: ${ privateKey . length } ` ) ;
3432 }
35- const publicKey = Utility . catapult_crypto . extractPublicKey ( secretKey , Utility . catapult_hash . func , signSchema ) ;
33+ const publicKey = Utility . catapult_crypto . extractPublicKey ( privateKey , Utility . catapult_hash . func , signSchema ) ;
3634 return {
3735 privateKey,
3836 publicKey,
@@ -43,16 +41,11 @@ export class KeyPair {
4341 * Signs a data buffer with a key pair.
4442 * @param {module:crypto/keyPair~KeyPair } keyPair The key pair to use for signing.
4543 * @param {Uint8Array } data The data to sign.
46- * @param {SignSchema } signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3)
44+ * @param {SignSchema } signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult) )
4745 * @returns {Uint8Array } The signature.
4846 */
49- public static sign = ( keyPair , data , signSchema = SignSchema . SHA3 ) => {
50- let secretKey = keyPair . privateKey ;
51- // KECCAK_REVERSED_KEY uses reversed private key.
52- if ( signSchema === SignSchema . KECCAK_REVERSED_KEY ) {
53- secretKey = convert . hexToUint8Reverse ( convert . uint8ToHex ( secretKey ) ) ;
54- }
55- return Utility . catapult_crypto . sign ( data , keyPair . publicKey , secretKey ,
47+ public static sign = ( keyPair , data : Uint8Array , signSchema : SignSchema ) => {
48+ return Utility . catapult_crypto . sign ( data , keyPair . publicKey , keyPair . privateKey ,
5649 Utility . catapult_hash . createHasher ( 64 , signSchema ) ) ;
5750 }
5851
@@ -61,10 +54,10 @@ export class KeyPair {
6154 * @param {module:crypto/keyPair~PublicKey } publicKey The public key to use for verification.
6255 * @param {Uint8Array } data The data to verify.
6356 * @param {Uint8Array } signature The signature to verify.
64- * @param {SignSchema } signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3)
57+ * @param {SignSchema } signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult) )
6558 * @returns {boolean } true if the signature is verifiable, false otherwise.
6659 */
67- public static verify = ( publicKey , data , signature , signSchema = SignSchema . SHA3 ) => {
60+ public static verify = ( publicKey , data : Uint8Array , signature : Uint8Array , signSchema : SignSchema ) => {
6861 return Utility . catapult_crypto . verify ( publicKey , data , signature , Utility . catapult_hash . createHasher ( 64 , signSchema ) ) ;
6962 }
7063
@@ -74,21 +67,16 @@ export class KeyPair {
7467 * @param {module:crypto/keyPair~KeyPair } keyPair The key pair for which to create the shared key.
7568 * @param {Uint8Array } publicKey The public key for which to create the shared key.
7669 * @param {Uint8Array } salt A salt that should be applied to the shared key.
77- * @param {SignSchema } signSchema The Sign Schema. (KECCAK_REVERSED_KEY / SHA3)
70+ * @param {SignSchema } signSchema The Sign Schema. (KECCAK(NIS1) / SHA3(Catapult) )
7871 * @returns {Uint8Array } The shared key.
7972 */
80- public static deriveSharedKey = ( keyPair , publicKey , salt , signSchema = SignSchema . SHA3 ) => {
73+ public static deriveSharedKey = ( keyPair , publicKey : Uint8Array , salt : Uint8Array , signSchema : SignSchema ) => {
8174 if ( Utility . Key_Size !== salt . length ) {
8275 throw Error ( `salt has unexpected size: ${ salt . length } ` ) ;
8376 }
8477 if ( Utility . Key_Size !== publicKey . length ) {
8578 throw Error ( `public key has unexpected size: ${ salt . length } ` ) ;
8679 }
87- let secretKey = keyPair . privateKey ;
88- // KECCAK_REVERSED_KEY uses reversed private key.
89- if ( signSchema === SignSchema . KECCAK_REVERSED_KEY ) {
90- secretKey = convert . hexToUint8Reverse ( convert . uint8ToHex ( secretKey ) ) ;
91- }
92- return Utility . catapult_crypto . deriveSharedKey ( salt , secretKey , publicKey , Utility . catapult_hash . func , signSchema ) ;
80+ return Utility . catapult_crypto . deriveSharedKey ( salt , keyPair . privateKey , publicKey , Utility . catapult_hash . func , signSchema ) ;
9381 }
9482}
0 commit comments