Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
1. add one param(gasUniPrice) in utils.tx.generateRawUserTransaction …
…2.add encoding.stringToBytes && encoding.bytesToString
  • Loading branch information
wk3368 committed Aug 27, 2021
1 parent 5ec696d commit 5cd09ba
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file, as of versi
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.5.2] - 2021-08-26
- add encoding.stringToBytes && encoding.bytesToString

## [1.5.1] - 2021-08-26
- add one param(gasUniPrice) in utils.tx.generateRawUserTransaction

## [1.5.0] - 2021-08-26
- support contract.call_v2 method in rpc actions

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@starcoin/starcoin",
"version": "1.5.0",
"version": "1.5.2",
"description": "starcoin js sdk",
"author": "lerencao, wk3368, tarvos21",
"license": "Apache-2.0",
Expand Down
15 changes: 12 additions & 3 deletions src/encoding/index.spec.ts
Expand Up @@ -3,7 +3,7 @@ import { BigNumber } from '@ethersproject/bignumber';
import {
addressToSCS, decodeTransactionPayload, decodeSignedUserTransaction, privateKeyToPublicKey,
publicKeyToAuthKey, publicKeyToAddress, publicKeyToReceiptIdentifier, encodeReceiptIdentifier,
decodeReceiptIdentifier
decodeReceiptIdentifier, bytesToString, stringToBytes
} from '.';
import { BcsSerializer, BcsDeserializer } from '../lib/runtime/bcs';
import { TransactionArgumentVariantU128 } from '../lib/runtime/starcoin_types';
Expand Down Expand Up @@ -113,7 +113,7 @@ test("encoding SignedUserTransaction hex, 0x1::DaoVoteScripts::cast_vote", async

// TODO: generate maxGasAmount from contract.dry_run -> gas_used
const maxGasAmount = 10000000

const gasUnitPrice = 1
const chainId = 1

// because the time system in dev network is relatively static,
Expand Down Expand Up @@ -176,6 +176,7 @@ test("encoding SignedUserTransaction hex, 0x1::DaoVoteScripts::cast_vote", async
// senderAddressHex,
// scriptFunction,
// maxGasAmount,
// gasUnitPrice,
// senderSequenceNumber,
// expirationTimestampSecs,
// chainId
Expand Down Expand Up @@ -207,6 +208,7 @@ test("encoding SignedUserTransaction hex, 0x1::TransferScripts::peer_to_peer", a

// TODO: generate maxGasAmount from contract.dry_run -> gas_used
const maxGasAmount = 10000000
const gasUnitPrice = 1

const chainId = 1

Expand Down Expand Up @@ -261,6 +263,7 @@ test("encoding SignedUserTransaction hex, 0x1::TransferScripts::peer_to_peer", a
senderAddressHex,
scriptFunction,
maxGasAmount,
gasUnitPrice,
senderSequenceNumber,
expirationTimestampSecs,
chainId
Expand Down Expand Up @@ -380,4 +383,10 @@ test("encode struct type args: vote", () => {
expect(structTypeTags[1]['Struct'].type_params.length).toBe(1)
});


test('encoe/decode string', () => {
const imageUrl = 'ipfs://QmSPcvcXgdtHHiVTAAarzTeubk5X3iWymPAoKBfiRFjPMY'
const imageBytes = stringToBytes(imageUrl)
const imageHex = hexlify(imageBytes)
expect(imageHex).toBe('0x697066733a2f2f516d5350637663586764744848695654414161727a546575626b3558336957796d50416f4b42666952466a504d59')
expect(bytesToString(imageBytes)).toBe(imageUrl)
});
48 changes: 48 additions & 0 deletions src/encoding/index.ts
Expand Up @@ -356,3 +356,51 @@ export function publicKeyToReceiptIdentifier(publicKey: string): string {
// throw new Error(`invalid txn argument${data}`);
//
// }

export function stringToBytes(str: string): BytesLike {
let bytes = new Array();
let len, c;
len = str.length;
for (let i = 0; i < len; i++) {
c = str.charCodeAt(i);
if (c >= 0x010000 && c <= 0x10FFFF) {
bytes.push(((c >> 18) & 0x07) | 0xF0);
bytes.push(((c >> 12) & 0x3F) | 0x80);
bytes.push(((c >> 6) & 0x3F) | 0x80);
bytes.push((c & 0x3F) | 0x80);
} else if (c >= 0x000800 && c <= 0x00FFFF) {
bytes.push(((c >> 12) & 0x0F) | 0xE0);
bytes.push(((c >> 6) & 0x3F) | 0x80);
bytes.push((c & 0x3F) | 0x80);
} else if (c >= 0x000080 && c <= 0x0007FF) {
bytes.push(((c >> 6) & 0x1F) | 0xC0);
bytes.push((c & 0x3F) | 0x80);
} else {
bytes.push(c & 0xFF);
}
}
return bytes;
}

export function bytesToString(arr: BytesLike): string {
if (typeof arr === 'string') {
return arr;
}
let str = '';
for (let i = 0; i < arr.length; i++) {
let one = arr[i].toString(2),
v = one.match(/^1+?(?=0)/);
if (v && one.length == 8) {
let bytesLength = v[0].length;
let store = arr[i].toString(2).slice(7 - bytesLength);
for (let st = 1; st < bytesLength; st++) {
store += arr[st + i].toString(2).slice(2);
}
str += String.fromCharCode(parseInt(store, 2));
i += bytesLength - 1;
} else {
str += String.fromCharCode(arr[i]);
}
}
return str;
}
7 changes: 5 additions & 2 deletions src/providers/jsonrpc-provider.spec.ts
Expand Up @@ -183,7 +183,7 @@ describe('jsonrpc-provider', () => {

// TODO: generate maxGasAmount from contract.dry_run -> gas_used
let maxGasAmount = 10000000n;

const gasUnitPrice = 1;
// because the time system in dev network is relatively static,
// we should use nodeInfo.now_secondsinstead of using new Date().getTime()
const nowSeconds = await provider.getNowSeconds();
Expand Down Expand Up @@ -216,6 +216,7 @@ describe('jsonrpc-provider', () => {
senderAddressHex,
scriptFunction,
maxGasAmount,
gasUnitPrice,
senderSequenceNumber,
expirationTimestampSecs,
chainId
Expand All @@ -237,6 +238,7 @@ describe('jsonrpc-provider', () => {
senderAddressHex,
scriptFunction,
maxGasAmount,
gasUnitPrice,
senderSequenceNumber,
expirationTimestampSecs,
chainId
Expand Down Expand Up @@ -277,7 +279,7 @@ describe('jsonrpc-provider', () => {

// TODO: generate maxGasAmount from contract.dry_run -> gas_used
const maxGasAmount = 10000000;

const gasUnitPrice = 1;
// because the time system in dev network is relatively static,
// we should use nodeInfo.now_secondsinstead of using new Date().getTime()
const nowSeconds = await provider.getNowSeconds();
Expand All @@ -299,6 +301,7 @@ describe('jsonrpc-provider', () => {
address,
transactionPayload,
maxGasAmount,
gasUnitPrice,
senderSequenceNumber,
expirationTimestampSecs,
chainId
Expand Down
4 changes: 3 additions & 1 deletion src/providers/web3-provider.ts
Expand Up @@ -35,7 +35,9 @@ function buildWeb3LegacyFetcher(provider: ExternalProvider, sendFunc: Web3Legacy

return new Promise((resolve, reject) => {
sendFunc(request, function (error, result) {
if (error) { return reject(error); }
if (error) {
return reject(error);
}

if (result.error) {
const error = new Error(result.error.message);
Expand Down
3 changes: 2 additions & 1 deletion src/utils/tx.ts
Expand Up @@ -62,6 +62,7 @@ export function generateRawUserTransaction(
senderAddress: HexString,
payload: starcoin_types.TransactionPayload,
maxGasAmount: U64,
gasUnitPrice: U64,
senderSequenceNumber: U64,
expirationTimestampSecs: U64,
chainId: U8
Expand All @@ -72,7 +73,7 @@ export function generateRawUserTransaction(
const sequence_number = BigInt(senderSequenceNumber)

const max_gas_amount = BigInt(maxGasAmount)
const gas_unit_price = BigInt(1)
const gas_unit_price = BigInt(gasUnitPrice)
const gas_token_code = '0x1::STC::STC'
const expiration_timestamp_secs = BigInt(expirationTimestampSecs)
const chain_id = new starcoin_types.ChainId(chainId)
Expand Down

0 comments on commit 5cd09ba

Please sign in to comment.