From a6221982358d4970b6a870ef8f1b3de79c490e02 Mon Sep 17 00:00:00 2001 From: Justin Starry Date: Mon, 29 Mar 2021 11:00:56 +0800 Subject: [PATCH] feat: add PublicKey.toBytes and fix buffer incompatibility --- web3.js/src/message.ts | 3 ++- web3.js/src/publickey.ts | 13 +++++++++++-- web3.js/src/secp256k1-program.ts | 8 +------- web3.js/src/stake-program.ts | 13 +++++++------ web3.js/src/system-program.ts | 25 ++++++++++++++----------- web3.js/src/transaction.ts | 2 +- 6 files changed, 36 insertions(+), 28 deletions(-) diff --git a/web3.js/src/message.ts b/web3.js/src/message.ts index 17a23179c537ad..d3cdebf8ce70ea 100644 --- a/web3.js/src/message.ts +++ b/web3.js/src/message.ts @@ -7,6 +7,7 @@ import type {Blockhash} from './blockhash'; import * as Layout from './layout'; import {PACKET_DATA_SIZE} from './transaction'; import * as shortvec from './util/shortvec-encoding'; +import {toBuffer} from './util/to-buffer'; /** * The message header, identifying signed and read-only account @@ -160,7 +161,7 @@ export class Message { this.header.numReadonlyUnsignedAccounts, ]), keyCount: Buffer.from(keyCount), - keys: this.accountKeys.map(key => key.toBuffer()), + keys: this.accountKeys.map(key => toBuffer(key.toBytes())), recentBlockhash: bs58.decode(this.recentBlockhash), }; diff --git a/web3.js/src/publickey.ts b/web3.js/src/publickey.ts index e459d219852188..d27ad9bb699299 100644 --- a/web3.js/src/publickey.ts +++ b/web3.js/src/publickey.ts @@ -4,6 +4,8 @@ import nacl from 'tweetnacl'; import {sha256} from 'crypto-hash'; import {Buffer} from 'buffer'; +import {toBuffer} from './util/to-buffer'; + /** * Maximum length of derived pubkey seed */ @@ -48,7 +50,14 @@ export class PublicKey { * Return the base-58 representation of the public key */ toBase58(): string { - return bs58.encode(this.toBuffer()); + return bs58.encode(this.toBytes()); + } + + /** + * Return the byte array representation of the public key + */ + toBytes(): Uint8Array { + return this.toBuffer(); } /** @@ -101,7 +110,7 @@ export class PublicKey { if (seed.length > MAX_SEED_LENGTH) { throw new Error(`Max seed length exceeded`); } - buffer = Buffer.concat([buffer, Buffer.from(seed)]); + buffer = Buffer.concat([buffer, toBuffer(seed)]); }); buffer = Buffer.concat([ buffer, diff --git a/web3.js/src/secp256k1-program.ts b/web3.js/src/secp256k1-program.ts index 476ebfbb111b82..a610fa212d66ec 100644 --- a/web3.js/src/secp256k1-program.ts +++ b/web3.js/src/secp256k1-program.ts @@ -195,14 +195,8 @@ export class Secp256k1Program { `Private key must be ${PRIVATE_KEY_BYTES} bytes but received ${pkey.length} bytes`, ); - let privateKey; - if (Array.isArray(pkey)) { - privateKey = Uint8Array.from(pkey); - } else { - privateKey = pkey; - } - try { + const privateKey = toBuffer(pkey); const publicKey = publicKeyCreate(privateKey, false).slice(1); // throw away leading byte const messageHash = Buffer.from( keccak_256.update(toBuffer(message)).digest(), diff --git a/web3.js/src/stake-program.ts b/web3.js/src/stake-program.ts index ca61b4cb62d7f9..ec07edfffa54c5 100644 --- a/web3.js/src/stake-program.ts +++ b/web3.js/src/stake-program.ts @@ -10,6 +10,7 @@ import { SYSVAR_STAKE_HISTORY_PUBKEY, } from './sysvar'; import {Transaction, TransactionInstruction} from './transaction'; +import {toBuffer} from './util/to-buffer'; /** * Address of the stake config account which configures the rate @@ -506,13 +507,13 @@ export class StakeProgram { const type = STAKE_INSTRUCTION_LAYOUTS.Initialize; const data = encodeData(type, { authorized: { - staker: authorized.staker.toBuffer(), - withdrawer: authorized.withdrawer.toBuffer(), + staker: toBuffer(authorized.staker.toBytes()), + withdrawer: toBuffer(authorized.withdrawer.toBytes()), }, lockup: { unixTimestamp: lockup.unixTimestamp, epoch: lockup.epoch, - custodian: lockup.custodian.toBuffer(), + custodian: toBuffer(lockup.custodian.toBytes()), }, }); const instructionData = { @@ -613,7 +614,7 @@ export class StakeProgram { const type = STAKE_INSTRUCTION_LAYOUTS.Authorize; const data = encodeData(type, { - newAuthorized: newAuthorizedPubkey.toBuffer(), + newAuthorized: toBuffer(newAuthorizedPubkey.toBytes()), stakeAuthorizationType: stakeAuthorizationType.index, }); @@ -649,10 +650,10 @@ export class StakeProgram { const type = STAKE_INSTRUCTION_LAYOUTS.AuthorizeWithSeed; const data = encodeData(type, { - newAuthorized: newAuthorizedPubkey.toBuffer(), + newAuthorized: toBuffer(newAuthorizedPubkey.toBytes()), stakeAuthorizationType: stakeAuthorizationType.index, authoritySeed: authoritySeed, - authorityOwner: authorityOwner.toBuffer(), + authorityOwner: toBuffer(authorityOwner.toBytes()), }); const keys = [ diff --git a/web3.js/src/system-program.ts b/web3.js/src/system-program.ts index e3127409e123fe..b7194613767fa0 100644 --- a/web3.js/src/system-program.ts +++ b/web3.js/src/system-program.ts @@ -6,6 +6,7 @@ import {NONCE_ACCOUNT_LENGTH} from './nonce-account'; import {PublicKey} from './publickey'; import {SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY} from './sysvar'; import {Transaction, TransactionInstruction} from './transaction'; +import {toBuffer} from './util/to-buffer'; /** * Create account system transaction params @@ -669,7 +670,7 @@ export class SystemProgram { const data = encodeData(type, { lamports: params.lamports, space: params.space, - programId: params.programId.toBuffer(), + programId: toBuffer(params.programId.toBytes()), }); return new TransactionInstruction({ @@ -695,7 +696,7 @@ export class SystemProgram { data = encodeData(type, { lamports: params.lamports, seed: params.seed, - programId: params.programId.toBuffer(), + programId: toBuffer(params.programId.toBytes()), }); keys = [ {pubkey: params.fromPubkey, isSigner: false, isWritable: true}, @@ -729,9 +730,9 @@ export class SystemProgram { if ('basePubkey' in params) { const type = SYSTEM_INSTRUCTION_LAYOUTS.AssignWithSeed; data = encodeData(type, { - base: params.basePubkey.toBuffer(), + base: toBuffer(params.basePubkey.toBytes()), seed: params.seed, - programId: params.programId.toBuffer(), + programId: toBuffer(params.programId.toBytes()), }); keys = [ {pubkey: params.accountPubkey, isSigner: false, isWritable: true}, @@ -739,7 +740,9 @@ export class SystemProgram { ]; } else { const type = SYSTEM_INSTRUCTION_LAYOUTS.Assign; - data = encodeData(type, {programId: params.programId.toBuffer()}); + data = encodeData(type, { + programId: toBuffer(params.programId.toBytes()), + }); keys = [{pubkey: params.accountPubkey, isSigner: true, isWritable: true}]; } @@ -759,11 +762,11 @@ export class SystemProgram { ): TransactionInstruction { const type = SYSTEM_INSTRUCTION_LAYOUTS.CreateWithSeed; const data = encodeData(type, { - base: params.basePubkey.toBuffer(), + base: toBuffer(params.basePubkey.toBytes()), seed: params.seed, lamports: params.lamports, space: params.space, - programId: params.programId.toBuffer(), + programId: toBuffer(params.programId.toBytes()), }); let keys = [ {pubkey: params.fromPubkey, isSigner: true, isWritable: true}, @@ -828,7 +831,7 @@ export class SystemProgram { ): TransactionInstruction { const type = SYSTEM_INSTRUCTION_LAYOUTS.InitializeNonceAccount; const data = encodeData(type, { - authorized: params.authorizedPubkey.toBuffer(), + authorized: toBuffer(params.authorizedPubkey.toBytes()), }); const instructionData = { keys: [ @@ -903,7 +906,7 @@ export class SystemProgram { static nonceAuthorize(params: AuthorizeNonceParams): TransactionInstruction { const type = SYSTEM_INSTRUCTION_LAYOUTS.AuthorizeNonceAccount; const data = encodeData(type, { - authorized: params.newAuthorizedPubkey.toBuffer(), + authorized: toBuffer(params.newAuthorizedPubkey.toBytes()), }); return new TransactionInstruction({ @@ -927,10 +930,10 @@ export class SystemProgram { if ('basePubkey' in params) { const type = SYSTEM_INSTRUCTION_LAYOUTS.AllocateWithSeed; data = encodeData(type, { - base: params.basePubkey.toBuffer(), + base: toBuffer(params.basePubkey.toBytes()), seed: params.seed, space: params.space, - programId: params.programId.toBuffer(), + programId: toBuffer(params.programId.toBytes()), }); keys = [ {pubkey: params.accountPubkey, isSigner: false, isWritable: true}, diff --git a/web3.js/src/transaction.ts b/web3.js/src/transaction.ts index f3a99728c0f4bc..872cb7fb4e3267 100644 --- a/web3.js/src/transaction.ts +++ b/web3.js/src/transaction.ts @@ -569,7 +569,7 @@ export class Transaction { } } else { if ( - !nacl.sign.detached.verify(signData, signature, publicKey.toBuffer()) + !nacl.sign.detached.verify(signData, signature, publicKey.toBytes()) ) { return false; }