Skip to content

Instantly share code, notes, and snippets.

@256hax
Created March 3, 2024 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 256hax/d1815fae9f573623085242d0a0210cb2 to your computer and use it in GitHub Desktop.
Save 256hax/d1815fae9f573623085242d0a0210cb2 to your computer and use it in GitHub Desktop.
// Docs:
// https://docs.gameshift.dev/docs/arbitrary-transaction-signing
// https://docs.gameshift.dev/reference/arbitrarytransactionsigningcontroller_signtransaction
import * as dotenv from 'dotenv';
import * as bs58 from 'bs58';
import axios from 'axios';
import { v4 as uuidv4 } from 'uuid';
import {
Connection,
PublicKey,
SystemProgram,
Transaction,
LAMPORTS_PER_SOL,
} from '@solana/web3.js';
export const signTransactionForUser = async () => {
// --------------------------
// Setup API Key
// --------------------------
dotenv.config();
const gameshiftApiKey = process.env.GAMESHIFT_API_KEY;
if (!gameshiftApiKey) throw new Error('gameshiftApiKey not found.');
// --------------------------
// Setup Transaction
// --------------------------
// User Wallet on GameShift
const payerPublicKey = new PublicKey(
'Dub7RWQomW86ctP1NQDdZ5enUExbJt46Fve8be9cWuD8'
);
const takerPublicKey = new PublicKey(
'HXtBm8XZbxaTt41uqaKhwUAa6Z1aPyvJdsZVENiWsetg'
);
const connection = new Connection(
'https://api.devnet.solana.com',
'confirmed'
);
const latestBlockhash = await connection.getLatestBlockhash();
let transaction = new Transaction({
blockhash: latestBlockhash.blockhash,
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
feePayer: payerPublicKey,
});
transaction.add(
SystemProgram.transfer({
fromPubkey: payerPublicKey,
toPubkey: takerPublicKey,
lamports: LAMPORTS_PER_SOL * 0.0001,
})
);
// --------------------------
// Request to GameShift
// --------------------------
const url = 'https://api.gameshift.dev/transactions/sign';
const userReferenceId = '47e33b63-ad72-4968-aace-f5ce0e3fb3a8';
// serializedTransaction:
// This is the transaction you are requesting a signature for. The transaction must be serialized into a hex string. In addition, you must serialize just the Message within the transaction. As an example, if you have a transaction you created using @solana/web3.js, you could create the value for this field by calling transaction.serializeMessage().toString(‘hex’).
//
// onBehalfOf:
// The reference id of the user you are requesting a transaction signature from.
const data = {
serializedTransaction: bs58.encode(transaction.serializeMessage()),
onBehalfOf: userReferenceId,
};
const config = {
headers: {
accept: 'application/json',
'x-api-key': gameshiftApiKey,
},
};
try {
const res = await axios.post(url, data, config);
console.log(res.data);
} catch (e) {
console.log('%o', e);
}
};
signTransactionForUser();
/*
% ts-node src/<THIS_FILE>
{
url: 'https://app.gameshift.dev/consent?transaction=fb5bf485-9f03-401f-abbd-53b6ddc18635'
}
*/
@256hax
Copy link
Author

256hax commented Mar 3, 2024

I runned this script then I got following response.

{
  url: 'https://app.gameshift.dev/consent?transaction=fb5bf485-9f03-401f-abbd-53b6ddc18635'
}

Then I accessed above URL, I got error "Something went wrong".

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment