|
| 1 | +# @okxweb3/coin-kaia |
| 2 | +kaia SDK is used to interact with the Ethereum blockchain or Evm blockchains, it contains various functions can be used to web3 wallet. |
| 3 | + |
| 4 | +## Installation |
| 5 | + |
| 6 | +### Npm |
| 7 | + |
| 8 | +To obtain the latest version, simply require the project using npm : |
| 9 | + |
| 10 | +```shell |
| 11 | +npm install @okxweb3/coin-kaia |
| 12 | +``` |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +### Generate Private Key |
| 17 | + |
| 18 | +```typescript |
| 19 | +import { EthWallet } from "@okxweb3/coin-kaia" |
| 20 | + |
| 21 | +let wallet = new EthWallet(); |
| 22 | +// get random key |
| 23 | +let randomPrivateKey = await wallet.getRandomPrivateKey(); |
| 24 | + |
| 25 | +// get derived key |
| 26 | +let params = { |
| 27 | + mnemonic: "stool trumpet fame umbrella bench provide battle toward story fruit lock view", |
| 28 | + chainPath: "m/44'/60'/0'/0/1" |
| 29 | +} |
| 30 | +let derivePrivateKey = await wallet.getDerivedPrivateKey(params); |
| 31 | +``` |
| 32 | + |
| 33 | +### Private Key Derivation |
| 34 | + |
| 35 | +```typescript |
| 36 | +import { EthWallet } from "@okxweb3/coin-kaia" |
| 37 | + |
| 38 | +let wallet = new EthWallet(); |
| 39 | +let mnemonic = "stool trumpet fame umbrella bench provide battle toward story fruit lock view" |
| 40 | +let param = { |
| 41 | + mnemonic: mnemonic, |
| 42 | + hdPath: "m/44'/60'/0'/0/0" |
| 43 | +}; |
| 44 | + |
| 45 | +let privateKey = await wallet.getDerivedPrivateKey(param) |
| 46 | +``` |
| 47 | + |
| 48 | +### Generate Address |
| 49 | + |
| 50 | +```typescript |
| 51 | +import { EthWallet } from "@okxweb3/coin-kaia" |
| 52 | + |
| 53 | +let wallet = new EthWallet(); |
| 54 | +let params = { |
| 55 | + privateKey: '0x9fe340274262b4a3bec88e107b09f784d5f8c27bfe6ff178019ed25130a750e1' |
| 56 | +} |
| 57 | +let newAddress = await wallet.getNewAddress(params); |
| 58 | +``` |
| 59 | + |
| 60 | +### Verify Address |
| 61 | + |
| 62 | +```typescript |
| 63 | +import { EthWallet } from "@okxweb3/coin-kaia" |
| 64 | + |
| 65 | +let wallet = new EthWallet(); |
| 66 | +let params = { |
| 67 | + address: "0x01560cd3bac62cc6d7e6380600d9317363400896" |
| 68 | +}; |
| 69 | +let valid = await wallet.validAddress(60, params); |
| 70 | +``` |
| 71 | + |
| 72 | +### Sending a Transaction |
| 73 | +Use the signTransaction function to get the signed hex to broadcast |
| 74 | +#### Example |
| 75 | + |
| 76 | +```javascript |
| 77 | +import { EthWallet } from "@okxweb3/coin-kaia" |
| 78 | + |
| 79 | +let wallet = new EthWallet(); |
| 80 | +let signParams = { |
| 81 | + privateKey: '0x49c0722d56d6bac802bdf5c480a17c870d1d18bc4355d8344aa05390eb778280', |
| 82 | + data: { |
| 83 | + to: "0xee7c7f76795cd0cab3885fee6f2c50def89f48a3", |
| 84 | + value: new BigNumber(0), |
| 85 | + nonce: 5, |
| 86 | + gasPrice: new BigNumber(100 * 1000000000), |
| 87 | + gasLimit: new BigNumber(21000), |
| 88 | + chainId: 42 |
| 89 | + } |
| 90 | +} |
| 91 | +let tx = await wallet.signTransaction(60, signParams) |
| 92 | +``` |
| 93 | + |
| 94 | +#### Input Params |
| 95 | + |
| 96 | +```typescript |
| 97 | +export type EthTxParams ={ |
| 98 | + from?: string, |
| 99 | + to?: string, |
| 100 | + value: BigNumber, |
| 101 | + nonce: number, |
| 102 | + |
| 103 | + contractAddress?: string // contract address |
| 104 | + gasPrice?: BigNumber, |
| 105 | + gasLimit: BigNumber, |
| 106 | + |
| 107 | + data?: string; |
| 108 | + chainId?: number; // default: 1 |
| 109 | + |
| 110 | + // Typed-Transaction features |
| 111 | + // null, 1:legacy transaction, 2:eip1559 transaction |
| 112 | + type?: number | null; |
| 113 | + |
| 114 | + // EIP-2930; Type 1 & EIP-1559; Type 2 |
| 115 | + // accessList?: AccessListish; |
| 116 | + |
| 117 | + // EIP-1559; Type 2 |
| 118 | + maxPriorityFeePerGas?: BigNumber; |
| 119 | + maxFeePerGas?: BigNumber; |
| 120 | +} |
| 121 | +``` |
| 122 | +
|
| 123 | +#### Support Transaction Types Params |
| 124 | +
|
| 125 | +##### Native Coin Transfer |
| 126 | +
|
| 127 | +```json |
| 128 | +{ |
| 129 | + "to": "0xee7c7f76795cd0cab3885fee6f2c50def89f48a3", |
| 130 | + "value": "0x1", |
| 131 | + "nonce": "0x1", |
| 132 | + "gasPrice": "0x1", |
| 133 | + "gasLimit": "0x5208", |
| 134 | + "chainId": "0x2a" |
| 135 | +} |
| 136 | +``` |
| 137 | +
|
| 138 | +| Name | Type | Description | |
| 139 | +|:-------------------|:--------------------|:-------------------------------------------------------------------| |
| 140 | +| `gasLimit` | `string` | The transaction's gas limit. | |
| 141 | +| `gasPrice` | `string` | The transaction's gas price. | |
| 142 | +| `nonce` | `string` | The transaction's nonce. | |
| 143 | +| `to` | `string` | The transaction's the address is sent to. | |
| 144 | +| `value` | `string` | The amount of Ether sent. | |
| 145 | +| `chainId` | `string` | The id number of the chain, default value is 1, means eth mainnet. | |
| 146 | +
|
| 147 | +
|
| 148 | +##### Token Transfer |
| 149 | +
|
| 150 | +```json |
| 151 | +{ |
| 152 | + "contractAddress": "0x45Ef35936F0EB8F588Eb9C851C5B1C42B22e61EC", |
| 153 | + "to": "0xee7c7f76795cd0cab3885fee6f2c50def89f48a3", |
| 154 | + "value": "0x1", |
| 155 | + "nonce": "0x8", |
| 156 | + "gasPrice": "0x174876e800", |
| 157 | + "gasLimit": "0x5208", |
| 158 | + "chainId": "0x2a" |
| 159 | +} |
| 160 | +``` |
| 161 | +
|
| 162 | +| Name | Type | Description | |
| 163 | +|:-------------------|:------------|:-------------------------------------------------------------------| |
| 164 | +| `contractAddress` | `string` | The address of a contract. | |
| 165 | +| `gasLimit` | `string` | The transaction's gas limit. | |
| 166 | +| `gasPrice` | `string` | The transaction's gas price. | |
| 167 | +| `nonce` | `string` | The transaction's nonce. | |
| 168 | +| `to` | `string` | The transaction's the address is sent to. | |
| 169 | +| `value` | `string` | The amount of Ether sent. | |
| 170 | +| `chainId` | `string` | The id number of the chain, default value is 1, means eth mainnet. | |
| 171 | +
|
| 172 | +##### Data |
| 173 | +
|
| 174 | +```json |
| 175 | +{ |
| 176 | + "to": "0xee7c7f76795cd0cab3885fee6f2c50def89f48a3", |
| 177 | + "value": "0x0", |
| 178 | + "nonce": "0x5", |
| 179 | + "gasPrice": "0x174876e800", |
| 180 | + "gasLimit": "0x5208", |
| 181 | + "chainId": "0x2a", |
| 182 | + "data": "0xa9059cbb000000000000000000000000ee7c7f76795cd0cab3885fee6f2c50def89f48a30000000000000000000000000000000000000000000000000000000000002710" |
| 183 | +} |
| 184 | +``` |
| 185 | +
|
| 186 | +| Name | Type | Description | |
| 187 | +|:------------------|:---------------------|:-------------------------------------------------------------------| |
| 188 | +| `gasLimit` | `string` | The transaction's gas limit. | |
| 189 | +| `gasPrice` | `string` | The transaction's gas price. | |
| 190 | +| `nonce` | `string` | The transaction's nonce. | |
| 191 | +| `to` | `string` | The transaction's the address is sent to. | |
| 192 | +| `value` | `string` | The amount of Ether sent. | |
| 193 | +| `chainId` | `string` | The id number of the chain, default value is 1, means eth mainnet. | |
| 194 | +| `data` | `string` | The data of contract interface. | |
| 195 | +
|
| 196 | +
|
| 197 | +##### EIP-1559 Transaction |
| 198 | +
|
| 199 | +```json |
| 200 | +{ |
| 201 | + "gasPrice": "0xa5c681d00", |
| 202 | + "gasLimit": "0xa410", |
| 203 | + "to": "0x35b2438d33c7dc449ae9ffbda14f56dc39a4c6b8", |
| 204 | + "value": "0xde0b6b3a7640000", |
| 205 | + "nonce": "0xb", |
| 206 | + "maxFeePerGas": "0x826299e00", |
| 207 | + "maxPriorityFeePerGas": "0x77359400", |
| 208 | + "chainId": "0x1", |
| 209 | + "type": 2 |
| 210 | +} |
| 211 | +``` |
| 212 | +
|
| 213 | +| Name | Type | Description | |
| 214 | +|:-----------------------|:---------|:-------------------------------------------------------------------| |
| 215 | +| `gasLimit` | `string` | The transaction's gas limit. | |
| 216 | +| `gasPrice` | `string` | The transaction's gas price. | |
| 217 | +| `nonce` | `string` | The transaction's nonce. | |
| 218 | +| `to` | `string` | The transaction's the address is sent to. | |
| 219 | +| `value` | `string` | The amount of Ether sent. | |
| 220 | +| `chainId` | `string` | The id number of the chain, default value is 1, means eth mainnet. | |
| 221 | +| `data` | `string` | The data of contract interface. | |
| 222 | +| `maxFeePerGas` | `string` | The transaction's maxFeePerGas. | |
| 223 | +| `maxPriorityFeePerGas` | `string` | The transaction's maxPriorityFeePerGas, means miner tips. | |
| 224 | +| `type` | `number` | must be 2, means eip1559 transaction fee | |
| 225 | +
|
| 226 | +
|
| 227 | +#### Sign Message |
| 228 | +
|
| 229 | +**Different transaction types enum values** |
| 230 | +```typescript |
| 231 | +enum MessageTypes { |
| 232 | + ETH_SIGN = 0, |
| 233 | + PERSONAL_SIGN = 1, |
| 234 | + TYPE_DATA_V1 = 2, |
| 235 | + TYPE_DATA_V3 = 3, |
| 236 | + TYPE_DATA_V4 = 4, |
| 237 | +} |
| 238 | +``` |
| 239 | + |
| 240 | +##### ETH_SIGN |
| 241 | +```typescript |
| 242 | +let signParams: SignTxParams = { |
| 243 | + privateKey: privateKey, |
| 244 | + data: { |
| 245 | + type: MessageTypes.ETH_SIGN, |
| 246 | + message: Buffer.from(ethUtil.stripHexPrefix("0x879a053d4800c6354e76c7985a865d2922c82fb5b3f4577b2fe08b998954f2e0"), "hex") |
| 247 | + } |
| 248 | +} |
| 249 | +``` |
| 250 | + |
| 251 | +##### PERSONAL_SIGN |
| 252 | +```typescript |
| 253 | +let signParams: SignTxParams = { |
| 254 | + privateKey: privateKey, |
| 255 | + data: { |
| 256 | + type: MessageTypes.PERSONAL_SIGN, |
| 257 | + message: "Example `personal_sign` message" |
| 258 | + } |
| 259 | +}; |
| 260 | +``` |
| 261 | + |
| 262 | +##### TYPE_DATA_V1 |
| 263 | +```typescript |
| 264 | +let param = { |
| 265 | + privateKey: '0x49c0722d56d6bac802bdf5c480a17c870d1d18bc4355d8344aa05390eb778280', |
| 266 | + data: { |
| 267 | + type: 2, |
| 268 | + message: '[{"type":"string","name":"Message","value":"Hi, Alice!"},{"type":"uint32","name":"A number","value":"1337"}]' |
| 269 | + } |
| 270 | +} |
| 271 | +``` |
| 272 | + |
| 273 | +##### TYPE_DATA_V3 |
| 274 | +```typescript |
| 275 | +let param = { |
| 276 | + privateKey: '0x49c0722d56d6bac802bdf5c480a17c870d1d18bc4355d8344aa05390eb778280', |
| 277 | + data: { |
| 278 | + type: 3, |
| 279 | + message: '{"types":{"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}],"Person":[{"name":"name","type":"string"},{"name":"wallet","type":"address"}],"Mail":[{"name":"from","type":"Person"},{"name":"to","type":"Person"},{"name":"contents","type":"string"}]},"primaryType":"Mail","domain":{"name":"Ether Mail","version":"1","chainId":42,"verifyingContract":"0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"},"message":{"from":{"name":"Cow","wallet":"0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"},"to":{"name":"Bob","wallet":"0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"},"contents":"Hello, Bob!"}}' |
| 280 | + } |
| 281 | +} |
| 282 | +``` |
| 283 | + |
| 284 | +##### TYPE_DATA_V4 |
| 285 | +```typescript |
| 286 | +let param = { |
| 287 | + privateKey: '0x49c0722d56d6bac802bdf5c480a17c870d1d18bc4355d8344aa05390eb778280', |
| 288 | + data: { |
| 289 | + type: 4, |
| 290 | + message: '{"domain":{"chainId":"42","name":"Ether Mail","verifyingContract":"0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC","version":"1"},"message":{"contents":"Hello, Bob!","from":{"name":"Cow","wallets":["0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826","0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF"]},"to":[{"name":"Bob","wallets":["0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB","0xB0BdaBea57B0BDABeA57b0bdABEA57b0BDabEa57","0xB0B0b0b0b0b0B000000000000000000000000000"]}]},"primaryType":"Mail","types":{"EIP712Domain":[{"name":"name","type":"string"},{"name":"version","type":"string"},{"name":"chainId","type":"uint256"},{"name":"verifyingContract","type":"address"}],"Group":[{"name":"name","type":"string"},{"name":"members","type":"Person[]"}],"Mail":[{"name":"from","type":"Person"},{"name":"to","type":"Person[]"},{"name":"contents","type":"string"}],"Person":[{"name":"name","type":"string"},{"name":"wallets","type":"address[]"}]}}' |
| 291 | + } |
| 292 | +} |
| 293 | +``` |
| 294 | + |
| 295 | +#### Signing with a hardware wallet |
| 296 | + |
| 297 | +##### Build raw transaction |
| 298 | +```typescript |
| 299 | +import { EthWallet } from "@okxweb3/coin-kaia" |
| 300 | + |
| 301 | +let wallet = new EthWallet(); |
| 302 | +let ethTxParams = { |
| 303 | + to: "0xee7c7f76795cd0cab3885fee6f2c50def89f48a3", |
| 304 | + value: 1, |
| 305 | + nonce: 5, |
| 306 | + gasPrice: "100000000000", |
| 307 | + gasLimit: 21000, |
| 308 | + chainId: 42, |
| 309 | +}; |
| 310 | + |
| 311 | +let signParams = { |
| 312 | + data: ethTxParams |
| 313 | +}; |
| 314 | +const rawTx = await wallet.getHardWareRawTransaction(signParams); |
| 315 | +console.info(rawTx); |
| 316 | +``` |
| 317 | + |
| 318 | +**input param** |
| 319 | + |
| 320 | +```typescript |
| 321 | +let param = { |
| 322 | + data: { |
| 323 | + to: "0xee7c7f76795cd0cab3885fee6f2c50def89f48a3", |
| 324 | + value: 1, |
| 325 | + nonce: 5, |
| 326 | + gasPrice: "100000000000", |
| 327 | + gasLimit: 21000, |
| 328 | + chainId: 42, |
| 329 | + } |
| 330 | +} |
| 331 | +``` |
| 332 | + |
| 333 | + |
| 334 | +##### Build signed transaction |
| 335 | + |
| 336 | +```typescript |
| 337 | +import { EthWallet } from "@okxweb3/coin-kaia" |
| 338 | + |
| 339 | +let wallet = new EthWallet(); |
| 340 | +const hardwareRawTransactionParam = { |
| 341 | + raw: rawTx, |
| 342 | + r: r, |
| 343 | + s: s, |
| 344 | + v: v, |
| 345 | +} |
| 346 | +const signedTx = await wallet.getHardWareSignedTransaction(hardwareRawTransactionParam); |
| 347 | +``` |
| 348 | + |
| 349 | +## License |
| 350 | + |
| 351 | +[MPL-2.0](<https://tldrlegal.com/license/mozilla-public-license-2.0-(mpl-2)>) |
0 commit comments