Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
1. add encodeStarcoinTypesPackage -> return starcoion_types.Package
2. add encodeStarcoinTypesScriptFunction -> return starcoion_types.ScriptFunction
3. keep encodePackage -> return starcoion_types.TransactionPayloadVariantPackage, for backward compatibility
4. keep encodeScriptFunction -> return starcoion_types.TransactionPayloadVariantScriptFunction, for backward compatibility
  • Loading branch information
wk3368 committed May 16, 2022
1 parent 963b2cf commit 8866795
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 18 deletions.
Binary file added src/encoding/data/TestToken.mv
Binary file not shown.
81 changes: 78 additions & 3 deletions src/encoding/index.spec.ts
Expand Up @@ -9,11 +9,11 @@ import {
decodeReceiptIdentifier, bytesToString, stringToBytes, bcsEncode
} from '.';
import { BcsSerializer, BcsDeserializer } from '../lib/runtime/bcs';
import { TransactionArgumentVariantU128 } from '../lib/runtime/starcoin_types';
import * as starcoin_types from '../lib/runtime/starcoin_types';
import { toHexString } from '../utils/hex';
import { JsonRpcProvider } from '../providers/jsonrpc-provider';
import {
encodeScriptFunction, generateRawUserTransaction, signRawUserTransaction, encodeStructTypeTags, encodeScriptFunctionByResolve
encodePackage, encodeScriptFunction, generateRawUserTransaction, signRawUserTransaction, encodeStructTypeTags, encodeScriptFunctionByResolve
} from "../utils/tx";

test("encoding token", () => {
Expand Down Expand Up @@ -513,7 +513,7 @@ test("encoding SignedUserTransaction hex, 0x1::TransferScripts::peer_to_peer", a
const signedUserTransactionDecoded = decodeSignedUserTransaction(hex);

expect(signedUserTransactionDecoded.raw_txn.sender).toBe(senderAddressHex);
});
}, 10000);

test("decoding SignedUserTransaction hex", () => {
const hex = "0x49624992dd72da077ee19d0be210406a100000000000000002000000000000000000000000000000010f5472616e73666572536372697074730c706565725f746f5f706565720107000000000000000000000000000000010353544303535443000310621500bf2b4aad17a690cb24f9a225c601001000ca9a3b0000000000000000000000001fe501000000000001000000000000000d3078313a3a5354433a3a535443e8ab000000000000fe002020e2c9a32b0ce41c3a5f4a5f010909741f12e265debcb681c9f9d58c2e69e65c4040288d5662f0f0e72d181073c00bd4f6a15fcfaf4911f6ac35827e09c5e6e02d0df0f2d1bb81c90617911362a39801b88cf6ae405ef226c2e6645a1e5c946e09";
Expand Down Expand Up @@ -686,3 +686,78 @@ test('encode/decode data:image/png;base64', () => {
expect(imageHex).toBe(imageHexExpected)
expect(Buffer.from(imageBytes).toString()).toBe(imageData)
});

test('encodePackage', async () => {
const address = '0xedb4a7199ae49f76991614CF4C39c585'
const privateKey = '0a4a0fe4985df2590ac59c208775f36438a47193ce6eeb197964d8a8f8a6a1f9'

// There are two ways to generate TransactionPayloadVariantPackage from *.move

// Option 1:
// First, in Terminal,execute following commands:
// ➜ ~/work/my-token $mpm sandbox publish
// ➜ ~/work/my-token$ cp storage/0x0xedb4a7199ae49f76991614cf4c39c585/modules/TestToken.mv ../starcoin.js/src/encoding/data/
const buffer = fs.readFileSync(
path.join(__dirname, "data", "TestToken.mv")
);
const fileBytes = new Uint8Array(buffer)
const fileHex = hexlify(fileBytes)
const transactionPayload = encodePackage(
address,
[fileHex],
{
functionId: `${ address }::TestToken::init`,
tyArgs: encodeStructTypeTags([]),
args: [],
},
);


// Option 2:
// First, in Terminal,execute following commands:
// ➜ ~/work/my-token $mpm release --function 0xedb4a7199ae49f76991614CF4C39c585::TestToken::init
// Packaging Modules:
// 0xedb4a7199ae49f76991614cf4c39c585::TestToken
// Release done: release/my_token.v0.0.1.blob, package hash: 0x1ad1d9f2a97964f722fae2e157ef6c316625c269998c6127e0f9c3871b3e1438
// ➜ ~/work/my-token $hexdump -v -e '1/1 "%02x"' release/my_token.v0.0.1.blob
// edb4a7199ae49f76991614cf4c39c58501aa01a11ceb0b040000000901000402040403080b04130205150a071f340853200a73050c780d000001010002070000030001000105030101040102010c0001080002060c020954657374546f6b656e05546f6b656e0355534404696e69740b64756d6d795f6669656c640e72656769737465725f746f6b656eedb4a7199ae49f76991614cf4c39c5850000000000000000000000000000000100020104010002000001040e0031093800020001edb4a7199ae49f76991614cf4c39c5850954657374546f6b656e04696e69740000
// Then, copy and past the output hex string as the parameter moduleHex's value().

// const moduleHex = 'edb4a7199ae49f76991614cf4c39c58501aa01a11ceb0b040000000901000402040403080b04130205150a071f340853200a73050c780d000001010002070000030001000105030101040102010c0001080002060c020954657374546f6b656e05546f6b656e0355534404696e69740b64756d6d795f6669656c640e72656769737465725f746f6b656eedb4a7199ae49f76991614cf4c39c5850000000000000000000000000000000100020104010002000001040e0031093800020001edb4a7199ae49f76991614cf4c39c5850954657374546f6b656e04696e69740000'
// const de = new BcsDeserializer(arrayify(addHexPrefix(moduleHex)))
// const packageData = starcoin_types.Package.deserialize(de)
// const transactionPayload = new starcoin_types.TransactionPayloadVariantPackage(packageData);

const nodeUrl = 'https://barnard-seed.starcoin.org'
const provider = new JsonRpcProvider(nodeUrl);
const senderSequenceNumber = await provider.getSequenceNumber(address)
const chainId = 251;
const nowSeconds = await provider.getNowSeconds();
// console.log({ senderSequenceNumber, nowSeconds })
const rawUserTransaction = generateRawUserTransaction(
address,
transactionPayload,
10000000, //maxGasAmount
1, // gasUnitPrice
senderSequenceNumber,
nowSeconds + 43200,
chainId,
);

const rawUserTransactionHex = bcsEncode(rawUserTransaction)
// console.log({ rawUserTransactionHex })

const signedUserTransactionHex = await signRawUserTransaction(
privateKey,
rawUserTransaction,
);

const txn = await provider.sendTransaction(signedUserTransactionHex);

const txnInfo = await txn.wait(1);

// console.log(txnInfo);

expect(txnInfo.status).toBe('Executed');
}, 60000);

41 changes: 26 additions & 15 deletions src/utils/tx.ts
Expand Up @@ -25,44 +25,55 @@ export function encodeTransactionScript(
return new starcoin_types.TransactionPayloadVariantScript(script);
}

export function encodeScriptFunction(functionId: FunctionId,
export function encodeStarcoinTypesScriptFunction(
functionId: FunctionId,
tyArgs: TypeTag[],
args: bytes[]): starcoin_types.TransactionPayloadVariantScriptFunction {
args: bytes[]
): starcoin_types.ScriptFunction {
const funcId = parseFunctionId(functionId);
const scriptFunction = new starcoin_types.ScriptFunction(
new starcoin_types.ModuleId(addressToSCS(funcId.address), new starcoin_types.Identifier(funcId.module)),
new starcoin_types.Identifier(funcId.functionName),
tyArgs.map((t) => typeTagToSCS(t)),
args
);
return scriptFunction;
}

export function encodeScriptFunction(
functionId: FunctionId,
tyArgs: TypeTag[],
args: bytes[]
): starcoin_types.TransactionPayloadVariantScriptFunction {
const scriptFunction = encodeStarcoinTypesScriptFunction(functionId, tyArgs, args)
return new starcoin_types.TransactionPayloadVariantScriptFunction(scriptFunction);
}

export function encodePackage(
export function encodeStarcoinTypesPackage(
moduleAddress: string,
moduleCodes: HexString[],
initScriptFunction?: { functionId: FunctionId; tyArgs: TypeTag[]; args: bytes[] }
): starcoin_types.TransactionPayloadVariantPackage {
): starcoin_types.Package {
const modules = moduleCodes.map((m) => new starcoin_types.Module(arrayify(m)));
let scriptFunction = null;
if (!!initScriptFunction) {
scriptFunction = encodeScriptFunction(initScriptFunction.functionId, initScriptFunction.tyArgs, initScriptFunction.args);
// const funcId = parseFunctionId(initScriptFunction.functionId);
// scriptFunction = new starcoin_types.ScriptFunction(
// new starcoin_types.ModuleId(addressToSCS(funcId.address), new starcoin_types.Identifier(funcId.module)),
// new starcoin_types.Identifier(funcId.functionName),
// initScriptFunction.tyArgs.map((t) => typeTagToSCS(t)),
// initScriptFunction.args
// );
scriptFunction = encodeStarcoinTypesScriptFunction(initScriptFunction.functionId, initScriptFunction.tyArgs, initScriptFunction.args);
}
const packageData = new starcoin_types.Package(
addressToSCS(moduleAddress),
modules,
scriptFunction
);
return new starcoin_types.TransactionPayloadVariantPackage(
packageData
);
return packageData
}

export function encodePackage(
moduleAddress: string,
moduleCodes: HexString[],
initScriptFunction?: { functionId: FunctionId; tyArgs: TypeTag[]; args: bytes[] }
): starcoin_types.TransactionPayloadVariantPackage {
const packageData = encodeStarcoinTypesPackage(moduleAddress, moduleCodes, initScriptFunction)
return new starcoin_types.TransactionPayloadVariantPackage(packageData);
}

// Step 1: generate RawUserTransaction
Expand Down

0 comments on commit 8866795

Please sign in to comment.