From f38a4354fdce0b61ffa81d75eac5980788e3cb81 Mon Sep 17 00:00:00 2001 From: cebgcontract <99630598+cebgcontract@users.noreply.github.com> Date: Tue, 29 Nov 2022 14:21:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0sign=20with=20apple?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/JCWallet.d.ts | 22 ++++++++++++++++ src/api/WalletApi.ts | 6 +++++ src/config/chain_config.ts | 20 +++++++------- src/index.ts | 4 +-- src/manage/WalletManage.ts | 52 ++++++++++++++++++++++--------------- src/services/NativeSvr.ts | 10 ++++--- src/standards/JCStandard.ts | 12 +++------ src/util/sign.util.ts | 46 +++++++++++++++++++------------- 8 files changed, 108 insertions(+), 64 deletions(-) diff --git a/src/JCWallet.d.ts b/src/JCWallet.d.ts index b6e5b7e..81c6ea8 100644 --- a/src/JCWallet.d.ts +++ b/src/JCWallet.d.ts @@ -12,4 +12,26 @@ declare namespace jc { export const wallet: JCWallet; } +declare namespace jsb { + export function walletSign(str: string); + export function loadLocalStorage(key: string); + export function signWithGoogle(id: number); + export function signWithApple(id: number); + export function signOutGoogle(id: number); + export function scanQRCode(id: number, title: string); + export function restoreKey(id: number, oid: string); + export function generateWallet(idHash: string, seedHash: string); + export function prepareWallet( + idHash: string, + seedHash: string, + key_master: string + ); + export function restoreWallet( + idHash: string, + seedHash: string, + key_master: string, + key_restore: any + ); +} + declare let window: Window; diff --git a/src/api/WalletApi.ts b/src/api/WalletApi.ts index 8db72e1..51ee243 100644 --- a/src/api/WalletApi.ts +++ b/src/api/WalletApi.ts @@ -6,12 +6,18 @@ export function googleAuth(idToken: string) { return POST_JSON(url, { token: idToken }); } +export function appleAuth(idToken: string) { + const url = `${WALLET_API_HOST}/wallet/login/apple`; + return POST_JSON(url, { token: idToken }); +} + export function getWalletInfo() { const url = `${WALLET_API_HOST}/wallet/info`; return GET_JSON(url); } export function uploadWalletInfo(data) { + console.log("uploadWalletInfo: " + data); const url = `${WALLET_API_HOST}/wallet/info`; return POST_JSON(url, data); } diff --git a/src/config/chain_config.ts b/src/config/chain_config.ts index bcf0ccf..aefbcc7 100644 --- a/src/config/chain_config.ts +++ b/src/config/chain_config.ts @@ -94,13 +94,13 @@ export const DEFALUT_TOKENS = { }, { type: "erc20", - address: "0x89c320c75520777AB36f4924cE57c65960370fE1", + address: "0x9561C133DD8580860B6b7E504bC5Aa500f0f06a7", symbol: "CEC", decimal: 18, }, { type: "erc20", - address: "0x070a69230a08B93807c43a96327ff091aE7fC04D", + address: "0x59d3631c86BbE35EF041872d502F218A39FBa150", symbol: "CEG", decimal: 18, }, @@ -182,19 +182,19 @@ export const DEFAULT_NFT_TYPES = { }, 1338: { hero: { - address: "0xE9809Eb4231EE76963842A4cd8c89bD06dF5F09E", + address: "0x9b1f7F645351AF3631a656421eD2e40f2802E6c0", type: "erc721", }, weapon: { - address: "0x6E62eFEAb443bd1B233C4DF795Da4794511a8907", + address: "0x2612Af3A521c2df9EAF28422Ca335b04AdF3ac66", type: "erc721", }, chip: { - address: "0xAE3b465F19Bf5B0B2ef94A1acFd41b412b1D36b2", + address: "0x26b4AFb60d6C903165150C6F0AA14F8016bE4aec", type: "erc1155", }, shard: { - address: "0x7B08143e8B24F98Bc48D432f52C29b3cE15Ba78A", + address: "0x0E696947A06550DEf604e82C26fd9E493e576337", type: "erc1155", }, }, @@ -223,9 +223,9 @@ export const JC_CONTRACTS = { chipLocker: "0x4Ef766854EE104053cF2D243620b7A643fCC2B54", }, 1338: { - nftMall: "0x24eB97E328d7D513a9eb4AA91206d0cF2Ab78Ee0", - evolveFactory: "0x12F2df43A6385B8D5D6730822A09a2D6c42b61c0", - minterFactory: "0x0Ca8A551544d39eb71BD0F70Af22e126C263409A", - chipLocker: "0x757dbc31BB286832d21C505F1D80FbcfF6756e49", + nftMall: "0x5017A545b09ab9a30499DE7F431DF0855bCb7275", + evolveFactory: "0xaD888d0Ade988EbEe74B8D4F39BF29a8d0fe8A8D", + minterFactory: "0x6eD79Aa1c71FD7BdBC515EfdA3Bd4e26394435cC", + chipLocker: "0xFF6049B87215476aBf744eaA3a476cBAd46fB1cA", }, }; diff --git a/src/index.ts b/src/index.ts index febb3e8..62b954d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -110,8 +110,8 @@ export default class JCWallet { } } - public async initInternalWallet() { - let address: string = await loadInternalWallet(); + public async initInternalWallet(channel: number) { + let address: string = await loadInternalWallet(channel); this.nativeAccount = address; console.log("native wallet address: " + address); var start = Date.now(); diff --git a/src/manage/WalletManage.ts b/src/manage/WalletManage.ts index d23e5ba..5021824 100644 --- a/src/manage/WalletManage.ts +++ b/src/manage/WalletManage.ts @@ -2,7 +2,12 @@ import { hdkey } from "ethereumjs-wallet"; import { generateMnemonic, mnemonicToSeedSync } from "bip39"; import { loadMnemonic, saveMnemonic } from "./DataManage"; import { NativeSvr } from "../services/NativeSvr"; -import { getWalletInfo, googleAuth, uploadWalletInfo } from "../api/WalletApi"; +import { + appleAuth, + getWalletInfo, + googleAuth, + uploadWalletInfo, +} from "../api/WalletApi"; import { WalletEnv } from "../config/WalletEnv"; import { md5Hash, sha1Hash } from "../util/crypto.util"; import { retry } from "../util/promise.util"; @@ -30,11 +35,19 @@ export function restoreWalletByMnemonic(mnemonic: string, password: string) { saveMnemonic(mnemonic, password); } -export async function loadInternalWallet() { - let res: any = await new NativeSvr().signWithGoogle(); - console.log("native res: " + res); - let tokenRes = await googleAuth(res); +export async function loadInternalWallet(channel: number) { + let tokenRes: any; + if (channel == 1) { + let res: any = await new NativeSvr().signWithApple(); + console.log("native apple res: " + res); + tokenRes = await appleAuth(res); + } else { + let res: any = await new NativeSvr().signWithGoogle(); + console.log("native google res: " + res); + tokenRes = await googleAuth(res); + } console.log("wallet token: " + tokenRes.data?.token); + console.log(tokenRes); if (tokenRes.errcode || !tokenRes.data?.token) { return; } @@ -60,33 +73,31 @@ export async function loadInternalWallet() { let idHash = md5Hash(infoRes.data.oid); if (!infoRes.data.key) { let time = Date.now(); - //@ts-ignore - console.log(jsb.generateWallet); - //@ts-ignore let strWallet = jsb.generateWallet(idHash, seedHash); console.log("generate wallet cost: " + (Date.now() - time) / 1000); window.debug && console.log("native wallet info " + strWallet); let walletInfo = JSON.parse(strWallet); address = walletInfo.address; - setImmediate(function () { - retry( - () => uploadWalletInfo({ key: walletInfo.master }), - MAX_UPLOAD_COUNT - ); - }); + // setImmediate(function () { + + // }); + retry(() => uploadWalletInfo({ key: walletInfo.master }), MAX_UPLOAD_COUNT); } else { - let localSKey = localStorage.getItem("cebg_wallet_s_" + idHash); - let localBKey = localStorage.getItem("cebg_wallet_b_" + idHash); + let localSKey, localBKey; + if (jsb.loadLocalStorage) { + localSKey = jsb.loadLocalStorage("cebg_wallet_s_" + idHash); + localBKey = jsb.loadLocalStorage("cebg_wallet_b_" + idHash); + } else { + localSKey = localStorage.getItem("cebg_wallet_s_" + idHash); + localBKey = localStorage.getItem("cebg_wallet_b_" + idHash); + } + if (localSKey || localBKey) { - //@ts-ignore let strWallet = jsb.prepareWallet(idHash, seedHash, infoRes.data.key); let walletInfo = JSON.parse(strWallet); address = walletInfo.address; } else { let qrResult = await new NativeSvr().restoreKey(idHash); - // let qrResult = - // "cd00eb0126aeed39762579ce94c90a04695ad17fbd5e79aa4e9fc4a34ba32a5"; - //@ts-ignore let strWallet = jsb.restoreWallet( idHash, seedHash, @@ -108,7 +119,6 @@ export async function loadInternalWallet() { } export function walletSign(str: string) { - //@ts-ignore let result = jsb.walletSign(str); return result; } diff --git a/src/services/NativeSvr.ts b/src/services/NativeSvr.ts index fac2c2c..1c28813 100644 --- a/src/services/NativeSvr.ts +++ b/src/services/NativeSvr.ts @@ -30,28 +30,30 @@ export class NativeSvr { public signWithGoogle() { let id = payloadId(); - //@ts-ignore jsb.signWithGoogle(id); return this._subscribeToCallResponse(id); } + public signWithApple() { + let id = payloadId(); + jsb.signWithApple(id); + return this._subscribeToCallResponse(id); + } + public signOutGoogle() { let id = payloadId(); - //@ts-ignore jsb.signOutGoogle(id); return this._subscribeToCallResponse(id); } public scanQRCode(title: string) { let id = payloadId(); - //@ts-ignore jsb.scanQRCode(id, title); return this._subscribeToCallResponse(id); } public restoreKey(oid: string) { let id = payloadId(); - //@ts-ignore jsb.restoreKey(id, oid); return this._subscribeToCallResponse(id); } diff --git a/src/standards/JCStandard.ts b/src/standards/JCStandard.ts index 2de17f5..c9777bf 100644 --- a/src/standards/JCStandard.ts +++ b/src/standards/JCStandard.ts @@ -53,15 +53,9 @@ export class JCStandard { jumpToWallet(); }, 1500); } - let result; - try { - result = await contract.methods - .buy721NFT(addresses, values, signature) - .send({ gas: (gas * 1.1) | 0 }); - } catch (err) { - console.log(JSON.stringify(err)); - } - return result; + return contract.methods + .buy721NFT(addresses, values, signature) + .send({ gas: (gas * 1.1) | 0 }); } async buyNft1155({ diff --git a/src/util/sign.util.ts b/src/util/sign.util.ts index 6c8ce21..108fdad 100644 --- a/src/util/sign.util.ts +++ b/src/util/sign.util.ts @@ -1,29 +1,33 @@ -import { signTypedData, SignTypedDataVersion, TypedMessage } from "@metamask/eth-sig-util" +import { + signTypedData, + SignTypedDataVersion, + TypedMessage, +} from "@metamask/eth-sig-util"; export function buildLoginSignMsg(nonce: string, tips: string) { const signMsg = { tips, nonce, - } + }; const signObj = { types: { - EIP712Domain: [ - { name: 'name', type: 'string' }, - { name: 'version', type: 'string' } - ], - set: [ - { name: 'tips', type: 'string' }, - { name: 'nonce', type: 'string' } - ] + EIP712Domain: [ + { name: "name", type: "string" }, + { name: "version", type: "string" }, + ], + set: [ + { name: "tips", type: "string" }, + { name: "nonce", type: "string" }, + ], }, primaryType: "set", domain: { - name: 'Auth', - version: '1' + name: "Auth", + version: "1", }, - message: signMsg - } - return signObj + message: signMsg, + }; + return signObj; } export function signLogin(nonce: string, tips: string, privateKey: string) { @@ -31,7 +35,13 @@ export function signLogin(nonce: string, tips: string, privateKey: string) { return signTypedData({ //@ts-ignore data: signObj, - privateKey: Buffer.from(privateKey.replace('0x', ''), 'hex'), - version: SignTypedDataVersion.V4 - }) + privateKey: Buffer.from(privateKey.replace("0x", ""), "hex"), + version: SignTypedDataVersion.V4, + }); +} + +const REG_KEY = /^[0-9a-fA-F]{63,64}$/; + +export function isEncrypt(msg: string) { + return !REG_KEY.test(msg); }