feat: wrap public key in a class

This commit is contained in:
Michael Vines
2018-09-30 18:42:45 -07:00
parent 2c3208090c
commit ca6965f8c2
15 changed files with 180 additions and 95 deletions

View File

@@ -4,7 +4,8 @@ import assert from 'assert';
import nacl from 'tweetnacl';
import bs58 from 'bs58';
import type {Account, PublicKey} from './account';
import type {Account} from './account';
import type {PublicKey} from './publickey';
/**
* @typedef {string} TransactionSignature
@@ -92,14 +93,14 @@ export class Transaction {
transactionData.writeUInt32LE(this.keys.length, pos); // u64
pos += 8;
for (let key of this.keys) {
const keyBytes = Transaction.serializePublicKey(key);
const keyBytes = key.toBuffer();
keyBytes.copy(transactionData, pos);
pos += 32;
}
// serialize `this.programId`
if (this.programId) {
const keyBytes = Transaction.serializePublicKey(this.programId);
const keyBytes = this.programId.toBuffer();
keyBytes.copy(transactionData, pos);
}
pos += 32;
@@ -158,14 +159,5 @@ export class Transaction {
transactionData.copy(wireTransaction, signature.length);
return wireTransaction;
}
/**
* Serializes a public key into the wire format
*/
static serializePublicKey(key: PublicKey): Buffer {
const data = Buffer.from(bs58.decode(key));
assert(data.length === 32);
return data;
}
}