Skip to content

Commit

Permalink
make registerUser abiEncoder call, this commit also contains transfer…
Browse files Browse the repository at this point in the history
… logic to be used in a later task
  • Loading branch information
katien committed Mar 10, 2021
1 parent 5f1c325 commit 7ddb747
Showing 1 changed file with 81 additions and 3 deletions.
84 changes: 81 additions & 3 deletions packages/portis-web3/src/starkware/starkwareProvider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
import * as sc from '@authereum/starkware-crypto';
import { OrderParams, TransferErc20Params, TransferErc721Params, TransferEthParams } from './starkwareTypes';
import StarkwareAbiEncoder from '@authereum/starkware-abi-encoder';
import {
OrderParams,
TransferErc20Params,
TransferErc721Params,
TransferEthParams,
TransferParams,
} from './starkwareTypes';
import { getAssetId, quantizeAmount, sign, Signature } from '@authereum/starkware-crypto';

export default class StarkwareProvider {
private _abiEncoder: StarkwareAbiEncoder;

constructor() {
this._abiEncoder = new StarkwareAbiEncoder();
}

register(ethereumAddress, starkKey, operatorSignature) {
const txData = this._abiEncoder.registerUser({
ethKey: ethereumAddress,
starkKey,
operatorSignature,
});
console.log(txData);
return txData;
}
/**
* Demo code - logic will be moved into the widget in a later ticket
* Do we want to allow users to supply application/index/ethaddress?
* */
getStarkKey() {
Expand All @@ -19,9 +41,65 @@ export default class StarkwareProvider {

createLimitOrder(params: OrderParams) {}

transferEth(params: TransferEthParams) {}
transferEth(params: TransferEthParams): Promise<Signature> {
const starkKey = this.getStarkKey();
const { vaultId, to, quantum, amount, nonce, expirationTimestamp, condition } = params;
return this.transfer({
from: {
starkKey,
vaultId,
},
to,
asset: {
type: 'ETH',
data: {
quantum,
},
},
amount,
nonce,
expirationTimestamp,
condition,
});
}

transferErc20(params: TransferErc20Params) {}

transferErc721(params: TransferErc721Params) {}

private getKeypair() {
const mnemonic = 'curve become rib fuel garment engine great spring aisle mandate also host';
const layer = 'starkex';
const application = 'starkexdvf';
const index = '1';
const ethAddress = '0xc536a7ac035015a017146a97f3ef44851eaaef41';
const path = sc.getAccountPath(layer, application, ethAddress, index);
const keyPair = sc.getKeyPairFromPath(mnemonic, path);
return keyPair;
}

private async transfer(input: TransferParams): Promise<Signature> {
const { from, to, asset, amount, nonce, expirationTimestamp, condition } = input;
const assetId = getAssetId(asset);
const quantizedAmount = quantizeAmount(amount as string, asset.data.quantum as string);
const senderVaultId = from.vaultId;
const targetVaultId = to.vaultId;
const targetKey = input.to.starkKey;

const msgHash = await this._abiEncoder.transfer({
quantizedAmount,
nonce,
senderVaultId,
assetId,
targetVaultId,
targetKey,
expirationTimestamp,
condition,
});

console.log(`executing transfer with hash ${msgHash}`);
const keypair = this.getKeypair();
const starkSignature = await sign(keypair, msgHash);
return starkSignature;
}
}

0 comments on commit 7ddb747

Please sign in to comment.