0.0.2 • Published 1 year ago

@wepin/api-js v0.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

@wepin/api-js

npm version npm downloads

The Wepin API Library is designed for web environments. This package is not available for other platforms.

To perform functions after logging into Wepin, it must be used in conjunction with the @wepin/login-js module.

⏩ Get App ID and Key

After signing up for Wepin Workspace, go to the development tools menu and enter the information for each app platform to receive your App ID and App Key.

⏩ Installation

To install the Wepin API Library, you can use npm or yarn:

npm install @wepin/api-js

or

yarn add @wepin/api-js

⏩ Import WepinAPI

import { WepinAPI } from '@wepin/api-js'

⏩ Initialization

Create a new instance of WepinAPI and initialize it with your application's ID and key:

const wepinApi = new WepinAPI({
    appId: 'wepinAppId',
    appKey: 'wepinAppKey',
})

await wepinApi.init()

After initialization, you can use the various methods provided by the WepinAPI instance.

⏩ Methods

You can use the following methods after initializing the WepinAPI.

getStatus

await wepinApi.getStatus()

This method returns the current lifecycle state of Wepin.

Parameters

  • void

Returns

  • Promise\ - A promise that resolves to the current lifecycle state of Wepin. The possible states are:
    • 'not_initialized': WepinAPI has not been initialized.
    • 'initializing': WepinAPI is currently initializing.
    • 'initialized': WepinAPI has been initialized.
    • 'before_login': WepinAPI has been initialized, but the user is not logged in.
    • 'login': The user is logged in.
    • 'login_before_register': The user has logged in with email, but is not registered with Wepin.

Example

const status = await wepinApi.getStatus()

getAppInfo

await wepinApi.getAppInfo(withNetwork?)

This method retrieves information about your Wepin application. If the withNetwork parameter is set to true, it will also return network-related information.

Parameters

  • withNetwork \ optional - If true, network-related information is included in the returned data.

Returns

  • Promise\ - A promise that resolves to an object containing information about your Wepin application.

Example

const appInfo = await wepinApi.getAppInfo(true)
  • response
    {
        "id": "abcdefg",
        "iconImage": "app_icon.jpg",
        "logoImage": "app_logo.jpg",
        "color": "111111",
        "state": 1,
        "name": "test app",
        "desc": "test app description",
        "category": "game",
        "createdTime": "2024-01-08T08:10:39.000Z",
        "modifiedTime": "2024-01-08T08:10:39.000Z",
        "assets": [
            {
                "coinId": 1,
                "tokens": [],
                "nftContracts": [],
            },
        ],
        "includeOtherAsset": false,
        "property": {
            "emailVerify": true,
        },
    }

getAppCoins

await wepinApi.getAppCoins(locale?)

This method retrieves a list of coin networks supported by your Wepin application. The coin names are localized based on the provided locale.

Parameters

  • locale \<'ko'|'en'> optional - The locale for the coin names.

Returns

  • Promise\

Example

const coinInfo = await wepinApi.getAppCoins('en')
  • response
{
    "coins": [
        "id": 17,
        "bipPath": "60"
        "chainId": "137"
        "cmkId": 123
        "coinGeckoId": "matic-network"
        "color": "#2BBDF7"
        "coreNetwork": "ETHEREUM"
        "decimals": 18
        "defaultAccountState": 1
        "explorerAddress": "https://polygonscan.com/address/{address}"
        "explorerContract": "https://polygonscan.com/token/{contract}?a={address}"
        "explorerTransaction": "https://polygonscan.com/tx/{txID}"
        "explorerUrl": "https://polygonscan.com"
        "feeType": 1
        "iconUrl": "icon.png"
        "isTestnet": 0
        "name": "Polygon"
        "network": "evmPOLYGON"
        "order": 39
        "state": 1
        "symbol": "MATIC"
        "property": {
            "isAppOnly":false
        }
    ]
}

getAppSupportedNFTs

await wepinApi.getAppSupportedNFTs()

This method retrieves a list of NFTs (Non-Fungible Tokens) supported by your Wepin application.

Parameters

  • void

Returns

  • Promise\ - A promise that resolves to an array of supported NFTs.

Example

const coinInfo = await wepinApi.getAppSupportedNFTs()
  • response
{
    "supportNetworkList": [
        {
            "name": {
                "en": "Ethereum",
                "ko": "이더리움"
            },
            "network": "ETHEREUM"
        },
    ]
}

register

await wepinApi.register(pin)

If the userStatus's loginStatus value is not 'complete' after calling the loginWepin() method from @wepin/login-js, this method needs to be called. It registers the user in Wepin with a wallet pin.

After the sign-up and login are completed, the registration for Wepin service (wallet and account creation) will proceed.

Parameters

  • pin \ - The wallet PIN.

Returns

  • Promise\ - A promise that resolves to an object containing the user's login status and information. The object includes:
    • status \<'success'|'fail'> - The login status.
    • userInfo \ optional - The user's information, including:
      • userId \ - The user's ID.
      • email \ - The user's email.
      • provider \<'google'|'apple'|'naver'|'discord'|'email'|'external_token'> - The login provider.
      • use2FA \ - Whether the user uses two-factor authentication.
    • walletId \ = The user's wallet ID.

Example

const userInfo = await wepinApi.register('your_wallet_pin');

refreshSession

await wepinApi.refreshSession()

This method refreshes the user's session with the Wepin service, ensuring that the session is always active and up-to-date.

Parameters

  • void

Returns

  • Promise \ - A promise that resolves to true if the session has been successfully refreshed.

Example

const isRefreshed = await wepinApi.refreshSession();
if (isRefreshed) {
    console.log('Session refreshed');
} else {
    console.log('Session refresh failed');
}

logout

await wepinApi.logout()

This method logs the user out of the Wepin service, ending their current session.

Parameters

  • void

Returns

  • Promise \ - A promise that resolves when the logout process has been completed.

Example

await wepinApi.logout();
console.log('Logged out');

getAccounts

await wepinApi.getAccounts()
await wepinApi.getAccounts(options)

This method retrieves a list of the user's accounts in the Wepin service. It is recommended to use getAccounts() method without argument to get all user accounts. It can be only usable after widget login.

Parameters
  • options:
    • locale: \<'ko' | 'en'> optional Languages of Network Name
    • networks: \ optional A list of network names to filter the accounts.
      • network \ optional
    • withEoa: \ optional If AA accounts are included, whether to include EOA accounts
    • force: \ optional refresh account list

Returns

  • Promise \<Account[]> - A promise that resolves to an array of the user's accounts.
    • address \
    • network \
    • contract \ optional token contract address.
    • isAA \ optional Whether it is aa account or not

Example

const result = await wepinApi.getAccounts({
  locale='en',
  networks: ['Ethereum'],
  withEoa: true
})
  • response
[
  {
    "address": "0x0000001111112222223333334444445555556666",
    "network": "Ethereum",
  },
  {
    "address": "0x0000001111112222223333334444445555556666",
    "network": "Ethereum",
    "contract": "0x777777888888999999000000111111222222333333",
  },
  {
    "address": "0x4444445555556666000000111111222222333333",
    "network": "Ethereum",
    "isAA": true,
  },
]

getNfts

await wepinApi.getNfts()
await wepinApi.getNfts(options)

This method retrieves a list of available NFTs (Non-Fungible Tokens). It can be used after logging in. If the networks parameter is not provided, it returns all NFTs.

Parameters
  • options \ optional

    • network \ optional - Specify the network to filter the NFTs.
    • force \ optional - Set to true to refresh the NFT list.

Returns

  • Promise \<NFTInfo[]> - A promise that resolves to an array of NFTs. Each NFT is represented as an object with the following properties:
    • address \ - The address of the NFT.
    • network \ - The network of the NFT.
    • contract \ - The contract information of the NFT.
      • address \
      • scheme \<"ERC721" | "ERC1155">
    • name \ - The name of the NFT.
    • description \ - The description of the NFT.
    • externalLink \ - The NFT meta info url
    • imageUrl \ optional - The NFT Image or thumbnail url
    • contentUrl \ optional - The NFT Content url (if video)
    • quantity \ optional if ERC1155
    • contentType \<'image'|'video'> - The content type of the NFT

Example

const result = await wepinApi.getNfts()
  • response
[
  {
    "address": "0x0000001111112222223333334444445555556666",
    "network": "Ethereum",
    "contract": {
        "address": "0x0000001111112222223333334444445555556666",
        "scheme": "ERC721",
    },
    "name": "test nft",
    "description": "test nft",
    "externalLink": "https://externalLink.com",
    "imageUrl": "nft.jpg",
    "contentType": "image"
    }
]

getBalance

await wepinApi.getBalance([account])
await wepinApi.getBalance()

It returns the account's balance information. It can be only usable after widget login. It use getBalance() method without argument to get all user accounts.

Parameters

  • account \<Account[]> optional

    • network \
    • address \

Returns

  • Promise \<AccountBalanceInfo[]>
    • symbol \ - symbol of account
    • balance \ - balance of account
    • tokens \<Array\> - token balance information for account
      • symbol \ - token symbol
      • balance \ - token balance
      • contract \ - token contract address

Example

const result = await wepinApi.getBalance([{
  address: '0x0000001111112222223333334444445555556666',
  network: 'Ethereum',
}])
  • response
[
    {
        "symbol": "ETH",
            "balance": "1.1",
        "tokens":[
            {
                "contract": "0x123...213",
                "symbol": "TEST",
                "balance": "10"
            },
        ]
    }
]

checkAddressValidation

await wepinApi.checkAddressValidation({account, address})

The network corresponding to that account checks the validity of the address and returns the result.

Parameters

  • account \
    • network \
    • address \
  • address \

Returns

  • Promise \

Example

const result = await wepinApi.checkAddressValidation([{
  address: '0x0000001111112222223333334444445555556666',
  network: 'Ethereum',
}], '0x9999991111112222223333334444445555556666')

prepareTransaction

await wepinApi.prepareTransaction({account, to, amount, data?})

Inquire the necessary information before sending the coin

Parameters

  • account \
    • network \
    • address \
  • to \
  • amount \
  • data \ optional

Returns

  • Promise \
    • gasLimit \
    • gasPrice \
      • high \
      • medium \
      • low \
    • nonce \

Example

const result = await wepinApi.prepareTransaction({
    account: {
        address: '0x0000001111112222223333334444445555556666',
        network: 'Ethereum',
    },
    to: '0x9999991111112222223333334444445555556666',
    amount: '0.1'
})
  • response
[
    {
        "gasLimit": 210000,
        "gasPrice": {
            "high": 50,
            "medium": 50,
            "low": 50
        },
        "nonce": 8
    }
]

signAndBroadcast

await wepinApi.signAndBroadcast({account, txData, pin, otpCode?})

Sign the data and broadcast to the network. We recommend performing prepareTransaction before performing this method. Using the response value of the prepareTransaction method, it is possible to set the nonce, gasLimit, and gasPrice values of txData.

Parameters

  • account \
    • network \
    • address \
  • txData \
    • to \
    • amount \
    • nonce \
    • data \ optional
    • gasLimit \
    • gasPrice \
  • pin \
  • otpCode \ optional Required if userInfo's Use2FA value is true

    • code \
    • recovery \

Returns

  • Promise \
    • txId \

Example

const result = await wepinApi.signAndBroadcast({
    account: {
        address: '0x0000001111112222223333334444445555556666',
        network: 'Ethereum',
    },
    txData: {
        to: '0x9999991111112222223333334444445555556666',
        amount: '0.1',
        nonce: 8,
        gasLimit: '200000',
        gasPrice: '50000000000',
    },
    pin: '123456',
})
  • response
{
    "txId": "0x76bafd4b700ed959999d08ab76f95d7b6ab2249c0446921c62a6336a70b84f32"
}

sign

await wepinApi.sign({account, signData, pin, otpCode?})

Sign transaction data, messages, and type data.

If the type value is transaction, it is recommended to perform prepareTransaction before performing the method. Using the response value of the prepareTransaction method, it is possible to set the nonce, gasLimit, and gasPrice values of txData.

Parameters

  • account \
    • network \
    • address \
  • signData \
    • type \<'msg_sign'|'sign_data'|'transaction'>
    • txData \<EthSignMessageParams|EthSignDataParams|EthSignTransactionParams>
  • pin \
  • otpCode \ optional Required if userInfo's Use2FA value is true

    • code \
    • recovery \

Returns

  • Promise \
    • txId \

Example

// msg_sign
const result = await wepinApi.sign({
    account: {
        address: '0x0000001111112222223333334444445555556666',
        network: 'Ethereum',
    },
    signData: {
        type: 'msg_sign'
        txData: {
            data: 'test message sign',
        },
    }
    pin: '123456',
})
//sign_data
const result = await wepinApi.sign({
    account: {
        address: '0x0000001111112222223333334444445555556666',
        network: 'Ethereum',
    },
    signData: {
        type: 'sign_data'
        txData: {
            version: 'V1',
            data: {
                {
                    type: 'string',
                    name: 'Message',
                    value: 'Hi, Alice!',
                },
                {
                    type: 'uint32',
                    name: 'A number',
                    value: '1337',
                },
            }
        },
    }
    pin: '123456',
})
//transaction
const result = await wepinApi.sign({
    account: {
        address: '0x0000001111112222223333334444445555556666',
        network: 'Ethereum',
    },
    signData: {
        type: 'transaction'
        txData: {
            to: '0x9999991111112222223333334444445555556666',
            amount: '0.1',
            nonce: 8,
            gasLimit: '200000',
            gasPrice: '50000000000',
        },
    }
    pin: '123456',
})
  • response
// msg_sign / sign_data
{
    "signatureResult": "0x76bafd4b700ed959999d08ab76f95d7b6ab2249c0446921c62a6336a70b84f32"
}
// transaction
{
    "signatureResult": {
        "signedTx": "0xf8ac08850ba43b740083030d409452cfda3e278837d852c4315586c9464be762647e80b844a9059cbb000000000000000000000000262bea79bf3ca9288d7ddb3f4cc3ce2b4dd11e3000000000000000000000000000000000000000000000000000005af3107a4000824055a0926239443fa235b0ac0fa1826dc4946041658f7dc6c749056d4f5edf7ad445eca03aa41cac3e742d732af2106234e1d407eb295e06ca4cd970b83986278e9d4789",
        "sign": {
            "v": "0x4055",
            "r": "0x926239443fa235b0ac0fa1826dc4946041658f7dc6c749056d4f5edf7ad445ec",
            "s": "0x3aa41cac3e742d732af2106234e1d407eb295e06ca4cd970b83986278e9d4789"
        }
    },
}

resetPINRetryCount

await wepinApi.resetPINRetryCount()

Request to initialize the PIN try count value of the user's wallet.

Parameters

  • void

Returns

  • Promise \
    • status \<'success'|'fail'>
    • walletId \
    • maxTryCnt \
    • remainPinTryCnt \
    • recvResetCmd \
    • lockTime \
    • releaseTimestamp \

Example

const result = await wepinApi.resetPINRetryCount()
  • response
{
    "status": "success",
    "walletId": "232FDE8D...",
    "maxTryCnt": 10,
    "remainPinTryCnt": 0,
    "recvResetCmd": true,
    "lockTime": "1440", // 1440 minutes = 24 hours
    "releaseTimestamp": "2023-04-10T09:22:55.900Z" 
}

verifyPIN

await wepinApi.verifyPIN(pin?)

Verifies the PIN of the user's wallet.

Parameters

  • pin \

Returns

  • Promise \

Example

const result = await wepinApi.verifyPIN('123456')

finalize

wepinApi.finalize()

The finalize() method finalizes the Wepin API.

Parameters

  • void

Returns

  • void

Example

wepinApi.finalize()