Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add option to generate unsigned tx for publishing package #65

Closed
wants to merge 4 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 36 additions & 17 deletions scripts/publish-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ const { setConfig, getFullObject, requestSuiFromFaucet } = require('./utils');
const { Ed25519Keypair } = require('@mysten/sui.js/keypairs/ed25519');
const { TransactionBlock } = require('@mysten/sui.js/transactions');
const { SuiClient } = require('@mysten/sui.js/client');
const { toB64 } = require('@mysten/sui.js/utils');
const { execSync } = require('child_process');
const { parseEnv } = require('./utils');
const tmp = require('tmp');
const fs = require('fs');
const path = require('path');

async function publishPackage(packageName, client, keypair) {
async function getContractBuild(packageName) {
const toml = fs.readFileSync(`${__dirname}/../move/${packageName}/Move.toml`, 'utf8');
fs.writeFileSync(`${__dirname}/../move/${packageName}/Move.toml`, fillAddresses(toml, '0x0', packageName));

// remove all controlled temporary objects on process exit
const address = keypair.getPublicKey().toSuiAddress();
tmp.setGracefulCleanup();

const tmpobj = tmp.dirSync({ unsafeCleanup: true });

const { modules, dependencies } = JSON.parse(
return JSON.parse(
blockchainguyy marked this conversation as resolved.
Show resolved Hide resolved
execSync(
`sui move build --dump-bytecode-as-base64 --path ${path.join(__dirname, '/../move/', packageName)} --install-dir ${
tmpobj.name
Expand All @@ -30,6 +30,10 @@ async function publishPackage(packageName, client, keypair) {
},
),
);
}

async function publishPackage(packageName, client, keypair, options = {}) {
const { modules, dependencies } = await getContractBuild(packageName);

const tx = new TransactionBlock();
const cap = tx.publish({
Expand All @@ -38,25 +42,39 @@ async function publishPackage(packageName, client, keypair) {
});

// Transfer the upgrade capability to the sender so they can upgrade the package later if they want.
let address;

if (!options.sender) {
address = keypair.getPublicKey().toSuiAddress();
} else {
address = options.sender;
}
blockchainguyy marked this conversation as resolved.
Show resolved Hide resolved

tx.transferObjects([cap], tx.pure(address));

const publishTxn = await client.signAndExecuteTransactionBlock({
transactionBlock: tx,
signer: keypair,
options: {
showEffects: true,
showObjectChanges: true,
showContent: true,
},
requestType: 'WaitForLocalExecution',
});
if (publishTxn.effects?.status.status !== 'success') throw new Error('Publish Tx failed');
if (!options.offline) {
blockchainguyy marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the use case for offline support for publishing packages? similar to evm, i don't see why we're offline signing deployments as opposed to other txs. I'm ok to add it since it's a small change and it's already ready, but otherwise best to avoid spending time on use cases we don't need

const publishTxn = await client.signAndExecuteTransactionBlock({
transactionBlock: tx,
signer: keypair,
options: {
showEffects: true,
showObjectChanges: true,
showContent: true,
},
requestType: 'WaitForLocalExecution',
});
if (publishTxn.effects?.status.status !== 'success') throw new Error('Publish Tx failed');

const packageId = (publishTxn.objectChanges?.filter((a) => a.type === 'published') ?? [])[0].packageId;
const packageId = (publishTxn.objectChanges?.filter((a) => a.type === 'published') ?? [])[0].packageId;

console.info(`Published package ${packageId} from address ${address}}`);
console.info(`Published package ${packageId} from address ${address}}`);

return { packageId, publishTxn };
return { packageId, publishTxn };
}

tx.setSenderIfNotSet(address);
const txBytes = await tx.build({ client });
return toB64(txBytes);
blockchainguyy marked this conversation as resolved.
Show resolved Hide resolved
}

function updateMoveToml(packageName, packageId) {
Expand Down Expand Up @@ -116,6 +134,7 @@ module.exports = {
publishPackage,
updateMoveToml,
publishPackageFull,
getContractBuild,
};

if (require.main === module) {
Expand Down
Loading