@solana/actions
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -32,11 +32,8 @@ "use strict"; | ||
*/ | ||
function createPostResponse({ fields, signers, reference, actionIdentity, }) { | ||
function createPostResponse(args) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { transaction } = fields; | ||
if (!transaction.recentBlockhash) | ||
transaction.recentBlockhash = "11111111111111111111111111111111"; | ||
// Auto-magically detect the identity keypair | ||
if (!actionIdentity) { | ||
// Auto-magically detect the identity keypair from the env | ||
if (!args.actionIdentity) { | ||
try { | ||
actionIdentity = (0, actionIdentity_js_1.getActionIdentityFromEnv)(); | ||
args.actionIdentity = (0, actionIdentity_js_1.getActionIdentityFromEnv)(); | ||
} | ||
@@ -47,3 +44,18 @@ catch (err) { | ||
} | ||
if (transaction.instructions.length <= 0) { | ||
if (args.fields.transaction instanceof web3_js_1.Transaction) { | ||
return prepareLegacyTransaction(args); | ||
} | ||
else { | ||
return prepareVersionedTransaction(args); | ||
} | ||
}); | ||
} | ||
exports.createPostResponse = createPostResponse; | ||
/** | ||
* Prepare a `VersionedTransaction` to be sent as the ActionPostResponse | ||
*/ | ||
function prepareVersionedTransaction({ fields, signers, reference, actionIdentity, }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let message = web3_js_1.TransactionMessage.decompile(fields.transaction.message); | ||
if (message.instructions.length <= 0) { | ||
throw new CreatePostResponseError("at least 1 instruction is required"); | ||
@@ -53,27 +65,59 @@ } | ||
const { instruction, reference: finalReference } = (0, actionIdentity_js_1.createActionIdentifierInstruction)(actionIdentity, reference); | ||
transaction.add(instruction); | ||
const memoId = new web3_js_1.PublicKey(constants_js_1.MEMO_PROGRAM_ID); | ||
const nonMemoIndex = transaction.instructions.findIndex((ix) => ix.programId.toBase58() !== memoId.toBase58()); | ||
if (nonMemoIndex == -1) { | ||
throw new CreatePostResponseError("transaction requires at least 1 non-memo instruction"); | ||
} | ||
transaction.instructions[nonMemoIndex].keys.push({ | ||
pubkey: actionIdentity.publicKey, | ||
isWritable: false, | ||
isSigner: false, | ||
}); | ||
transaction.instructions[nonMemoIndex].keys.push({ | ||
pubkey: finalReference, | ||
isWritable: false, | ||
isSigner: false, | ||
}); | ||
message.instructions.push(instruction); | ||
message.instructions = injectReferencesToInstructions(message.instructions, actionIdentity.publicKey, finalReference); | ||
} | ||
// recompile the message correctly based on the original version | ||
if (fields.transaction.version == "legacy") { | ||
fields.transaction.message = message.compileToLegacyMessage(); | ||
} | ||
else { | ||
fields.transaction.message = message.compileToV0Message(); | ||
} | ||
if (signers && signers.length) | ||
transaction.partialSign(...signers); | ||
fields.transaction.sign(signers); | ||
return Object.assign(fields, { | ||
transaction: Buffer.from(transaction.serialize({ requireAllSignatures: false })).toString("base64"), | ||
transaction: Buffer.from(fields.transaction.serialize()).toString("base64"), | ||
}); | ||
}); | ||
} | ||
exports.createPostResponse = createPostResponse; | ||
/** | ||
* Prepare a legacy `Transaction` to be sent as the `ActionPostResponse` | ||
*/ | ||
function prepareLegacyTransaction({ fields, signers, reference, actionIdentity, }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (fields.transaction.instructions.length <= 0) { | ||
throw new CreatePostResponseError("at least 1 instruction is required"); | ||
} | ||
if (actionIdentity) { | ||
const { instruction, reference: finalReference } = (0, actionIdentity_js_1.createActionIdentifierInstruction)(actionIdentity, reference); | ||
fields.transaction.add(instruction); | ||
fields.transaction.instructions = injectReferencesToInstructions(fields.transaction.instructions, actionIdentity.publicKey, finalReference); | ||
} | ||
if (signers && signers.length) | ||
fields.transaction.partialSign(...signers); | ||
return Object.assign(fields, { | ||
transaction: Buffer.from(fields.transaction.serialize({ requireAllSignatures: false })).toString("base64"), | ||
}); | ||
}); | ||
} | ||
function injectReferencesToInstructions(instructions, actionIdentity, reference) { | ||
// locate a non-memo instruction | ||
const memoId = new web3_js_1.PublicKey(constants_js_1.MEMO_PROGRAM_ID); | ||
const nonMemoIndex = instructions.findIndex((ix) => ix.programId.toBase58() !== memoId.toBase58()); | ||
if (nonMemoIndex == -1) { | ||
throw new CreatePostResponseError("transaction requires at least 1 non-memo instruction"); | ||
} | ||
// insert the 2 reference keys as non signers | ||
instructions[nonMemoIndex].keys.push({ | ||
pubkey: actionIdentity, | ||
isWritable: false, | ||
isSigner: false, | ||
}); | ||
instructions[nonMemoIndex].keys.push({ | ||
pubkey: reference, | ||
isWritable: false, | ||
isSigner: false, | ||
}); | ||
return instructions; | ||
} | ||
//# sourceMappingURL=createPostResponse.js.map |
@@ -1,2 +0,2 @@ | ||
import { PublicKey } from "@solana/web3.js"; | ||
import { PublicKey, TransactionMessage, Transaction, } from "@solana/web3.js"; | ||
import { MEMO_PROGRAM_ID } from "./constants.js"; | ||
@@ -19,10 +19,7 @@ import { createActionIdentifierInstruction, getActionIdentityFromEnv, } from "./actionIdentity.js"; | ||
*/ | ||
export async function createPostResponse({ fields, signers, reference, actionIdentity, }) { | ||
const { transaction } = fields; | ||
if (!transaction.recentBlockhash) | ||
transaction.recentBlockhash = "11111111111111111111111111111111"; | ||
// Auto-magically detect the identity keypair | ||
if (!actionIdentity) { | ||
export async function createPostResponse(args) { | ||
// Auto-magically detect the identity keypair from the env | ||
if (!args.actionIdentity) { | ||
try { | ||
actionIdentity = getActionIdentityFromEnv(); | ||
args.actionIdentity = getActionIdentityFromEnv(); | ||
} | ||
@@ -33,3 +30,15 @@ catch (err) { | ||
} | ||
if (transaction.instructions.length <= 0) { | ||
if (args.fields.transaction instanceof Transaction) { | ||
return prepareLegacyTransaction(args); | ||
} | ||
else { | ||
return prepareVersionedTransaction(args); | ||
} | ||
} | ||
/** | ||
* Prepare a `VersionedTransaction` to be sent as the ActionPostResponse | ||
*/ | ||
async function prepareVersionedTransaction({ fields, signers, reference, actionIdentity, }) { | ||
let message = TransactionMessage.decompile(fields.transaction.message); | ||
if (message.instructions.length <= 0) { | ||
throw new CreatePostResponseError("at least 1 instruction is required"); | ||
@@ -39,25 +48,56 @@ } | ||
const { instruction, reference: finalReference } = createActionIdentifierInstruction(actionIdentity, reference); | ||
transaction.add(instruction); | ||
const memoId = new PublicKey(MEMO_PROGRAM_ID); | ||
const nonMemoIndex = transaction.instructions.findIndex((ix) => ix.programId.toBase58() !== memoId.toBase58()); | ||
if (nonMemoIndex == -1) { | ||
throw new CreatePostResponseError("transaction requires at least 1 non-memo instruction"); | ||
} | ||
transaction.instructions[nonMemoIndex].keys.push({ | ||
pubkey: actionIdentity.publicKey, | ||
isWritable: false, | ||
isSigner: false, | ||
}); | ||
transaction.instructions[nonMemoIndex].keys.push({ | ||
pubkey: finalReference, | ||
isWritable: false, | ||
isSigner: false, | ||
}); | ||
message.instructions.push(instruction); | ||
message.instructions = injectReferencesToInstructions(message.instructions, actionIdentity.publicKey, finalReference); | ||
} | ||
// recompile the message correctly based on the original version | ||
if (fields.transaction.version == "legacy") { | ||
fields.transaction.message = message.compileToLegacyMessage(); | ||
} | ||
else { | ||
fields.transaction.message = message.compileToV0Message(); | ||
} | ||
if (signers && signers.length) | ||
transaction.partialSign(...signers); | ||
fields.transaction.sign(signers); | ||
return Object.assign(fields, { | ||
transaction: Buffer.from(transaction.serialize({ requireAllSignatures: false })).toString("base64"), | ||
transaction: Buffer.from(fields.transaction.serialize()).toString("base64"), | ||
}); | ||
} | ||
/** | ||
* Prepare a legacy `Transaction` to be sent as the `ActionPostResponse` | ||
*/ | ||
async function prepareLegacyTransaction({ fields, signers, reference, actionIdentity, }) { | ||
if (fields.transaction.instructions.length <= 0) { | ||
throw new CreatePostResponseError("at least 1 instruction is required"); | ||
} | ||
if (actionIdentity) { | ||
const { instruction, reference: finalReference } = createActionIdentifierInstruction(actionIdentity, reference); | ||
fields.transaction.add(instruction); | ||
fields.transaction.instructions = injectReferencesToInstructions(fields.transaction.instructions, actionIdentity.publicKey, finalReference); | ||
} | ||
if (signers && signers.length) | ||
fields.transaction.partialSign(...signers); | ||
return Object.assign(fields, { | ||
transaction: Buffer.from(fields.transaction.serialize({ requireAllSignatures: false })).toString("base64"), | ||
}); | ||
} | ||
function injectReferencesToInstructions(instructions, actionIdentity, reference) { | ||
// locate a non-memo instruction | ||
const memoId = new PublicKey(MEMO_PROGRAM_ID); | ||
const nonMemoIndex = instructions.findIndex((ix) => ix.programId.toBase58() !== memoId.toBase58()); | ||
if (nonMemoIndex == -1) { | ||
throw new CreatePostResponseError("transaction requires at least 1 non-memo instruction"); | ||
} | ||
// insert the 2 reference keys as non signers | ||
instructions[nonMemoIndex].keys.push({ | ||
pubkey: actionIdentity, | ||
isWritable: false, | ||
isSigner: false, | ||
}); | ||
instructions[nonMemoIndex].keys.push({ | ||
pubkey: reference, | ||
isWritable: false, | ||
isSigner: false, | ||
}); | ||
return instructions; | ||
} | ||
//# sourceMappingURL=createPostResponse.js.map |
@@ -1,3 +0,2 @@ | ||
import { Commitment, Signer } from "@solana/web3.js"; | ||
import { Transaction } from "@solana/web3.js"; | ||
import { Commitment, Signer, VersionedTransaction, Transaction } from "@solana/web3.js"; | ||
import type { Reference } from "./types.js"; | ||
@@ -14,7 +13,7 @@ import { ActionPostResponse } from "@solana/actions-spec"; | ||
*/ | ||
export interface CreateActionPostResponseArgs { | ||
export interface CreateActionPostResponseArgs<TransactionType = Transaction | VersionedTransaction> { | ||
/** POST response fields per the Solana Actions spec. */ | ||
fields: Omit<ActionPostResponse, "transaction"> & { | ||
/** Solana transaction to be base64 encoded. */ | ||
transaction: Transaction; | ||
transaction: TransactionType; | ||
}; | ||
@@ -38,3 +37,3 @@ /** Optional signers that will sign the transaction. */ | ||
*/ | ||
export declare function createPostResponse({ fields, signers, reference, actionIdentity, }: CreateActionPostResponseArgs): Promise<ActionPostResponse>; | ||
export declare function createPostResponse(args: CreateActionPostResponseArgs): Promise<ActionPostResponse>; | ||
//# sourceMappingURL=createPostResponse.d.ts.map |
{ | ||
"name": "@solana/actions", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"author": "Solana Maintainers <maintainers@solana.foundation>", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/solana-developers/solana-actions", |
@@ -1,3 +0,10 @@ | ||
import { Commitment, PublicKey, Signer } from "@solana/web3.js"; | ||
import { Transaction } from "@solana/web3.js"; | ||
import { | ||
Commitment, | ||
PublicKey, | ||
Signer, | ||
TransactionInstruction, | ||
TransactionMessage, | ||
VersionedTransaction, | ||
Transaction, | ||
} from "@solana/web3.js"; | ||
import type { Reference } from "./types.js"; | ||
@@ -21,7 +28,9 @@ import { MEMO_PROGRAM_ID } from "./constants.js"; | ||
*/ | ||
export interface CreateActionPostResponseArgs { | ||
export interface CreateActionPostResponseArgs< | ||
TransactionType = Transaction | VersionedTransaction, | ||
> { | ||
/** POST response fields per the Solana Actions spec. */ | ||
fields: Omit<ActionPostResponse, "transaction"> & { | ||
/** Solana transaction to be base64 encoded. */ | ||
transaction: Transaction; | ||
transaction: TransactionType; | ||
}; | ||
@@ -44,3 +53,29 @@ /** Optional signers that will sign the transaction. */ | ||
*/ | ||
export async function createPostResponse({ | ||
export async function createPostResponse( | ||
args: CreateActionPostResponseArgs, | ||
): Promise<ActionPostResponse> { | ||
// Auto-magically detect the identity keypair from the env | ||
if (!args.actionIdentity) { | ||
try { | ||
args.actionIdentity = getActionIdentityFromEnv(); | ||
} catch (err) { | ||
// do nothing | ||
} | ||
} | ||
if (args.fields.transaction instanceof Transaction) { | ||
return prepareLegacyTransaction( | ||
args as CreateActionPostResponseArgs<Transaction>, | ||
); | ||
} else { | ||
return prepareVersionedTransaction( | ||
args as CreateActionPostResponseArgs<VersionedTransaction>, | ||
); | ||
} | ||
} | ||
/** | ||
* Prepare a `VersionedTransaction` to be sent as the ActionPostResponse | ||
*/ | ||
async function prepareVersionedTransaction({ | ||
fields, | ||
@@ -50,18 +85,45 @@ signers, | ||
actionIdentity, | ||
}: CreateActionPostResponseArgs): Promise<ActionPostResponse> { | ||
const { transaction } = fields; | ||
}: CreateActionPostResponseArgs<VersionedTransaction>): Promise<ActionPostResponse> { | ||
let message = TransactionMessage.decompile(fields.transaction.message); | ||
if (!transaction.recentBlockhash) | ||
transaction.recentBlockhash = "11111111111111111111111111111111"; | ||
if (message.instructions.length <= 0) { | ||
throw new CreatePostResponseError("at least 1 instruction is required"); | ||
} | ||
// Auto-magically detect the identity keypair | ||
if (!actionIdentity) { | ||
try { | ||
actionIdentity = getActionIdentityFromEnv(); | ||
} catch (err) { | ||
// do nothing | ||
} | ||
if (actionIdentity) { | ||
const { instruction, reference: finalReference } = | ||
createActionIdentifierInstruction(actionIdentity, reference); | ||
message.instructions.push(instruction); | ||
message.instructions = injectReferencesToInstructions( | ||
message.instructions, | ||
actionIdentity.publicKey, | ||
finalReference, | ||
); | ||
} | ||
if (transaction.instructions.length <= 0) { | ||
// recompile the message correctly based on the original version | ||
if (fields.transaction.version == "legacy") { | ||
fields.transaction.message = message.compileToLegacyMessage(); | ||
} else { | ||
fields.transaction.message = message.compileToV0Message(); | ||
} | ||
if (signers && signers.length) fields.transaction.sign(signers); | ||
return Object.assign(fields, { | ||
transaction: Buffer.from(fields.transaction.serialize()).toString("base64"), | ||
}); | ||
} | ||
/** | ||
* Prepare a legacy `Transaction` to be sent as the `ActionPostResponse` | ||
*/ | ||
async function prepareLegacyTransaction({ | ||
fields, | ||
signers, | ||
reference, | ||
actionIdentity, | ||
}: CreateActionPostResponseArgs<Transaction>): Promise<ActionPostResponse> { | ||
if (fields.transaction.instructions.length <= 0) { | ||
throw new CreatePostResponseError("at least 1 instruction is required"); | ||
@@ -73,33 +135,50 @@ } | ||
createActionIdentifierInstruction(actionIdentity, reference); | ||
transaction.add(instruction); | ||
const memoId = new PublicKey(MEMO_PROGRAM_ID); | ||
const nonMemoIndex = transaction.instructions.findIndex( | ||
(ix) => ix.programId.toBase58() !== memoId.toBase58(), | ||
fields.transaction.add(instruction); | ||
fields.transaction.instructions = injectReferencesToInstructions( | ||
fields.transaction.instructions, | ||
actionIdentity.publicKey, | ||
finalReference, | ||
); | ||
if (nonMemoIndex == -1) { | ||
throw new CreatePostResponseError( | ||
"transaction requires at least 1 non-memo instruction", | ||
); | ||
} | ||
transaction.instructions[nonMemoIndex].keys.push({ | ||
pubkey: actionIdentity.publicKey, | ||
isWritable: false, | ||
isSigner: false, | ||
}); | ||
transaction.instructions[nonMemoIndex].keys.push({ | ||
pubkey: finalReference, | ||
isWritable: false, | ||
isSigner: false, | ||
}); | ||
} | ||
if (signers && signers.length) transaction.partialSign(...signers); | ||
if (signers && signers.length) fields.transaction.partialSign(...signers); | ||
return Object.assign(fields, { | ||
transaction: Buffer.from( | ||
transaction.serialize({ requireAllSignatures: false }), | ||
fields.transaction.serialize({ requireAllSignatures: false }), | ||
).toString("base64"), | ||
}); | ||
} | ||
function injectReferencesToInstructions( | ||
instructions: TransactionInstruction[], | ||
actionIdentity: PublicKey, | ||
reference: PublicKey, | ||
): TransactionInstruction[] { | ||
// locate a non-memo instruction | ||
const memoId = new PublicKey(MEMO_PROGRAM_ID); | ||
const nonMemoIndex = instructions.findIndex( | ||
(ix) => ix.programId.toBase58() !== memoId.toBase58(), | ||
); | ||
if (nonMemoIndex == -1) { | ||
throw new CreatePostResponseError( | ||
"transaction requires at least 1 non-memo instruction", | ||
); | ||
} | ||
// insert the 2 reference keys as non signers | ||
instructions[nonMemoIndex].keys.push({ | ||
pubkey: actionIdentity, | ||
isWritable: false, | ||
isSigner: false, | ||
}); | ||
instructions[nonMemoIndex].keys.push({ | ||
pubkey: reference, | ||
isWritable: false, | ||
isSigner: false, | ||
}); | ||
return instructions; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
140240
2162