fix: Add preliminary handling for credit-only account designation

This commit is contained in:
Tyera Eulberg
2019-05-23 18:24:38 -06:00
committed by Michael Vines
parent 78ad376bcd
commit c99d8dd15c
9 changed files with 156 additions and 110 deletions

View File

@@ -201,7 +201,10 @@ export class BudgetProgram {
}
return new Transaction().add({
keys: [{pubkey: from, isSigner: true}, {pubkey: to, isSigner: false}],
keys: [
{pubkey: from, isSigner: true, isDebitable: true},
{pubkey: to, isSigner: false, isDebitable: false},
],
programId: this.programId,
data: data.slice(0, pos),
});
@@ -222,9 +225,9 @@ export class BudgetProgram {
return new Transaction().add({
keys: [
{pubkey: from, isSigner: true},
{pubkey: program, isSigner: false},
{pubkey: to, isSigner: false},
{pubkey: from, isSigner: true, isDebitable: true},
{pubkey: program, isSigner: false, isDebitable: true},
{pubkey: to, isSigner: false, isDebitable: false},
],
programId: this.programId,
data: data.slice(0, pos),
@@ -246,9 +249,9 @@ export class BudgetProgram {
return new Transaction().add({
keys: [
{pubkey: from, isSigner: true},
{pubkey: program, isSigner: false},
{pubkey: to, isSigner: false},
{pubkey: from, isSigner: true, isDebitable: true},
{pubkey: program, isSigner: false, isDebitable: true},
{pubkey: to, isSigner: false, isDebitable: false},
],
programId: this.programId,
data: data.slice(0, pos),
@@ -294,9 +297,9 @@ export class BudgetProgram {
return new Transaction().add({
keys: [
{pubkey: from, isSigner: true},
{pubkey: program, isSigner: false},
{pubkey: to, isSigner: false},
{pubkey: from, isSigner: true, isDebitable: true},
{pubkey: program, isSigner: false, isDebitable: true},
{pubkey: to, isSigner: false, isDebitable: false},
],
programId: this.programId,
data: data.slice(0, pos),
@@ -321,9 +324,9 @@ export class BudgetProgram {
return new Transaction().add({
keys: [
{pubkey: from, isSigner: true},
{pubkey: program, isSigner: false},
{pubkey: to, isSigner: false},
{pubkey: from, isSigner: true, isDebitable: true},
{pubkey: program, isSigner: false, isDebitable: true},
{pubkey: to, isSigner: false, isDebitable: false},
],
programId: this.programId,
data,
@@ -351,9 +354,9 @@ export class BudgetProgram {
return new Transaction().add({
keys: [
{pubkey: from, isSigner: true},
{pubkey: program, isSigner: false},
{pubkey: to, isSigner: false},
{pubkey: from, isSigner: true, isDebitable: true},
{pubkey: program, isSigner: false, isDebitable: true},
{pubkey: to, isSigner: false, isDebitable: false},
],
programId: this.programId,
data,

View File

@@ -78,7 +78,7 @@ export class Loader {
);
const transaction = new Transaction().add({
keys: [{pubkey: program.publicKey, isSigner: true}],
keys: [{pubkey: program.publicKey, isSigner: true, isDebitable: true}],
programId,
data,
});
@@ -117,7 +117,7 @@ export class Loader {
);
const transaction = new Transaction().add({
keys: [{pubkey: program.publicKey, isSigner: true}],
keys: [{pubkey: program.publicKey, isSigner: true, isDebitable: true}],
programId,
data,
});

View File

@@ -49,8 +49,8 @@ export class SystemProgram {
return new Transaction().add({
keys: [
{pubkey: from, isSigner: true},
{pubkey: newAccount, isSigner: false},
{pubkey: from, isSigner: true, isDebitable: true},
{pubkey: newAccount, isSigner: false, isDebitable: true},
],
programId: SystemProgram.programId,
data,
@@ -76,7 +76,10 @@ export class SystemProgram {
);
return new Transaction().add({
keys: [{pubkey: from, isSigner: true}, {pubkey: to, isSigner: false}],
keys: [
{pubkey: from, isSigner: true, isDebitable: true},
{pubkey: to, isSigner: false, isDebitable: false},
],
programId: SystemProgram.programId,
data,
});
@@ -101,7 +104,7 @@ export class SystemProgram {
);
return new Transaction().add({
keys: [{pubkey: from, isSigner: true}],
keys: [{pubkey: from, isSigner: true, isDebitable: true}],
programId: SystemProgram.programId,
data,
});

View File

@@ -237,8 +237,8 @@ export class Token {
transaction = new Transaction().add({
keys: [
{pubkey: tokenAccount.publicKey, isSigner: true},
{pubkey: initialAccountPublicKey, isSigner: false},
{pubkey: tokenAccount.publicKey, isSigner: true, isDebitable: false},
{pubkey: initialAccountPublicKey, isSigner: false, isDebitable: true},
],
programId,
data,
@@ -292,12 +292,12 @@ export class Token {
// Initialize the token account
const keys = [
{pubkey: tokenAccount.publicKey, isSigner: true},
{pubkey: owner.publicKey, isSigner: false},
{pubkey: this.token, isSigner: false},
{pubkey: tokenAccount.publicKey, isSigner: true, isDebitable: true},
{pubkey: owner.publicKey, isSigner: false, isDebitable: false},
{pubkey: this.token, isSigner: false, isDebitable: false},
];
if (source) {
keys.push({pubkey: source, isSigner: false});
keys.push({pubkey: source, isSigner: false, isDebitable: false});
}
transaction = new Transaction().add({
keys,
@@ -496,12 +496,16 @@ export class Token {
);
const keys = [
{pubkey: owner, isSigner: true},
{pubkey: source, isSigner: false},
{pubkey: destination, isSigner: false},
{pubkey: owner, isSigner: true, isDebitable: false},
{pubkey: source, isSigner: false, isDebitable: true},
{pubkey: destination, isSigner: false, isDebitable: true},
];
if (accountInfo.source) {
keys.push({pubkey: accountInfo.source, isSigner: false});
keys.push({
pubkey: accountInfo.source,
isSigner: false,
isDebitable: true,
});
}
return new TransactionInstruction({
keys,
@@ -540,9 +544,9 @@ export class Token {
return new TransactionInstruction({
keys: [
{pubkey: owner, isSigner: true},
{pubkey: account, isSigner: false},
{pubkey: delegate, isSigner: false},
{pubkey: owner, isSigner: true, isDebitable: false},
{pubkey: account, isSigner: false, isDebitable: true},
{pubkey: delegate, isSigner: false, isDebitable: true},
],
programId: this.programId,
data,
@@ -588,9 +592,9 @@ export class Token {
return new TransactionInstruction({
keys: [
{pubkey: owner, isSigner: true},
{pubkey: account, isSigner: false},
{pubkey: newOwner, isSigner: false},
{pubkey: owner, isSigner: true, isDebitable: false},
{pubkey: account, isSigner: false, isDebitable: true},
{pubkey: newOwner, isSigner: false, isDebitable: true},
],
programId: this.programId,
data,

View File

@@ -30,7 +30,7 @@ export const PACKET_DATA_SIZE = 512;
* @property {?Buffer} data
*/
type TransactionInstructionCtorFields = {|
keys?: Array<{pubkey: PublicKey, isSigner: boolean}>,
keys?: Array<{pubkey: PublicKey, isSigner: boolean, isDebitable: boolean}>,
programId?: PublicKey,
data?: Buffer,
|};
@@ -43,7 +43,11 @@ export class TransactionInstruction {
* Public keys to include in this transaction
* Boolean represents whether this pubkey needs to sign the transaction
*/
keys: Array<{pubkey: PublicKey, isSigner: boolean}> = [];
keys: Array<{
pubkey: PublicKey,
isSigner: boolean,
isDebitable: boolean,
}> = [];
/**
* Program Id to execute
@@ -166,6 +170,13 @@ export class Transaction {
if (!keys.includes(keyStr)) {
if (keySignerPair.isSigner) {
numRequiredSignatures += 1;
if (!keySignerPair.isDebitable) {
numCreditOnlySignedAccounts += 1;
}
} else {
if (!keySignerPair.isDebitable) {
numCreditOnlyUnsignedAccounts += 1;
}
}
keys.push(keyStr);
}
@@ -422,6 +433,20 @@ export class Transaction {
const PUBKEY_LENGTH = 32;
const SIGNATURE_LENGTH = 64;
function isCreditDebit(
i: number,
numRequiredSignatures: number,
numCreditOnlySignedAccounts: number,
numCreditOnlyUnsignedAccounts: number,
numKeys: number,
): boolean {
return (
i < numRequiredSignatures - numCreditOnlySignedAccounts ||
(i >= numRequiredSignatures &&
i < numKeys - numCreditOnlyUnsignedAccounts)
);
}
let transaction = new Transaction();
// Slice up wire data
@@ -435,9 +460,12 @@ export class Transaction {
signatures.push(signature);
}
byteArray = byteArray.slice(1); // Skip numRequiredSignatures byte
byteArray = byteArray.slice(1); // Skip numCreditOnlySignedAccounts byte
byteArray = byteArray.slice(1); // Skip numCreditOnlyUnsignedAccounts byte
const numRequiredSignatures = byteArray.shift();
// byteArray = byteArray.slice(1); // Skip numRequiredSignatures byte
const numCreditOnlySignedAccounts = byteArray.shift();
// byteArray = byteArray.slice(1); // Skip numCreditOnlySignedAccounts byte
const numCreditOnlyUnsignedAccounts = byteArray.shift();
// byteArray = byteArray.slice(1); // Skip numCreditOnlyUnsignedAccounts byte
const accountCount = shortvec.decodeLength(byteArray);
let accounts = [];
@@ -481,11 +509,19 @@ export class Transaction {
};
for (let j = 0; j < instructions[i].accountIndex.length; j++) {
const pubkey = new PublicKey(accounts[instructions[i].accountIndex[j]]);
instructionData.keys.push({
pubkey,
isSigner: transaction.signatures.some(
keyObj => keyObj.publicKey.toString() === pubkey.toString(),
),
isDebitable: isCreditDebit(
j,
numRequiredSignatures,
numCreditOnlySignedAccounts,
numCreditOnlyUnsignedAccounts,
accounts.length,
),
});
}
let instruction = new TransactionInstruction(instructionData);