feat: add PublicKey.toBytes and fix buffer incompatibility

This commit is contained in:
Justin Starry
2021-03-29 11:00:56 +08:00
committed by Justin Starry
parent abada56ba1
commit a622198235
6 changed files with 36 additions and 28 deletions

View File

@ -7,6 +7,7 @@ import type {Blockhash} from './blockhash';
import * as Layout from './layout'; import * as Layout from './layout';
import {PACKET_DATA_SIZE} from './transaction'; import {PACKET_DATA_SIZE} from './transaction';
import * as shortvec from './util/shortvec-encoding'; import * as shortvec from './util/shortvec-encoding';
import {toBuffer} from './util/to-buffer';
/** /**
* The message header, identifying signed and read-only account * The message header, identifying signed and read-only account
@ -160,7 +161,7 @@ export class Message {
this.header.numReadonlyUnsignedAccounts, this.header.numReadonlyUnsignedAccounts,
]), ]),
keyCount: Buffer.from(keyCount), keyCount: Buffer.from(keyCount),
keys: this.accountKeys.map(key => key.toBuffer()), keys: this.accountKeys.map(key => toBuffer(key.toBytes())),
recentBlockhash: bs58.decode(this.recentBlockhash), recentBlockhash: bs58.decode(this.recentBlockhash),
}; };

View File

@ -4,6 +4,8 @@ import nacl from 'tweetnacl';
import {sha256} from 'crypto-hash'; import {sha256} from 'crypto-hash';
import {Buffer} from 'buffer'; import {Buffer} from 'buffer';
import {toBuffer} from './util/to-buffer';
/** /**
* Maximum length of derived pubkey seed * Maximum length of derived pubkey seed
*/ */
@ -48,7 +50,14 @@ export class PublicKey {
* Return the base-58 representation of the public key * Return the base-58 representation of the public key
*/ */
toBase58(): string { 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) { if (seed.length > MAX_SEED_LENGTH) {
throw new Error(`Max seed length exceeded`); throw new Error(`Max seed length exceeded`);
} }
buffer = Buffer.concat([buffer, Buffer.from(seed)]); buffer = Buffer.concat([buffer, toBuffer(seed)]);
}); });
buffer = Buffer.concat([ buffer = Buffer.concat([
buffer, buffer,

View File

@ -195,14 +195,8 @@ export class Secp256k1Program {
`Private key must be ${PRIVATE_KEY_BYTES} bytes but received ${pkey.length} bytes`, `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 { try {
const privateKey = toBuffer(pkey);
const publicKey = publicKeyCreate(privateKey, false).slice(1); // throw away leading byte const publicKey = publicKeyCreate(privateKey, false).slice(1); // throw away leading byte
const messageHash = Buffer.from( const messageHash = Buffer.from(
keccak_256.update(toBuffer(message)).digest(), keccak_256.update(toBuffer(message)).digest(),

View File

@ -10,6 +10,7 @@ import {
SYSVAR_STAKE_HISTORY_PUBKEY, SYSVAR_STAKE_HISTORY_PUBKEY,
} from './sysvar'; } from './sysvar';
import {Transaction, TransactionInstruction} from './transaction'; import {Transaction, TransactionInstruction} from './transaction';
import {toBuffer} from './util/to-buffer';
/** /**
* Address of the stake config account which configures the rate * Address of the stake config account which configures the rate
@ -506,13 +507,13 @@ export class StakeProgram {
const type = STAKE_INSTRUCTION_LAYOUTS.Initialize; const type = STAKE_INSTRUCTION_LAYOUTS.Initialize;
const data = encodeData(type, { const data = encodeData(type, {
authorized: { authorized: {
staker: authorized.staker.toBuffer(), staker: toBuffer(authorized.staker.toBytes()),
withdrawer: authorized.withdrawer.toBuffer(), withdrawer: toBuffer(authorized.withdrawer.toBytes()),
}, },
lockup: { lockup: {
unixTimestamp: lockup.unixTimestamp, unixTimestamp: lockup.unixTimestamp,
epoch: lockup.epoch, epoch: lockup.epoch,
custodian: lockup.custodian.toBuffer(), custodian: toBuffer(lockup.custodian.toBytes()),
}, },
}); });
const instructionData = { const instructionData = {
@ -613,7 +614,7 @@ export class StakeProgram {
const type = STAKE_INSTRUCTION_LAYOUTS.Authorize; const type = STAKE_INSTRUCTION_LAYOUTS.Authorize;
const data = encodeData(type, { const data = encodeData(type, {
newAuthorized: newAuthorizedPubkey.toBuffer(), newAuthorized: toBuffer(newAuthorizedPubkey.toBytes()),
stakeAuthorizationType: stakeAuthorizationType.index, stakeAuthorizationType: stakeAuthorizationType.index,
}); });
@ -649,10 +650,10 @@ export class StakeProgram {
const type = STAKE_INSTRUCTION_LAYOUTS.AuthorizeWithSeed; const type = STAKE_INSTRUCTION_LAYOUTS.AuthorizeWithSeed;
const data = encodeData(type, { const data = encodeData(type, {
newAuthorized: newAuthorizedPubkey.toBuffer(), newAuthorized: toBuffer(newAuthorizedPubkey.toBytes()),
stakeAuthorizationType: stakeAuthorizationType.index, stakeAuthorizationType: stakeAuthorizationType.index,
authoritySeed: authoritySeed, authoritySeed: authoritySeed,
authorityOwner: authorityOwner.toBuffer(), authorityOwner: toBuffer(authorityOwner.toBytes()),
}); });
const keys = [ const keys = [

View File

@ -6,6 +6,7 @@ import {NONCE_ACCOUNT_LENGTH} from './nonce-account';
import {PublicKey} from './publickey'; import {PublicKey} from './publickey';
import {SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY} from './sysvar'; import {SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY} from './sysvar';
import {Transaction, TransactionInstruction} from './transaction'; import {Transaction, TransactionInstruction} from './transaction';
import {toBuffer} from './util/to-buffer';
/** /**
* Create account system transaction params * Create account system transaction params
@ -669,7 +670,7 @@ export class SystemProgram {
const data = encodeData(type, { const data = encodeData(type, {
lamports: params.lamports, lamports: params.lamports,
space: params.space, space: params.space,
programId: params.programId.toBuffer(), programId: toBuffer(params.programId.toBytes()),
}); });
return new TransactionInstruction({ return new TransactionInstruction({
@ -695,7 +696,7 @@ export class SystemProgram {
data = encodeData(type, { data = encodeData(type, {
lamports: params.lamports, lamports: params.lamports,
seed: params.seed, seed: params.seed,
programId: params.programId.toBuffer(), programId: toBuffer(params.programId.toBytes()),
}); });
keys = [ keys = [
{pubkey: params.fromPubkey, isSigner: false, isWritable: true}, {pubkey: params.fromPubkey, isSigner: false, isWritable: true},
@ -729,9 +730,9 @@ export class SystemProgram {
if ('basePubkey' in params) { if ('basePubkey' in params) {
const type = SYSTEM_INSTRUCTION_LAYOUTS.AssignWithSeed; const type = SYSTEM_INSTRUCTION_LAYOUTS.AssignWithSeed;
data = encodeData(type, { data = encodeData(type, {
base: params.basePubkey.toBuffer(), base: toBuffer(params.basePubkey.toBytes()),
seed: params.seed, seed: params.seed,
programId: params.programId.toBuffer(), programId: toBuffer(params.programId.toBytes()),
}); });
keys = [ keys = [
{pubkey: params.accountPubkey, isSigner: false, isWritable: true}, {pubkey: params.accountPubkey, isSigner: false, isWritable: true},
@ -739,7 +740,9 @@ export class SystemProgram {
]; ];
} else { } else {
const type = SYSTEM_INSTRUCTION_LAYOUTS.Assign; 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}]; keys = [{pubkey: params.accountPubkey, isSigner: true, isWritable: true}];
} }
@ -759,11 +762,11 @@ export class SystemProgram {
): TransactionInstruction { ): TransactionInstruction {
const type = SYSTEM_INSTRUCTION_LAYOUTS.CreateWithSeed; const type = SYSTEM_INSTRUCTION_LAYOUTS.CreateWithSeed;
const data = encodeData(type, { const data = encodeData(type, {
base: params.basePubkey.toBuffer(), base: toBuffer(params.basePubkey.toBytes()),
seed: params.seed, seed: params.seed,
lamports: params.lamports, lamports: params.lamports,
space: params.space, space: params.space,
programId: params.programId.toBuffer(), programId: toBuffer(params.programId.toBytes()),
}); });
let keys = [ let keys = [
{pubkey: params.fromPubkey, isSigner: true, isWritable: true}, {pubkey: params.fromPubkey, isSigner: true, isWritable: true},
@ -828,7 +831,7 @@ export class SystemProgram {
): TransactionInstruction { ): TransactionInstruction {
const type = SYSTEM_INSTRUCTION_LAYOUTS.InitializeNonceAccount; const type = SYSTEM_INSTRUCTION_LAYOUTS.InitializeNonceAccount;
const data = encodeData(type, { const data = encodeData(type, {
authorized: params.authorizedPubkey.toBuffer(), authorized: toBuffer(params.authorizedPubkey.toBytes()),
}); });
const instructionData = { const instructionData = {
keys: [ keys: [
@ -903,7 +906,7 @@ export class SystemProgram {
static nonceAuthorize(params: AuthorizeNonceParams): TransactionInstruction { static nonceAuthorize(params: AuthorizeNonceParams): TransactionInstruction {
const type = SYSTEM_INSTRUCTION_LAYOUTS.AuthorizeNonceAccount; const type = SYSTEM_INSTRUCTION_LAYOUTS.AuthorizeNonceAccount;
const data = encodeData(type, { const data = encodeData(type, {
authorized: params.newAuthorizedPubkey.toBuffer(), authorized: toBuffer(params.newAuthorizedPubkey.toBytes()),
}); });
return new TransactionInstruction({ return new TransactionInstruction({
@ -927,10 +930,10 @@ export class SystemProgram {
if ('basePubkey' in params) { if ('basePubkey' in params) {
const type = SYSTEM_INSTRUCTION_LAYOUTS.AllocateWithSeed; const type = SYSTEM_INSTRUCTION_LAYOUTS.AllocateWithSeed;
data = encodeData(type, { data = encodeData(type, {
base: params.basePubkey.toBuffer(), base: toBuffer(params.basePubkey.toBytes()),
seed: params.seed, seed: params.seed,
space: params.space, space: params.space,
programId: params.programId.toBuffer(), programId: toBuffer(params.programId.toBytes()),
}); });
keys = [ keys = [
{pubkey: params.accountPubkey, isSigner: false, isWritable: true}, {pubkey: params.accountPubkey, isSigner: false, isWritable: true},

View File

@ -569,7 +569,7 @@ export class Transaction {
} }
} else { } else {
if ( if (
!nacl.sign.detached.verify(signData, signature, publicKey.toBuffer()) !nacl.sign.detached.verify(signData, signature, publicKey.toBytes())
) { ) {
return false; return false;
} }