Skip to content

Commit 78ec0a5

Browse files
committed
feat: add PublicKey.toBytes and fix buffer incompatibility
1 parent a1dffa3 commit 78ec0a5

File tree

6 files changed

+36
-28
lines changed

6 files changed

+36
-28
lines changed

src/message.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {Blockhash} from './blockhash';
77
import * as Layout from './layout';
88
import {PACKET_DATA_SIZE} from './transaction';
99
import * as shortvec from './util/shortvec-encoding';
10+
import {toBuffer} from './util/to-buffer';
1011

1112
/**
1213
* The message header, identifying signed and read-only account
@@ -160,7 +161,7 @@ export class Message {
160161
this.header.numReadonlyUnsignedAccounts,
161162
]),
162163
keyCount: Buffer.from(keyCount),
163-
keys: this.accountKeys.map(key => key.toBuffer()),
164+
keys: this.accountKeys.map(key => toBuffer(key.toBytes())),
164165
recentBlockhash: bs58.decode(this.recentBlockhash),
165166
};
166167

src/publickey.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import nacl from 'tweetnacl';
44
import {sha256} from 'crypto-hash';
55
import {Buffer} from 'buffer';
66

7+
import {toBuffer} from './util/to-buffer';
8+
79
/**
810
* Maximum length of derived pubkey seed
911
*/
@@ -48,7 +50,14 @@ export class PublicKey {
4850
* Return the base-58 representation of the public key
4951
*/
5052
toBase58(): string {
51-
return bs58.encode(this.toBuffer());
53+
return bs58.encode(this.toBytes());
54+
}
55+
56+
/**
57+
* Return the byte array representation of the public key
58+
*/
59+
toBytes(): Uint8Array {
60+
return this.toBuffer();
5261
}
5362

5463
/**
@@ -101,7 +110,7 @@ export class PublicKey {
101110
if (seed.length > MAX_SEED_LENGTH) {
102111
throw new Error(`Max seed length exceeded`);
103112
}
104-
buffer = Buffer.concat([buffer, Buffer.from(seed)]);
113+
buffer = Buffer.concat([buffer, toBuffer(seed)]);
105114
});
106115
buffer = Buffer.concat([
107116
buffer,

src/secp256k1-program.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,8 @@ export class Secp256k1Program {
195195
`Private key must be ${PRIVATE_KEY_BYTES} bytes but received ${pkey.length} bytes`,
196196
);
197197

198-
let privateKey;
199-
if (Array.isArray(pkey)) {
200-
privateKey = Uint8Array.from(pkey);
201-
} else {
202-
privateKey = pkey;
203-
}
204-
205198
try {
199+
const privateKey = toBuffer(pkey);
206200
const publicKey = publicKeyCreate(privateKey, false).slice(1); // throw away leading byte
207201
const messageHash = Buffer.from(
208202
keccak_256.update(toBuffer(message)).digest(),

src/stake-program.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
SYSVAR_STAKE_HISTORY_PUBKEY,
1111
} from './sysvar';
1212
import {Transaction, TransactionInstruction} from './transaction';
13+
import {toBuffer} from './util/to-buffer';
1314

1415
/**
1516
* Address of the stake config account which configures the rate
@@ -506,13 +507,13 @@ export class StakeProgram {
506507
const type = STAKE_INSTRUCTION_LAYOUTS.Initialize;
507508
const data = encodeData(type, {
508509
authorized: {
509-
staker: authorized.staker.toBuffer(),
510-
withdrawer: authorized.withdrawer.toBuffer(),
510+
staker: toBuffer(authorized.staker.toBytes()),
511+
withdrawer: toBuffer(authorized.withdrawer.toBytes()),
511512
},
512513
lockup: {
513514
unixTimestamp: lockup.unixTimestamp,
514515
epoch: lockup.epoch,
515-
custodian: lockup.custodian.toBuffer(),
516+
custodian: toBuffer(lockup.custodian.toBytes()),
516517
},
517518
});
518519
const instructionData = {
@@ -613,7 +614,7 @@ export class StakeProgram {
613614

614615
const type = STAKE_INSTRUCTION_LAYOUTS.Authorize;
615616
const data = encodeData(type, {
616-
newAuthorized: newAuthorizedPubkey.toBuffer(),
617+
newAuthorized: toBuffer(newAuthorizedPubkey.toBytes()),
617618
stakeAuthorizationType: stakeAuthorizationType.index,
618619
});
619620

@@ -649,10 +650,10 @@ export class StakeProgram {
649650

650651
const type = STAKE_INSTRUCTION_LAYOUTS.AuthorizeWithSeed;
651652
const data = encodeData(type, {
652-
newAuthorized: newAuthorizedPubkey.toBuffer(),
653+
newAuthorized: toBuffer(newAuthorizedPubkey.toBytes()),
653654
stakeAuthorizationType: stakeAuthorizationType.index,
654655
authoritySeed: authoritySeed,
655-
authorityOwner: authorityOwner.toBuffer(),
656+
authorityOwner: toBuffer(authorityOwner.toBytes()),
656657
});
657658

658659
const keys = [

src/system-program.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {NONCE_ACCOUNT_LENGTH} from './nonce-account';
66
import {PublicKey} from './publickey';
77
import {SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY} from './sysvar';
88
import {Transaction, TransactionInstruction} from './transaction';
9+
import {toBuffer} from './util/to-buffer';
910

1011
/**
1112
* Create account system transaction params
@@ -669,7 +670,7 @@ export class SystemProgram {
669670
const data = encodeData(type, {
670671
lamports: params.lamports,
671672
space: params.space,
672-
programId: params.programId.toBuffer(),
673+
programId: toBuffer(params.programId.toBytes()),
673674
});
674675

675676
return new TransactionInstruction({
@@ -695,7 +696,7 @@ export class SystemProgram {
695696
data = encodeData(type, {
696697
lamports: params.lamports,
697698
seed: params.seed,
698-
programId: params.programId.toBuffer(),
699+
programId: toBuffer(params.programId.toBytes()),
699700
});
700701
keys = [
701702
{pubkey: params.fromPubkey, isSigner: false, isWritable: true},
@@ -729,17 +730,19 @@ export class SystemProgram {
729730
if ('basePubkey' in params) {
730731
const type = SYSTEM_INSTRUCTION_LAYOUTS.AssignWithSeed;
731732
data = encodeData(type, {
732-
base: params.basePubkey.toBuffer(),
733+
base: toBuffer(params.basePubkey.toBytes()),
733734
seed: params.seed,
734-
programId: params.programId.toBuffer(),
735+
programId: toBuffer(params.programId.toBytes()),
735736
});
736737
keys = [
737738
{pubkey: params.accountPubkey, isSigner: false, isWritable: true},
738739
{pubkey: params.basePubkey, isSigner: true, isWritable: false},
739740
];
740741
} else {
741742
const type = SYSTEM_INSTRUCTION_LAYOUTS.Assign;
742-
data = encodeData(type, {programId: params.programId.toBuffer()});
743+
data = encodeData(type, {
744+
programId: toBuffer(params.programId.toBytes()),
745+
});
743746
keys = [{pubkey: params.accountPubkey, isSigner: true, isWritable: true}];
744747
}
745748

@@ -759,11 +762,11 @@ export class SystemProgram {
759762
): TransactionInstruction {
760763
const type = SYSTEM_INSTRUCTION_LAYOUTS.CreateWithSeed;
761764
const data = encodeData(type, {
762-
base: params.basePubkey.toBuffer(),
765+
base: toBuffer(params.basePubkey.toBytes()),
763766
seed: params.seed,
764767
lamports: params.lamports,
765768
space: params.space,
766-
programId: params.programId.toBuffer(),
769+
programId: toBuffer(params.programId.toBytes()),
767770
});
768771
let keys = [
769772
{pubkey: params.fromPubkey, isSigner: true, isWritable: true},
@@ -828,7 +831,7 @@ export class SystemProgram {
828831
): TransactionInstruction {
829832
const type = SYSTEM_INSTRUCTION_LAYOUTS.InitializeNonceAccount;
830833
const data = encodeData(type, {
831-
authorized: params.authorizedPubkey.toBuffer(),
834+
authorized: toBuffer(params.authorizedPubkey.toBytes()),
832835
});
833836
const instructionData = {
834837
keys: [
@@ -903,7 +906,7 @@ export class SystemProgram {
903906
static nonceAuthorize(params: AuthorizeNonceParams): TransactionInstruction {
904907
const type = SYSTEM_INSTRUCTION_LAYOUTS.AuthorizeNonceAccount;
905908
const data = encodeData(type, {
906-
authorized: params.newAuthorizedPubkey.toBuffer(),
909+
authorized: toBuffer(params.newAuthorizedPubkey.toBytes()),
907910
});
908911

909912
return new TransactionInstruction({
@@ -927,10 +930,10 @@ export class SystemProgram {
927930
if ('basePubkey' in params) {
928931
const type = SYSTEM_INSTRUCTION_LAYOUTS.AllocateWithSeed;
929932
data = encodeData(type, {
930-
base: params.basePubkey.toBuffer(),
933+
base: toBuffer(params.basePubkey.toBytes()),
931934
seed: params.seed,
932935
space: params.space,
933-
programId: params.programId.toBuffer(),
936+
programId: toBuffer(params.programId.toBytes()),
934937
});
935938
keys = [
936939
{pubkey: params.accountPubkey, isSigner: false, isWritable: true},

src/transaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ export class Transaction {
569569
}
570570
} else {
571571
if (
572-
!nacl.sign.detached.verify(signData, signature, publicKey.toBuffer())
572+
!nacl.sign.detached.verify(signData, signature, publicKey.toBytes())
573573
) {
574574
return false;
575575
}

0 commit comments

Comments
 (0)