Skip to content

Library to support mobile wallet and non-Chromium based connection to Zetrix.

Notifications You must be signed in to change notification settings

Zetrix-Chain/zetrix-connect-wallet-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

Zetrix Connect Wallet SDK

First install the npm package:

npm install zetrix-connect-wallet-sdk

The syntax shown below is Javascript ES6, which needs to be translated to run in a Web browser

1. Initialize the SDK

import ZetrixWalletConnect from 'zetrix-connect-wallet-sdk';

const options = {
  bridge: 'wss://test-wscw.Zetrix.com',
  callMode: 'web'
}
const ZetrixWalletConnect = new ZetrixWalletConnect(options)

Initialization parameters:

param type required description
bridge String Yes Bridge service IP for communication between H5 JS and App
qrcode Boolean No Whether to enable the code scan authorization mode (it is recommended to configure this mode on the PC)
callMode String Yes If the passed in parameter 'web' is used in the web page. If the incoming parameter 'webView' is used in webView

2. The connection

After initialization, the JS plug-in is required to establish a connection with the WebSocket service

If authorized in Zetrix Wallet app, the authorized account address in Zetrix Wallet App will be returned

ZetrixWalletConnect.connect().then(res => {
   
}).catch(error => {
   
})

Return Parameter Description:

param type description
code Int Status code
data.address String App authorized account address
message String Return messages
resp:

{
  code: 0,
  data: {
    address: 'ZTX3YgemDoX7JXkqsyeyprQVpoMJHkp5Qu8US'
  },
  message: ''
}

Status Code Description:

code description promise
0 Success Resolve
90001 This plug-in cannot be used because the browser does not support the API syntax used by the plug-in Reject
90002 Browsers do not support Websocket communication and cannot use this plug-in Reject

3. Authorized to connect

By invoking the authorization SDK, Zetrix Wallet App can be notified to perform authorization connection and authorize the current App account. After authorization, the application side can use Zetrix Wallet App's signature, transfer and other functions.

Note: the SDK is used on the PC, and it is recommended to set the initialization parameters qrcode:true. After invoking the authorization SDK, the authorization qrcode will be automatically generated. Zetrix wallet app can scan the code for authorization connection

ZetrixWalletConnect.auth().then(res => {
   
}).catch(error => {
       
})

Return Parameter Description:

param type description
code Int Status code
data.address String App authorized account address
message String Return messages
resp:

{
  code: 0,
  data: {
    address: 'ZTX3YgemDoX7JXkqsyeyprQVpoMJHkp5Qu8US'
  },
  message: ''
}

Status Code Description:

code description promise
0 Success Resolve
1 Cancel Reject

4. The message signature

Zetrix Wallet App can display and sign the content to be signed by calling this method. After signing, the user will return the app signature account public key, App signature account address, signature string, etc.

Note: When the SDK is used on the PC side, the initial parameter is set qrcode:true and authorization is granted, the MESSAGE signature SDK will automatically notify Zetrix Wallet app to perform the signature function

const obj = {
    message: 'Hello world'
}
ZetrixWalletConnect.signMessage(obj).then(res => {
    
}).catch(error => {
   
})

Request param:

param type description
message String Information to be signed provided by the service side

Return Parameter Description:

param type description
code Int Status code
data.address String App authorized account address
data.publicKey String App signed the public key of the account
data.signData String Signature string
message String Return messages
resp:

{
  code: 0,
  data: {
    address: 'ZTX3YgemDoX7JXkqsyeyprQVpoMJHkp5Qu8US',
    publicKey: 'b001226ed6ee3bdae141e5e2686b2034fd7db1d3e4b562a3d315a14af71de6ab7c844ef786b6',
    signData: '2ed1e99b92377fdf9ed8e7d55d7de322cd59d9df91d8e9c4a4f495931ad5d4da14b1c9258e2e0fdc33d77a48921b0a4acf46492cd171495deec30954ac2eef07',
  },
  message: ''
}

Status Code Description:

code description promise
0 Success Resolve
1 Cancel Reject
10011 Unauthorized Reject

5. Blob signature

By calling this method, blob can be passed into the application side and signed by Zetrix Wallet App. After signing, the user will return the app signature account public key, App signature account address, signature string, etc.

Note: PC side using SDK, initial parameter configuration qrcode:true after authorization, call message signature SDK will automatically notify Zetrix Wallet app to perform BLOB signature function (signature only)

const obj = {
    message: '0a255a54583364703475586b32415742576758386648675833353263666b567050366550357835106222b401080712255a54583364703475586b32415742576758386648675833353263666b5670503665503578356288010a255a5458334b5a4d7a4b4451483851525236516d7152346b385a7241366e55693546377a73341a5f7b226d6574686f64223a227472616e73666572222c22706172616d73223a7b22746f223a225a545833545857635879416a4634617959706d6244656165387a414a38726f587a31385941222c2276616c7565223a2231303030303030227d7d30a64a380a'
}
ZetrixWalletConnect.signBlob(obj).then(res => {
    
}).catch(error => {
   
})

Request param:

param type description
message String Information to be signed provided by the service side

Return Parameter Description:

param type description
code Int Status code
data.address String App authorized account address
data.publicKey String App signed the public key of the account
data.signData String Signature string
message String Return messages
resp:

resp:

{
code: 0,
data: {
    address: "ZTX3YgemDoX7JXkqsyeyprQVpoMJHkp5Qu8US"
    publicKey: "b001226ed6ee3bdae141e5e2686b2034fd7db1d3e4b562a3d315a14af71de6ab7c844ef786b6"
    signData: "00bb613675c634f1586fb31274572ed5211972d94ff3fe25f78997a683744957753ebce3d31913d17ea53d9048b0c30daf6a2dba023486a65c1cae2ca5dea40d
},
message: ''
}

Status Code Description:

code description promise
0 Success Resolve
1 Cancel Reject
10011 Unauthorized Reject

6. Obtain account nonce value before transfer

The account Nonce value (account transaction serial number) needs to be obtained before the transfer, and the account continuity needs to be guaranteed, and the nonce value of the account needs to be added by 1 when the new transaction occurs

 const obj = {
    address: 'ZTX3dp4uXk2AWBWgX8fHgX352cfkVpP6eP5x5',
    chainId: '2'
}

// SDK
const nonce = this.ZetrixWalletConnect.getNonce(obj)

// code example
async getNonce () {
    let nonceResult = await this.ZetrixWalletConnect.getNonce(obj)
    const nonce = nonceResult + 1
    return nonce
}

Request param:

param type description
address String Address of the account whose nonce value needs to be queried
chainId String Network environment: 1 Primary network, 2 Test network

Return Parameter Description:

param type description
nonce Int Account transaction serial number

7. Transaction

This method can support transfer of ZTX, ZTP20 and other protocols and creation and invocation of smart contracts. This SDK is usually used for transactions on the Zetrix Wallet app chain, and developers need to assemble their own data structures from the following data templates before invoking the SDK. The confirmation page is for the App account to sign the transaction independently. After signing, the App will broadcast the transaction to the blockchain node, and the App will respond to the submission status. The final status of the transaction requires the developer to confirm the final state of the transaction by calling the transaction query interface after returning the hash value of the transaction.

Note: When the SDK is used on PC side, the initial parameter configuration qrcode:true and authorization is made, the MESSAGE signature SDK will automatically notify Zetrix Wallet App to execute the transaction function

var obj = {
    from:'ZTX3dp4uXk2AWBWgX8fHgX352cfkVpP6eP5x5',
    to:'ZTX3KZMzKDQH8QRR6QmqR4k8ZrA6nUi5F7zs4',
    nonce: 98,
    amount:'1',
    gasFee: '0.01',
    data:'{"method":"transfer","params":{"to":"ZTX3TXWcXyAjF4ayYpmbDeae8zAJ8roXz18YA","value":"1000000"}}',
    chainId:'2'
}
ZetrixWalletConnect.sendTransaction(obj).then(res => {
    
}).catch(error => {
   
})

Request param:

param type description
from String Account address of the party initiating the transaction
to String Target account address
nonce Int Account transaction sequence number. The account must be continuous and the nonce of the account must be added by 1 when a new transaction occurs
amount String ZTX transfer quantity; If to is the smart contract address, the number of transfers supported is 0
gasFee String Transaction prepaid expenses, such as 0.01ZTX pre-paid, the real cost consumed after the transaction is successful will be less than 0.00674 ZTX
data String The contract calls the function, usually a JSON string
chainId String Network environment: 1 Primary network, 2 Test network

Return Parameter Description:

param type description
code Int Status code
data.hash String Trading hash
message String Return messages
resp:

{
  code: 0,
  data: {
    hash: 'f38436d58a1df817bfd6b5e3d1474ca5feced26a8c37beb2591862ba2a01d91d',
  },
  message: ''
}

Status Code Description:

code description promise
0 Success Resolve
1 Cancel Reject

8. Cancel the authorization

Call this method to cancel the authorization status between the application and the Zetrix Wallet app

ZetrixWalletConnect.disconnect()

webview SDK

With zetrix-connect-wallet-sdk, the various capabilities of the Zetrix App can also be accessed and used in the webview environment.

The syntax shown below is Javascript ES6, which needs to be translated to run in a webview

1. To initialize the webview SDK

import ZetrixWalletConnect from 'zetrix-connect-wallet-sdk';
const ZetrixWalletConnect = new ZetrixWalletConnect()

1.1 Customize the App header style

Preload URL Options allows you to define a number of parameters on the URL address, the Zetrix app parses these parameters and prepares all the design styles for you on a native level. Faster, more elegant, easier to configure, less invasive

In fact, the above configuration only requires a string at the end of the URL, for example: https://test-appcredential.Zetrix.com/?navStyle=0&appType=1, no APIs or SDK required.

When Zetrix's WebView reads the query parameters in the URL, it serializes and analyzes them, if the parameters meet the agreed criteria, the WebView pre-configures the layout at the native level according to this configuration.

1.1.1 navStyle

Configure this field to be immersive

0: navigation bar bright color style, the title button is black, and the background is white

1: navigation bar Dark style, the title button is white, and the background is black

f this field is not configured, it is considered non immersive

If it is immersive, as shown in the following figure

1.1.2 appType

Technologies used in app development

0: The old app adopts native development

1: The new app adopts native development

2. The auth

Call the App to perform authorization

ZetrixWalletConnect.auth().then((res) => {
  
}).catch((err) => {
  
})

Return Parameter Description:

param type description
code string Status code
data.address string App authorized account address
data.walletName string App authorized account name
data.chainId string Network environment: 1 Primary network, 2 Test network
{
  code: "0"
  data: {
    "walletName":"",
    "chainId": ""
    "address":""
  }
  message: ""
}

Status Code Description:

code description promise
0 Success Resolve
1 Cancel Reject

3. The message signature

The Zetrix Wallet App method is called in the webview environment to display and sign the content to be signed. After the user signs, the public key of the app signature account, the address of the app signature account, and the signature string will be returned.

const obj = {
  "message":"Hello world",
}
ZetrixWalletConnect.signMessage(obj).then((res) => {
  
}).catch((err) => {
  
})

Request param:

param type description
message string Information to be signed provided by the service side

Return Parameter Description:

param type description
code string Status code
data.address string App authorized account address
data.publicKey string App signed the public key of the account
data.signData string Signature string
message string Return messages
{
  code: "0"
  data: {
    "address":"",
    "publicKey": "",
    "signData": ""
  }
  message: ""
}

Status Code Description:

code description promise
0 Success Resolve
1 Cancel Reject
-1 Error Reject

4. String encryption

Calling this sdk in the webview environment can perform various types of encryption on strings

const obj = {
  "originStr":"Hello world",
  "type":"keyStore",
}
ZetrixWalletConnect.apis.native.clientEncryption(obj).then((res) => {
  
}).catch((err) => {
  
})

Request param:

param type description
originStr String String to be encrypted
type String keyStore / SM2 / ED2519

Return Parameter Description:

param type description
code String Status code
data.resultStr String encrypted string
message String Return messages
{
  code: "0"
  data: {
    resultStr:"eyJhZGRyZXNzljoiliwiYWVzY3RyX2I2ljoiODlkYzBmZWM50 WUzMGRhYjcwOTQ3YTAyNjM0MWUzMDUiLCJzY3J5cHRf cGFyYW1zljp7InNhbHQiOiJmM2YyMmQ3ZWZjZjBhNzM3Y TE2N2YxNTJkYjUwZThmZTM4YzA0Y2NjMzlzMTA4MDImM TliNWFhOTE3ZTM1OTU5liwibil6MTYzODQsInAiOjEsInliOjh9 LCJjeXBoZXJfdGV4dC16ljdiNzA3MmNkZDNjYjZkNGZmNDFi NWIiLCJ2ZXJzaW9uljoyfQ=="
  }
  message: ""
}

Status Code Description:

code description promise
0 Success Resolve
1 Cancel Reject
-1 Error Reject

5. String decryption

Calling this sdk in the webview environment can perform various types of decryption on the encrypted string

const obj = {
  "originStr":"eyJhZGRyZXNzljoiliwiYWVzY3RyX2I2ljoiODlkYzBmZWM50 WUzMGRhYjcwOTQ3YTAyNjM0MWUzMDUiLCJzY3J5cHRf cGFyYW1zljp7InNhbHQiOiJmM2YyMmQ3ZWZjZjBhNzM3Y TE2N2YxNTJkYjUwZThmZTM4YzA0Y2NjMzlzMTA4MDImM TliNWFhOTE3ZTM1OTU5liwibil6MTYzODQsInAiOjEsInliOjh9 LCJjeXBoZXJfdGV4dC16ljdiNzA3MmNkZDNjYjZkNGZmNDFi NWIiLCJ2ZXJzaW9uljoyfQ==",
  "type":"keyStore",
}
ZetrixWalletConnect.apis.native.clientDecrypt(obj).then((res) => {
  
}).catch((err) => {
  
})

Request param:

param type description
originStr String String to be decrypted
type String keyStore / SM2 / ED2519
message String Return messages

Return Parameter Description:

param type description
code String Status code
data.resultStr String decrypted string
{
  code: "0"
  data: {
    "resultStr":"Hello world"
  }
  message: ""
}

Status Code Description:

code description promise
0 Success Resolve
1 Cancel Reject
-1 Error Reject
-2 Decryption error Reject

6. Obtain account nonce value before transfer

The account Nonce value (account transaction serial number) needs to be obtained before the transfer, and the account continuity needs to be guaranteed, and the nonce value of the account needs to be added by 1 when the new transaction occurs

const obj = {
 address: 'ZTX3H37r2RKhqDqH7zjuPuo8Fezjbwi4ZfgWA'
}

getNonce () {
  ZetrixWalletConnect.getNonce(obj).then((res) => {
    const nonce = res + 1
  })
}

Request param:

param type description
address String Address of the account whose nonce value needs to be queried

Return Parameter Description:

param type description
nonce Int Account transaction serial number

7. Transaction

This method can support the transmission of ZTX, ZTP20 and other protocols as well as the creation and invocation of smart contracts. This SDK is usually used to call transactions on the Zetrix Wallet App application chain in the webview environment. Developers need to assemble their own data structures from the following data templates before calling the SDK. The confirmation page is for the App account to independently sign the transaction. After signing, the App broadcasts the transaction to the blockchain nodes, and the App responds with the submission status. The final status of the transaction requires the developer to confirm the final status of the transaction by calling the transaction query interface after returning the hash value of the transaction.

const obj = {
  from:'ZTX3dp4uXk2AWBWgX8fHgX352cfkVpP6eP5x5',
  to:'ZTX3KZMzKDQH8QRR6QmqR4k8ZrA6nUi5F7zs4',
  nonce: 98,
  amount:'1',
  gasFee: '0.01',
  data:'{"method":"transfer","params":{"to":"ZTX3TXWcXyAjF4ayYpmbDeae8zAJ8roXz18YA","value":"1000000"}}',
}
 ZetrixWalletConnect.sendTransaction(obj).then((res) => {
   
 }).catch((err) => {
   
 })

Request param:

param type description
from String Account address of the party initiating the transaction
to String Target account address
nonce Int Account transaction sequence number. The account must be continuous and the nonce of the account must be added by 1 when a new transaction occurs
amount String ZTX transfer quantity; If to is the smart contract address, the number of transfers supported is 0
gasFee String Transaction prepaid expenses, such as 0.01ZTX pre-paid, the real cost consumed after the transaction is successful will be less than 0.00674 ZTX
data String The contract calls the function, usually a JSON string

Return Parameter Description:

param type description
code Int Status code
data.hash String Trading hash
message String Return messages
{
  code: 0,
  data: {
    hash: 'f38436d58a1df817bfd6b5e3d1474ca5feced26a8c37beb2591862ba2a01d91d',
  },
  message: ''
}

Status Code Description:

code description promise
0 Success Resolve
1 Cancel Reject
-1 Error Reject

8. Close webView

Call this sdk to close the webview and return to the App page

ZetrixWalletConnect.apis.native.closeDapp()

9. Get preload parameters

Preload configuration allows the user to define some additional parameters in the URL address to specify the style and frame layout required by the web application. All preload configuration parameters will be returned as an object, such as navigation bar height

ZetrixWalletConnect.apis.native.getPreloadParams().then((res) => {
  console.log(res)
})
param type description
navHeight String navigation bar height

Return Parameter Description:

{
  navHeight: "70"
}

About

Library to support mobile wallet and non-Chromium based connection to Zetrix.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published