Skip to content

Commit

Permalink
add publishing to typescript sdk (MystenLabs#3084)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Li <[email protected]>
  • Loading branch information
ncitron and 666lcz authored Jul 12, 2022
1 parent 388f2dd commit ec8e376
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 4 deletions.
21 changes: 21 additions & 0 deletions sdk/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,24 @@ const moveCallTxn = await signer.executeMoveCall({
});
console.log('moveCallTxn', moveCallTxn);
```

To publish a package:

```typescript
import { Ed25519Keypair, JsonRpcProvider, RawSigner } from '@mysten/sui.js';
import * as fs from 'fs/promises';
// Generate a new Keypair
const keypair = new Ed25519Keypair();
const signer = new RawSigner(
keypair,
new JsonRpcProvider('https://gateway.devnet.sui.io:443')
);
const bytecode = await fs.readFile('path/to/project/build/project_name/bytecode_modules/module_name.mv', 'base64');
const publishTxn = await signer.publish(
{
compiledModules: [bytecode.toString()],
gasBudget: 1000
}
);
console.log('publishTxn', publishTxn);
```
52 changes: 49 additions & 3 deletions sdk/typescript/src/index.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Generated type guards for "index.ts".
* WARNING: Do not manually change this file.
*/
import { Ed25519KeypairData, Keypair, PublicKeyInitData, PublicKeyData, TransferObjectTransaction, MergeCoinTransaction, SplitCoinTransaction, MoveCallTransaction, TxnDataSerializer, SignaturePubkeyPair, Signer, TransactionDigest, SuiAddress, ObjectOwner, SuiObjectRef, SuiObjectInfo, ObjectContentFields, MovePackageContent, SuiData, SuiMoveObject, SuiMovePackage, SuiObject, ObjectStatus, ObjectType, GetOwnedObjectsResponse, GetObjectDataResponse, ObjectDigest, ObjectId, SequenceNumber, TransferObject, TransactionKindName, SuiTransactionKind, TransactionData, EpochId, AuthorityQuorumSignInfo, CertifiedTransaction, GasCostSummary, ExecutionStatusType, ExecutionStatus, OwnedObjectRef, TransactionEffects, TransactionEffectsResponse, GatewayTxSeqNumber, GetTxnDigestsResponse, MoveCall, SuiJsonValue, EmptySignInfo, AuthorityName, AuthoritySignature, TransactionBytes, MergeCoinResponse, SplitCoinResponse, TransactionResponse } from "./index";
import { Ed25519KeypairData, Keypair, PublicKeyInitData, PublicKeyData, TransferObjectTransaction, MergeCoinTransaction, SplitCoinTransaction, MoveCallTransaction, PublishTransaction, TxnDataSerializer, SignaturePubkeyPair, Signer, TransactionDigest, SuiAddress, ObjectOwner, SuiObjectRef, SuiObjectInfo, ObjectContentFields, MovePackageContent, SuiData, SuiMoveObject, SuiMovePackage, SuiObject, ObjectStatus, ObjectType, GetOwnedObjectsResponse, GetObjectDataResponse, ObjectDigest, ObjectId, SequenceNumber, TransferObject, TransactionKindName, SuiTransactionKind, TransactionData, EpochId, AuthorityQuorumSignInfo, CertifiedTransaction, GasCostSummary, ExecutionStatusType, ExecutionStatus, OwnedObjectRef, TransactionEffects, TransactionEffectsResponse, GatewayTxSeqNumber, GetTxnDigestsResponse, MoveCall, SuiJsonValue, EmptySignInfo, AuthorityName, AuthoritySignature, TransactionBytes, MergeCoinResponse, SplitCoinResponse, PublishResponse, SuiPackage, TransactionResponse } from "./index";
import { BN } from "bn.js";
import { Base64DataBuffer } from "./serialization/base64";
import { PublicKey } from "./cryptography/publickey";
Expand Down Expand Up @@ -117,6 +117,21 @@ export function isMoveCallTransaction(obj: any, _argumentName?: string): obj is
)
}

export function isPublishTransaction(obj: any, _argumentName?: string): obj is PublishTransaction {
return (
(obj !== null &&
typeof obj === "object" ||
typeof obj === "function") &&
Array.isArray(obj.compiledModules) &&
obj.compiledModules.every((e: any) =>
isTransactionDigest(e) as boolean
) &&
(typeof obj.gasPayment === "undefined" ||
isTransactionDigest(obj.gasPayment) as boolean) &&
isSequenceNumber(obj.gasBudget) as boolean
)
}

export function isTxnDataSerializer(obj: any, _argumentName?: string): obj is TxnDataSerializer {
return (
(obj !== null &&
Expand All @@ -125,7 +140,8 @@ export function isTxnDataSerializer(obj: any, _argumentName?: string): obj is Tx
typeof obj.newTransferObject === "function" &&
typeof obj.newMoveCall === "function" &&
typeof obj.newMergeCoin === "function" &&
typeof obj.newSplitCoin === "function"
typeof obj.newSplitCoin === "function" &&
typeof obj.newPublish === "function"
)
}

Expand Down Expand Up @@ -615,6 +631,32 @@ export function isSplitCoinResponse(obj: any, _argumentName?: string): obj is Sp
)
}

export function isPublishResponse(obj: any, _argumentName?: string): obj is PublishResponse {
return (
(obj !== null &&
typeof obj === "object" ||
typeof obj === "function") &&
isCertifiedTransaction(obj.certificate) as boolean &&
Array.isArray(obj.createdObjects) &&
obj.createdObjects.every((e: any) =>
isSuiObject(e) as boolean
) &&
isSuiPackage(obj.package) as boolean &&
isSuiObject(obj.updatedGas) as boolean
)
}

export function isSuiPackage(obj: any, _argumentName?: string): obj is SuiPackage {
return (
(obj !== null &&
typeof obj === "object" ||
typeof obj === "function") &&
isTransactionDigest(obj.digest) as boolean &&
isTransactionDigest(obj.objectId) as boolean &&
isSequenceNumber(obj.version) as boolean
)
}

export function isTransactionResponse(obj: any, _argumentName?: string): obj is TransactionResponse {
return (
((obj !== null &&
Expand All @@ -628,6 +670,10 @@ export function isTransactionResponse(obj: any, _argumentName?: string): obj is
(obj !== null &&
typeof obj === "object" ||
typeof obj === "function") &&
isMergeCoinResponse(obj.MergeCoinResponse) as boolean)
isMergeCoinResponse(obj.MergeCoinResponse) as boolean ||
(obj !== null &&
typeof obj === "object" ||
typeof obj === "function") &&
isPublishResponse(obj.PublishResponse) as boolean)
)
}
12 changes: 12 additions & 0 deletions sdk/typescript/src/signers/signer-with-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
SplitCoinTransaction,
TransferObjectTransaction,
TxnDataSerializer,
PublishTransaction,
} from './txn-data-serializers/txn-data-serializer';

///////////////////////////////
Expand Down Expand Up @@ -120,4 +121,15 @@ export abstract class SignerWithProvider implements Signer {
);
return await this.signAndExecuteTransaction(txBytes);
}

async publish(
transaction: PublishTransaction
): Promise<TransactionResponse> {
const signerAddress = await this.getAddress();
const txBytes = await this.serializer.newPublish(
signerAddress,
transaction
);
return await this.signAndExecuteTransaction(txBytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
MergeCoinTransaction,
SplitCoinTransaction,
TransferObjectTransaction,
PublishTransaction,
TxnDataSerializer,
} from './txn-data-serializer';

Expand Down Expand Up @@ -116,4 +117,25 @@ export class RpcTxnDataSerializer implements TxnDataSerializer {
throw new Error(`Error splitting coin: ${err}`);
}
}

async newPublish(
signerAddress: SuiAddress,
t: PublishTransaction
): Promise<Base64DataBuffer> {
try {
const resp = await this.client.requestWithType(
'sui_publish',
[
signerAddress,
t.compiledModules,
t.gasPayment,
t.gasBudget,
],
isTransactionBytes
);
return new Base64DataBuffer(resp.txBytes);
} catch (err) {
throw new Error(`Error publishing package ${err}`);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export interface MoveCallTransaction {
gasBudget: number;
}

export interface PublishTransaction {
compiledModules: string[],
gasPayment?: ObjectId;
gasBudget: number;
}


///////////////////////////////
// Exported Abstracts
/**
Expand All @@ -62,4 +69,9 @@ export interface TxnDataSerializer {
signerAddress: SuiAddress,
txn: SplitCoinTransaction
): Promise<Base64DataBuffer>;

newPublish(
signerAddress: SuiAddress,
txn: PublishTransaction
): Promise<Base64DataBuffer>;
}
23 changes: 22 additions & 1 deletion sdk/typescript/src/types/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,31 @@ export type SplitCoinResponse = {
updatedGas: SuiObject;
};

export type PublishResponse = {
certificate: CertifiedTransaction;
createdObjects: SuiObject[];
package: SuiPackage;
updatedGas: SuiObject;
}

export type SuiPackage = {
digest: string;
objectId: string;
version: number;
}

export type TransactionResponse =
| {
EffectResponse: TransactionEffectsResponse;
// TODO: Add Publish Response
}
| {
SplitCoinResponse: SplitCoinResponse;
}
| {
MergeCoinResponse: MergeCoinResponse;
}
| {
PublishResponse: PublishResponse;
};

/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -273,6 +288,12 @@ export function getMergeCoinResponse(
return 'MergeCoinResponse' in data ? data.MergeCoinResponse : undefined;
}

export function getPublishResponse(
data: TransactionResponse
): PublishResponse | undefined {
return 'PublishResponse' in data ? data.PublishResponse : undefined;
}

/**
* Get the updated coin after a merge.
* @param data the response for executing a merge coin transaction
Expand Down

0 comments on commit ec8e376

Please sign in to comment.