Skip to content

Commit

Permalink
Merge pull request #24 from MickWang/master
Browse files Browse the repository at this point in the history
to support wasm contract
  • Loading branch information
backslash47 committed Oct 15, 2019
2 parents f09c189 + bbb38f0 commit f98c20d
Show file tree
Hide file tree
Showing 21 changed files with 148 additions and 85 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -14,6 +14,7 @@
"@bugsnag/plugin-react": "^5.1.0",
"@ont-community/hdkey-secp256r1": "^1.0.1",
"@ont-community/ontology-ts-sdk-ledger": "^1.0.8",
"@ont-dev/ontology-dapi": "^0.4.9",
"autoprefixer": "7.1.6",
"axios": "^0.18.0",
"babel-polyfill": "^6.26.0",
Expand All @@ -30,8 +31,7 @@
"lodash": "^4.17.10",
"long": "^4.0.0",
"object-assign": "4.1.1",
"ontology-dapi": "^0.4.9",
"ontology-ts-sdk": "^1.0.22",
"ontology-ts-sdk": "^1.0.24",
"ontology-ts-test": "^0.2.37",
"postcss-flexbugs-fixes": "3.2.0",
"promise": "8.0.1",
Expand Down
2 changes: 1 addition & 1 deletion public/manifest.json
Expand Up @@ -4,7 +4,7 @@
"name": "Cyano wallet",
"author": "Matus Zamborsky <zamborsky@gmail.com>",
"description": "Cyano wallet - an Ontology wallet",
"version": "0.7.15",
"version": "0.7.16",

"browser_action": {
"default_title": "Open the wallet"
Expand Down
2 changes: 1 addition & 1 deletion src/background/api/messageApi.ts
@@ -1,4 +1,4 @@
import { Signature } from 'ontology-dapi';
import { Signature } from '@ont-dev/ontology-dapi';
import { Crypto, utils } from 'ontology-ts-sdk';
import { decryptAccount } from '../../api/accountApi';
import { getWallet } from '../../api/authApi';
Expand Down
81 changes: 62 additions & 19 deletions src/background/api/smartContractApi.ts
Expand Up @@ -15,8 +15,8 @@
* You should have received a copy of the GNU Lesser General Public License
* along with The Ontology Wallet&ID. If not, see <http://www.gnu.org/licenses/>.
*/
import { Parameter } from 'ontology-dapi';
import { Crypto, Transaction, TransactionBuilder, utils } from 'ontology-ts-sdk';
import { Parameter } from '@ont-dev/ontology-dapi';
import { Crypto, Parameter as Param, ParameterType, Transaction, TransactionBuilder, utils } from 'ontology-ts-sdk';
import { buildInvokePayload } from 'ontology-ts-test';
import { decryptAccount, getAccount } from '../../api/accountApi';
import { getWallet } from '../../api/authApi';
Expand All @@ -25,7 +25,7 @@ import { getClient } from '../network';
import { getStore } from '../redux';

import Address = Crypto.Address;
import {decryptDefaultIdentity } from 'src/api/identityApi';
import { decryptDefaultIdentity } from 'src/api/identityApi';

/**
* Creates, signs and sends the transaction for Smart Contract call.
Expand All @@ -48,15 +48,16 @@ export async function scCall(request: ScCallRequest, password: string): Promise<
const state = getStore().getState();
const wallet = getWallet(state.wallet.wallet!);
const account = getAccount(state.wallet.wallet!).address;

let tx: Transaction;
if (request.presignedTransaction) {
tx = Transaction.deserialize(request.presignedTransaction);
} else {
// convert params
const params = convertParams(request.parameters);
/*
* we use ontology-ts-sdk to build the transaction
const payload = buildInvokePayload(request.contract, request.method, params);

tx = TransactionBuilder.makeInvokeTransaction(
request.method,
[],
Expand All @@ -65,8 +66,27 @@ export async function scCall(request: ScCallRequest, password: string): Promise<
String(request.gasLimit),
account,
);

(tx.payload as any).code = payload.toString('hex');
*/
if (request.isWasmVm) {
tx = TransactionBuilder.makeWasmVmInvokeTransaction(
request.method,
params,
new Address(utils.reverseHex(request.contract)),
String(request.gasPrice),
String(request.gasLimit),
account,
)
} else {
tx = TransactionBuilder.makeInvokeTransaction(
request.method,
params,
new Address(utils.reverseHex(request.contract)),
String(request.gasPrice),
String(request.gasLimit),
account,
);
}
}

let privateKey: Crypto.PrivateKey;
Expand Down Expand Up @@ -100,19 +120,37 @@ export async function scCall(request: ScCallRequest, password: string): Promise<

export async function scCallRead(request: ScCallReadRequest) {
request.parameters = request.parameters !== undefined ? request.parameters : [];

const gasPrice = '500';
const gasLimit = '30000';
// convert params
const params = convertParams(request.parameters);
/*
const payload = buildInvokePayload(request.contract, request.method, params);

const tx = TransactionBuilder.makeInvokeTransaction(
request.method,
[],
new Address(utils.reverseHex(request.contract)),
);

(tx.payload as any).code = payload.toString('hex');

*/
let tx: Transaction;
if (request.isWasmVm) {
tx = TransactionBuilder.makeWasmVmInvokeTransaction(
request.method,
params,
new Address(utils.reverseHex(request.contract)),
gasPrice,
gasLimit,
);
} else {
tx = TransactionBuilder.makeInvokeTransaction(
request.method,
params,
new Address(utils.reverseHex(request.contract)),
gasPrice,
gasLimit,
)
}
const client = getClient();
return await client.sendRawTransaction(tx.serialize(), true, false);
}
Expand All @@ -134,7 +172,7 @@ export async function scDeploy(request: ScDeployRequest, password: string) {
request.author,
request.email,
request.description,
request.needStorage,
request.vmType,
String(request.gasPrice),
String(request.gasLimit),
account,
Expand All @@ -146,7 +184,7 @@ export async function scDeploy(request: ScDeployRequest, password: string) {
return await client.sendRawTransaction(tx.serialize(), false, true);
}

function convertParams(parameters?: Parameter[]): any[] {
function convertParams(parameters?: Parameter[]): Param[] {
if (parameters === undefined) {
return [];
}
Expand All @@ -165,19 +203,24 @@ function convertMapParams(map: any) {
return obj;
}

function convertParam(parameter: Parameter) {
function convertParam(parameter: Parameter): Param {
if (parameter.type === 'Boolean') {
return parameter.value === true || parameter.value === 'true';
return new Param('', ParameterType.Boolean, parameter.value === true || parameter.value === 'true')
} else if (parameter.type === 'Integer') {
return Number(parameter.value);
return new Param('', ParameterType.Integer, Number(parameter.value))
} else if (parameter.type === 'ByteArray') {
return new Buffer(parameter.value, 'hex');
// return new Buffer(parameter.value, 'hex');
// return parameter.value;
// will use ontology-ts-sdk to build script code and it treats ByteArray as hex string;
return new Param('', ParameterType.ByteArray, parameter.value)
} else if (parameter.type === 'String') {
return parameter.value;
return new Param('', ParameterType.String, parameter.value)
} else if (parameter.type === 'Array') {
return convertParams(parameter.value);
return new Param('', ParameterType.Array, convertParams(parameter.value));
} else if (parameter.type === 'Map') {
return convertMapParams(parameter.value);
return new Param('', ParameterType.Map, convertMapParams(parameter.value));
} else if (parameter.type === 'Address') {
return new Param('', ParameterType.Address, new Address(parameter.value));
} else {
// send as is, so underlying library can process it
return parameter.value;
Expand Down
2 changes: 1 addition & 1 deletion src/background/dapp/asset.ts
@@ -1,4 +1,4 @@
import { AssetApi } from 'ontology-dapi';
import { AssetApi } from '@ont-dev/ontology-dapi';
import { getAddress, getPublicKey } from '../../api/accountApi';
import { getStore } from '../redux';
import { getRequestsManager } from '../requestsManager';
Expand Down
2 changes: 1 addition & 1 deletion src/background/dapp/identity.ts
@@ -1,4 +1,4 @@
import { IdentityApi, OntIdDDO } from 'ontology-dapi';
import { IdentityApi, OntIdDDO } from '@ont-dev/ontology-dapi';
import { DDO, OntidContract } from 'ontology-ts-sdk';
import { getIdentity } from '../../api/identityApi';
import { getClient } from '../network';
Expand Down
2 changes: 1 addition & 1 deletion src/background/dapp/index.ts
@@ -1,4 +1,4 @@
import { client, provider } from 'ontology-dapi';
import { client, provider } from '@ont-dev/ontology-dapi';
import { assetApi as asset } from './asset';
import { identityApi as identity } from './identity';
import { messageApi as message } from './message';
Expand Down
2 changes: 1 addition & 1 deletion src/background/dapp/message.ts
@@ -1,4 +1,4 @@
import { MessageApi, Signature } from 'ontology-dapi';
import { MessageApi, Signature } from '@ont-dev/ontology-dapi';
import { messageVerify } from '../api/messageApi';
import { getRequestsManager } from '../requestsManager';

Expand Down
2 changes: 1 addition & 1 deletion src/background/dapp/network.ts
@@ -1,5 +1,5 @@
import Address = Crypto.Address;
import { Balance, Block, MerkleProof, Network, NetworkApi, Transaction } from 'ontology-dapi';
import { Balance, Block, MerkleProof, Network, NetworkApi, Transaction } from '@ont-dev/ontology-dapi';
import { Crypto } from 'ontology-ts-sdk';
import { decodeAmount } from 'src/popup/utils/number';
import { getTokenBalance } from '../api/tokenApi';
Expand Down
2 changes: 1 addition & 1 deletion src/background/dapp/provider.ts
@@ -1,4 +1,4 @@
import { Provider, ProviderApi } from 'ontology-dapi';
import { Provider, ProviderApi } from '@ont-dev/ontology-dapi';
import { browser } from 'webextension-polyfill-ts';

export const providerApi: ProviderApi = {
Expand Down
65 changes: 41 additions & 24 deletions src/background/dapp/smartContract.ts
@@ -1,41 +1,58 @@
import { Response, SmartContractApi } from 'ontology-dapi';
import { Response, SmartContractApi } from '@ont-dev/ontology-dapi';
import { Hash } from 'ontology-ts-crypto';

import { getRequestsManager } from '../requestsManager';

export const smartContractApi: SmartContractApi = {
async invoke(options): Promise<Response> {
const { scriptHash, operation, args, gasPrice, gasLimit, requireIdentity } = options;
async function invoke(options: any): Promise<Response> {
const { scriptHash, operation, args, gasPrice, gasLimit, requireIdentity, isWasmVm } = options;

const oldOptions: any = options;
const oldOptions: any = options;

const parameters = args !== undefined ? args : oldOptions.parameters;
const paramsHash = Hash.sha256(new Buffer(JSON.stringify(parameters))).toString('hex');
const parameters = args !== undefined ? args : oldOptions.parameters;
const paramsHash = Hash.sha256(new Buffer(JSON.stringify(parameters))).toString('hex');

return await getRequestsManager().initScCall({
contract: scriptHash !== undefined ? scriptHash : oldOptions.contract,
gasLimit,
gasPrice,
method: operation !== undefined ? operation : oldOptions.method,
parameters,
paramsHash,
requireIdentity,
});
},
return await getRequestsManager().initScCall({
contract: scriptHash !== undefined ? scriptHash : oldOptions.contract,
gasLimit,
gasPrice,
isWasmVm,
method: operation !== undefined ? operation : oldOptions.method,
parameters,
paramsHash,
requireIdentity,
});
}

async invokeRead(options): Promise<any> {
const { scriptHash, operation, args } = options;
async function invokeRead(options: any): Promise<Response> {
const { scriptHash, operation, args, isWasmVm } = options;

const oldOptions: any = options;

return await getRequestsManager().initScCallRead({
contract: scriptHash !== undefined ? scriptHash : oldOptions.contract,
method: operation !== undefined ? operation : oldOptions.method,
return await getRequestsManager().initScCallRead({
contract: scriptHash !== undefined ? scriptHash : oldOptions.contract,
isWasmVm,
method: operation !== undefined ? operation : oldOptions.method,
parameters: args !== undefined ? args : oldOptions.parameters,
});
}

export const smartContractApi: SmartContractApi = {
async invoke(options: any): Promise<Response> {
return invoke(options);
},
async invokeWasm(options: any): Promise<Response> {
return invoke(Object.assign({}, options, { isWasmVm: true }));
},

async invokeRead(options: any): Promise<any> {
return invokeRead(options);
},

async invokeWasmRead(options: any): Promise<any> {
return invokeRead(Object.assign({}, options, { isWasmVm: true }));
},

async deploy({ code, name, version, author, email, description, needStorage, gasPrice, gasLimit }): Promise<void> {
async deploy({ code, name, version, author, email, description, vmType, gasPrice, gasLimit }): Promise<void> {
return await getRequestsManager().initScDeploy({
author,
code,
Expand All @@ -44,8 +61,8 @@ export const smartContractApi: SmartContractApi = {
gasLimit,
gasPrice,
name,
needStorage,
version,
vmType,
});
},
};
4 changes: 2 additions & 2 deletions src/background/dapp/state-channel.ts
@@ -1,5 +1,5 @@
import { Signature } from 'ontology-dapi';
import { StateChannelApi } from 'ontology-dapi/lib/types/api/stateChannel';
import { Signature } from '@ont-dev/ontology-dapi';
import { StateChannelApi } from '@ont-dev/ontology-dapi';

export const stateChannelApi: StateChannelApi = {
async login(): Promise<string> {
Expand Down
2 changes: 1 addition & 1 deletion src/background/dapp/stateChannel.ts
@@ -1,4 +1,4 @@
import { Signature, StateChannelApi } from 'ontology-dapi';
import { Signature, StateChannelApi } from '@ont-dev/ontology-dapi';
import { getRequestsManager } from '../requestsManager';


Expand Down
2 changes: 1 addition & 1 deletion src/background/popUpManager.ts
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with The Ontology Wallet&ID. If not, see <http://www.gnu.org/licenses/>.
*/
import { MethodType, Rpc } from 'ontology-dapi';
import { MethodType, Rpc } from '@ont-dev/ontology-dapi';
import { Identity } from 'ontology-ts-sdk';
import { decryptDefaultIdentity } from 'src/api/identityApi';
import { browser } from 'webextension-polyfill-ts';
Expand Down
4 changes: 2 additions & 2 deletions src/background/redux/transactionRequestsReducer.ts
Expand Up @@ -197,7 +197,7 @@ function isTrustedSc(request: ScCallRequest, state: GlobalState) {
if (request.requireIdentity) {
return false;
}

const trustedScs = state.settings.trustedScs;

const trustedSc = trustedScs.find(
Expand Down Expand Up @@ -249,7 +249,7 @@ async function submitScCall(request: ScCallRequest, password: string, dispatch:
};
}


}

async function submitMessageSign(request: MessageSignRequest, password: string) {
Expand Down
9 changes: 5 additions & 4 deletions src/background/requestsManager.ts
@@ -1,4 +1,4 @@
import { Parameter } from 'ontology-dapi';
import { Parameter, VmType } from '@ont-dev/ontology-dapi';
import { getWallet } from 'src/api/authApi';
import { hasIdentity } from 'src/api/identityApi';
import { v4 as uuid } from 'uuid';
Expand Down Expand Up @@ -122,7 +122,8 @@ export class RequestsManager {
return deferred.promise;
}

public async initScCall(args: {
public async initScCall(args: {
isWasmVm: boolean,
contract: string;
method: string;
parameters?: Parameter[];
Expand Down Expand Up @@ -197,7 +198,7 @@ export class RequestsManager {
return deferred.promise;
}

public async initScCallRead(args: { contract: string; method: string; parameters?: Parameter[] }) {
public async initScCallRead(args: { isWasmVm: boolean; contract: string; method: string; parameters?: Parameter[] }) {
const requestId = uuid();

// stores deferred object to resolve when the transaction is resolved
Expand All @@ -224,7 +225,7 @@ export class RequestsManager {
author?: string;
email?: string;
description?: string;
needStorage?: boolean;
vmType?: VmType | boolean;
gasPrice?: number;
gasLimit?: number;
}) {
Expand Down

0 comments on commit f98c20d

Please sign in to comment.