Documentation Index
Fetch the complete documentation index at: https://injectivelabs-mintlify-jp-native-developers-first-half.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Injectiveは、鍵にEthereumのECDSA secp256k1曲線を使用する独自のカスタムAccount型を定義しています。これは完全なBIP44パスのためのEIP84を満たします。Injectiveベースのアカウントのルート HD パスはm/44'/60'/0'/0です。
アドレス変換
@injectivelabs/sdk-tsパッケージのユーティリティ関数を使用して、InjectiveアドレスとEthereumアドレスを簡単に変換できます:
import {
getInjectiveAddress,
getEthereumAddress,
} from "@injectivelabs/sdk-ts/utils";
const injectiveAddress = "inj1...";
const ethereumAddress = "0x..";
console.log(
"Injective address from Ethereum address => ",
getInjectiveAddress(ethereumAddress)
);
console.log(
"Ethereum address from Injective address => ",
getEthereumAddress(injectiveAddress)
);
ウォレットの導出
Injectiveユーティリティクラスを使用する
- 秘密鍵やニーモニックフレーズからInjective Accountを導出する方法のサンプルコード:
import { PrivateKey } from "@injectivelabs/sdk-ts/core/accounts";
const mnemonic =
"indoor dish desk flag debris potato excuse depart ticket judge file exit";
const privateKey =
"afdfd9c3d2095ef696594f6cedcae59e72dcd697e2a7521b1578140422a4f890";
const privateKeyFromMnemonic = PrivateKey.fromMnemonic(mnemonic);
const privateKeyFromHex = PrivateKey.fromPrivateKey(privateKey);
const address =
privateKeyFromMnemonic.toAddress(); /* or privateKeyFromHex.toAddress() */
console.log({
injectiveAddress: address.toBech32(),
ethereumAddress: address.toHex(),
});
- 公開鍵から公開アドレスを導出する方法のサンプルコード:
import { PublicKey } from "@injectivelabs/sdk-ts/core/accounts";
const pubKey = "AuY3ASbyRHfgKNkg7rumWCXzSGCvvgtpR6KKWlpuuQ9Y";
const publicKey = PublicKey.fromBase64(pubKey);
console.log(publicKey.toAddress().toBech32());
- 秘密鍵からアドレスを導出する方法のサンプルコード:
import { PublicKey } from "@injectivelabs/sdk-ts/core/accounts";
const privateKey =
"afdfd9c3d2095ef696594f6cedcae59e72dcd697e2a7521b1578140422a4f890";
const publicKey = PublicKey.fromPrivateKeyHex(privateKey);
const type = "/injective.crypto.v1beta1.ethsecp256k1.PubKey";
console.log(publicKey.toBase64());
Injectiveユーティリティクラスを使用しない
- 秘密鍵やニーモニックフレーズからInjective Accountを導出する方法のサンプルコード:
import { Wallet } from "ethers";
import { Address as EthereumUtilsAddress } from "ethereumjs-util";
const mnemonic =
"indoor dish desk flag debris potato excuse depart ticket judge file exit";
const privateKey =
"afdfd9c3d2095ef696594f6cedcae59e72dcd697e2a7521b1578140422a4f890";
const defaultDerivationPath = "m/44'/60'/0'/0/0";
const defaultBech32Prefix = "inj";
const isPrivateKey: boolean = true; /* just for the example */
const wallet = isPrivateKey
? Wallet.fromMnemonic(mnemonic, defaultDerivationPath)
: new Wallet(privateKey);
const ethereumAddress = wallet.address;
const addressBuffer = EthereumUtilsAddress.fromString(
ethereumAddress.toString()
).toBuffer();
const injectiveAddress = bech32.encode(
defaultBech32Prefix,
bech32.toWords(addressBuffer)
);
import secp256k1 from "secp256k1";
const privateKey =
"afdfd9c3d2095ef696594f6cedcae59e72dcd697e2a7521b1578140422a4f890";
const privateKeyHex = Buffer.from(privateKey.toString(), "hex");
const publicKeyByte = secp256k1.publicKeyCreate(privateKeyHex);
const buf1 = Buffer.from([10]);
const buf2 = Buffer.from([publicKeyByte.length]);
const buf3 = Buffer.from(publicKeyByte);
const publicKey = Buffer.concat([buf1, buf2, buf3]).toString("base64");
const type = "/injective.crypto.v1beta1.ethsecp256k1.PubKey";
CosmosアドレスをInjectiveアドレスに変換する
InjectiveはデフォルトのCosmosとは異なる導出パスを持つため、CosmosのpublicAddressをInjectiveのものに変換するには、アカウントのpublicKeyが必要です。
実行方法の例は以下のとおりです:
import { config } from "dotenv";
import { PublicKey } from "@injectivelabs/sdk-ts/core/accounts";
import { ChainRestAuthApi } from "@injectivelabs/sdk-ts/client/chain";
config();
(async () => {
const chainApi = new ChainRestAuthApi(
"https://rest.cosmos.directory/cosmoshub"
);
const cosmosAddress = "cosmos1..";
const account = await chainApi.fetchCosmosAccount(cosmosAddress);
if (!account.pub_key?.key) {
console.log("No public key found");
return;
}
console.log(
"injectiveAddress",
PublicKey.fromBase64(account.pub_key.key || "")
.toAddress()
.toBech32()
);
})();