Add StakeInstruction::AuthorizeWithSeed (#11700)

* Add StakeInstruction::AuthorizeWithSeed

* chore: add authorize-with-seed to web.js

* fix: add address_owner

* Add SystemInstruction::TransferWithSeed

* Update ABI hash

* chore: better variable names

* Add AuthorizeWithSeedArgs

* Reorder and rename arguments for clarity
This commit is contained in:
Greg Fitzgerald
2020-08-21 12:28:01 -06:00
committed by GitHub
parent 247f27af37
commit f02a78d8ff
9 changed files with 469 additions and 40 deletions

View File

@ -124,6 +124,25 @@ export type AuthorizeStakeParams = {|
stakeAuthorizationType: StakeAuthorizationType,
|};
/**
* Authorize stake instruction params using a derived key
* @typedef {Object} AuthorizeWithSeedStakeParams
* @property {PublicKey} stakePubkey
* @property {PublicKey} authorityBase
* @property {string} authoritySeed
* @property {PublicKey} authorityOwner
* @property {PublicKey} newAuthorizedPubkey
* @property {StakeAuthorizationType} stakeAuthorizationType
*/
export type AuthorizeWithSeedStakeParams = {|
stakePubkey: PublicKey,
authorityBase: PublicKey,
authoritySeed: string,
authorityOwner: PublicKey,
newAuthorizedPubkey: PublicKey,
stakeAuthorizationType: StakeAuthorizationType,
|};
/**
* Split stake instruction params
* @typedef {Object} SplitStakeParams
@ -262,6 +281,31 @@ export class StakeInstruction {
};
}
/**
* Decode an authorize-with-seed stake instruction and retrieve the instruction params.
*/
static decodeAuthorizeWithSeed(
instruction: TransactionInstruction,
): AuthorizeWithSeedStakeParams {
this.checkProgramId(instruction.programId);
this.checkKeyLength(instruction.keys, 2);
const {newAuthorized, stakeAuthorizationType, authoritySeed, authorityOwner} = decodeData(
STAKE_INSTRUCTION_LAYOUTS.AuthorizeWithSeed,
instruction.data,
);
return {
stakePubkey: instruction.keys[0].pubkey,
authorityBase: instruction.keys[1].pubkey,
authoritySeed: authoritySeed,
authorityOwner: new PublicKey(authorityOwner),
newAuthorizedPubkey: new PublicKey(newAuthorized),
stakeAuthorizationType: {
index: stakeAuthorizationType,
},
};
}
/**
* Decode a split stake instruction and retrieve the instruction params.
*/
@ -341,7 +385,7 @@ export class StakeInstruction {
/**
* An enumeration of valid StakeInstructionType's
* @typedef { 'Initialize' | 'Authorize' | 'Delegate' | 'Split' | 'Withdraw'
* @typedef { 'Initialize' | 'Authorize' | 'AuthorizeWithSeed' | 'Delegate' | 'Split' | 'Withdraw'
| 'Deactivate' } StakeInstructionType
*/
export type StakeInstructionType = $Keys<typeof STAKE_INSTRUCTION_LAYOUTS>;
@ -388,6 +432,16 @@ export const STAKE_INSTRUCTION_LAYOUTS = Object.freeze({
index: 5,
layout: BufferLayout.struct([BufferLayout.u32('instruction')]),
},
AuthorizeWithSeed: {
index: 8,
layout: BufferLayout.struct([
BufferLayout.u32('instruction'),
Layout.publicKey('newAuthorized'),
BufferLayout.u32('stakeAuthorizationType'),
Layout.rustString('authoritySeed'),
Layout.publicKey('authorityOwner'),
]),
},
});
/**
@ -551,6 +605,38 @@ export class StakeProgram {
});
}
/**
* Generate a Transaction that authorizes a new PublicKey as Staker
* or Withdrawer on the Stake account.
*/
static authorizeWithSeed(params: AuthorizeWithSeedStakeParams): Transaction {
const {
stakePubkey,
authorityBase,
authoritySeed,
authorityOwner,
newAuthorizedPubkey,
stakeAuthorizationType,
} = params;
const type = STAKE_INSTRUCTION_LAYOUTS.AuthorizeWithSeed;
const data = encodeData(type, {
newAuthorized: newAuthorizedPubkey.toBuffer(),
stakeAuthorizationType: stakeAuthorizationType.index,
authoritySeed: authoritySeed,
authorityOwner: authorityOwner.toBuffer(),
});
return new Transaction().add({
keys: [
{pubkey: stakePubkey, isSigner: false, isWritable: true},
{pubkey: authorityBase, isSigner: true, isWritable: false},
],
programId: this.programId,
data,
});
}
/**
* Generate a Transaction that splits Stake tokens into another stake account
*/