DAPP文档

  • 中文
  • 介绍
  • 产品
    • WebView
      • Dapp开发指引
      • hwaToken SDK
      • web3 SDK
      • hwa SDK
      • ethereum SDK
    • Deep WalletConnect
    • Deep Linking
    • Public Hchains Methods

    hwaToken SDK

    访问 hwaToken 公开 API 的现代化工具。

    介绍

    借助于 @lnssh/webview,我们可以轻松地访问和使用 hwaToken WebView 环境中的各种接口。无论是将 DApp 迁移到 hwaToken 应用还是改编自己的 Web 应用程序,都可以用最少的代码和最小的侵入性快速解决。 该 SDK 包括获得语言环境、Native UI、路由导航、扫描二维码等功能。SDK 本身不包含依赖性,只作为绑定宿主环境的工具,不会对你的项目造成任何侵入性的损害。K。

    支持功能

    完善的 TypeScript 支持

    所有的接口都有完善的类型支持,大部分场景下甚至无需参考文档就能完成开发

    多模块支持

    在现代化的前端项目中,esm 是默认模块导入方式,但我们仍旧支持 cjs umd 等模块。

    Promise

    当前,所有的异步方法都会支持 Promise,并有相应的类型标识。

    快速开始

    如果你正在寻找一个完整的项目示例,请查看我们准备好的完整项目示例

    • React 项目示例:examples/react-app
    • Vue 项目示例:examples/Vue-app
    • UMD 项目示例:examples/UMD

    安装

    建议使用包管理工具下载与安装 @lnssh/webview 。为此,我们的项目需要 NodeJS 环境与 NPM 管理工具。(或是 Yarn)

    NPM: (推荐方式)

    npm i @lnssh/webview --save

    Yarn:

    yarn add @lnssh/webview

    引入和使用

    在使用 SDK 之前,你需要使用 import 引入模块:

    import HWAWebView from '@lnssh/webview'

    为了保持兼容性,我们建议你总是在使用前先判断是否处于 imToken 环境中:

    if (HWAWebView.isHwaEnv()) { HWAWebView.apis.navigator.setTitle('hello world') }

    API 参考

    这是所有的 API 和类型参考。事实上如果你正在使用 TypeScript,项目中已经有完善的接口提示

    通用

    这是根模块携带的静态 API (纯函数),通常用于检查环境和通用场景的处理。

    callAPI

    我们提供了一系列的方法来增强 DApp 的功能,每一个方法都有一个 apiName, 开发者可以按下面的形式调用:

    hwaToken.callAPI(apiName, options)=>Promise<object|string|number>

    ParamsTypeDescription
    apiNamestring被调用方法的名称,在后面会看到可用的方法名列表
    optionsobject方法参数,取决于具体的方法

    通过 callPromisifyAPI 去调用一个 API,,第一个参数总是 error 对象 (如果没有错误发生,error 的值会是 null), error 对象总是会包含一个 message 属性, 有时候会包含一个 code 属性表示特殊的错误码 。 第二个参数是方法调用成功返回的 result。

    示例
    window.hwaToken.callAPI(apiName, params).then((error, result) => { if (error) { alert(error.message); } else { alert(result); } } );

    isHwaEnv

    检查当前是否处于 hwaToken WebView 环境中,返回布尔值。

    type isHwaEnv = () => boolean

    示例
    console.log('Currently in HWA: ', window.hwaToken.isHwaEnv())

    getVersion

    获取 HWA 应用当前版本。

    type getVersion = () => string

    示例
    console.log('Currently version: ', window.hwaToken.getVersion()); // output -> 'Currently version: 1.0.0'

    apis

    与宿主环境进行通信的 API,详见下文。

    navigator API

    getOrientation

    获得 APP 当前的 orientation 状态,返回 'LANDSCAPE' 或 'PORTRAIT'

    type getOrientation = () => Promise<Orientation>

    示例
    window.hwaToken.apis.navigator.getOrientation().then((err,orientation)=>{ console.log(orientation) }); // output -> 'LANDSCAPE'

    closeDapp

    关闭当前 DApp 页面,返回到 DApp列表页

    type closeDapp = () => void

    示例
    window.hwaToken.apis.navigator.closeDapp();

    goBack

    返回到历史记录的上一页, 类似 history.go(-1) 如果没有上一页,将会退出当前 DApp

    type goBack = () => void

    示例
    window.hwaToken.apis.navigator.goBack();

    routeTo

    导航进入 HWA App 的某个特殊页面,例如进入某个 DApp 页面。

    type RouteToParams = { screen: string props: { title: string url: string } }

    type routeTo = (params: RouteToParams) => void

    ParamsTypeDescription
    screenstring导航进入APP某个页面
    propsobject导航进入APP某个页面附带的参数
    示例
    window.hwaToken.apis.navigator.routeTo({ screen: 'DappView', props: { title: 'DApp API Examples', url: 'https://ln1778.github.io/dapp-sdk-doc/index.html' } });

    native API

    alert

    显示一个 native 的警告框,类似 window.alert

    type alert = (content: string) => void

    示例
    window.hwaToken.apis.native.alert("winner winner, chicken dinner!");

    toast

    显示一个 native 的 toast,用来展示信息

    type toastProps={ type:string, message:string, duration:number }

    type toast = (params: toastProps) => void

    ParamsTypeRequired Default Description
    messagestringtruenull信息内容
    typestringfalsenull信息类型['info', 'warnning', 'success']
    durationnumberfalse1500显示时长
    示例
    window.hwaToken.apis.native.toast({type:"info",message:"消息内容",duration:"2000"});

    confirm

    打开一个确认会话窗口。

    用户点击确认或取消后会返回布尔值。

    type confirm = (content: { title: string message: string cancelText: string confirmText: string }) => Promise<boolean>

    ParamsTypeRequired Default Description
    titlestringfalsenulldialog title
    messagestringtruenulldialog content
    cancelTextstringfalse取消cancel button text
    confirmTextstringfalse确定confirm button text
    示例
    window.hwaToken.apis.native.confirm({ title: 'quick question', message: 'is Javascript the worst language?', cancelText: 'no', confirmText: 'yes', }).then((err,result)=>{ console.log("result:",result); });

    selectPicture

    从相册中选择一个图片,或者拍照,返回图片的 base64 数据 (默认是 jpeg)

    type selectPictureProps={ maxWidth:number, maxHeight:number, quality:number }

    type selectPicture=(params:selectPictureProps)=> Promise<object>

    ParamsTypeRequired Default Description
    maxWidthnumberfalsenull返回的内容会被裁剪
    maxHeightnumberfalsenull返回的内容会被裁剪
    qualitynumberfalse1图片质量, 取值 0 到 1
    示例
    window.hwaToken.apis.native.selectPicture({ maxWidth: 400, maxHeight: 200 }).then((err,image)=>{ console.log("image:",image); });

    setLoading

    设置当前 WebView 为 Loading 状态。

    type setLoading = (visible: boolean) => void

    示例
    window.hwaToken.apis.native.setLoading(true);

    share

    分享链接或是图片。

    当分享内容为图片时,需要编码为 base64 格式或者网络图片。

    是否能够成功分享取决于用户的确认与是否允许权限。

    type ShareParamsDefault = { title: string message: string url: string }

    type ShareParamsImage = { title: string image: string }

    type share = (params: ShareParamsDefault & ShareParamsImage) => Promise<boolean>

    示例
    window.hwaToken.apis.native.share({ title: 'Share', message: 'Share this page with others', url: location.href, }).then((err,result)=>{ console.log("result:",result); });

    scanQRCode

    调用摄像头扫描二维码,扫码完成将会返回字符串,失败或取消将抛出错误。

    type scanQRCode =()=>Promise<string>

    示例
    window.hwaToken.apis.native.scanQRCode().then((err,result)=>{ console.log("result:",result); });

    setClipboard

    粘贴字符串。

    type setClipboard = (text: string) => void

    示例
    window.hwaToken.apis.native..setClipboard('address');

    device API

    getCurrentLanguage

    获取当前的语言环境,来源于用户在 HWA 应用内的设定。

    type getCurrentLanguage = () => Promise<string>

    示例
    window.hwaToken.apis.device.getCurrentLanguage().then((err,result)=>{ console.log(result) //output -> 'en-us' });

    getCurrentCurrency

    获取当前的语言环境,来源于用户在 HWA 应用内的设定。

    type getCurrentCurrency = () => Promise<string>

    示例
    window.hwaToken.apis.device.getCurrentCurrency().then((err,result)=>{ console.log(result) //output -> 'CNY' });

    user API

    showAccountSwitch

    展示钱包的切换面板。如果切换成功,新的地址将会被返回。

    type ChainType = | 'ETHEREUM' | 'TRON' | 'HWAN COIN'

    type showAccountSwitch = (chainType: ChainType | null)=> Promise<string>

    示例
    window.hwaToken.apis.user.showAccountSwitch('ETHEREUM').then((err,result)=>{ console.log(result) //output -> '0x...' });

    getAccount

    获取钱包的地址。

    type getAccount = () =>Promise<string>

    示例
    window.hwaToken.apis.user.getAccount().then((err,result)=>{ console.log(result); //output -> '0x...' });

    getBalance

    获取钱包的余额。

    type balanceProps={ coin_code:string, contract:string }

    type getBalance = (null|balanceProps) =>Promise<number>

    示例
    window.hwaToken.apis.user.getBalance().then((err,result)=>{ console.log(result); //output -> '0' });

    getCurrencyPair

    获取钱包币对列表。

    type balanceProps={ coin_code:string, contract:string }

    type getCurrencyPair = () =>Promise<number>

    示例
    window.hwaToken.apis.user.getCurrencyPair().then((err,result)=>{ console.log(result); //output -> '[{coin_code:"HWA",address:"Lnfdsf...dfdf",currency:[]},{coin_code:"ETH",address:"0xdf...dsfdsf",currency:[]}]' });

    layout API

    setOptions

    设置 WebView 的外部框架样式。

    这将同步更新当前 WebView 的布局,并且只能在页面被加载和解析后应用。如果你想在页面加载时对布局进行修改,请参考我们的预加载配置文档。

    type HexColor = "#{string}"

    type WebViewLayoutOptions = { background?: HexColor | Array<HexColor> bodyBackground?: HexColor titleSize?: number statusBarColor?: HexColor }

    type setOptions = (options: WebViewLayoutOptions) => void

    示例
    window.hwaToken.apis.layout.setOptions({ background: '#000000', titleSize: 20, })

    transaction API

    sendHwaPayment

    对hwaToken交易进行签名并发送,会直接返回签名结果,支持 | HWAN COIN | ETHEREUM | TRON

    type transactionProps={ to:string, value:number, memos?:string }

    type contractTransctionProps={ to:string, coin_code:string, contract:string, value:number, memos?:string }

    type signTransaction=(params:transactionProps||contractTransctionProps)=>Promise<object>

    ParamsTypeRequiredDefault Description
    tostringtruenull交易的接收者
    coin_codestringfalsenull交易的合约币种
    contractstringfalsenull交易的合约地址
    valuenumbertruenull交易的金额
    示例
    window.hwaToken.apis.transaction.sendHwaPayment( { to: "0x0fa38abec02bd4dbb87f189df50b674b9db0b468", value: "1250000000000000", }).then((err,result)=>{ console.log(result); //output -> '{}' })
    • 介绍
    • 支持功能
    • Promise
    • 快速开始
    • 安装
    • 引入和使用
    • API参考
      • 通用
      • apis
      • navigator API
      • native API
      • device API
      • user API
      • layout API
      • transaction API