Solana 方法无法将字符串转换为 Base58 格式
Solana method cannot convert string to Base58 format
我正在使用 @solana/web3.js
并拥有此代码:
const web3 = require("@solana/web3.js");
const clusterApi = process.env.SOLANA_CLUSTER;
module.exports = {
getConfirmedSignaturesForAddress: async address => {
try {
const connection = new web3.Connection(web3.clusterApiUrl(clusterApi), "confirmed");
const result = await connection.getSignaturesForAddress(address, {
limit: 25
});
return {
tx: result,
status: true
};
} catch (e) {
return {
status: false,
error: e.message
};
}
}
}
每次我调用这个函数我都会得到这个错误:
{ status: false, error: 'address.toBase58 is not a function' }
我试图发送已经转换为 Base58 的它,但它不起作用。怎么了?
我就是这样解决这个问题的。一般来说,你需要转换它不只是转换成纯Base58
,而是像这样:
const web3 = require("@solana/web3.js");
const bip39 = require("bip39");
const getKeyFromMemonic = async mnemonic => {
return new Promise((resolve, reject) => {
bip39
.mnemonicToSeed(mnemonic)
.then(buffer => {
const a = new Uint8Array(buffer.toJSON().data.slice(0, 32));
const key = web3.Keypair.fromSeed(a);
resolve(key);
})
.catch(err => reject(err));
});
};
getSignaturesForAddress: async address => {
try {
const key = await getKeyFromMemonic(address);
const connection = new web3.Connection(web3.clusterApiUrl(clusterApi), "confirmed");
const result = await connection.getSignaturesForAddress(key.publicKey);
return {
tx: result,
status: true
};
} catch (e) {
return {
status: false,
error: e.message
};
}
}
我正在使用 @solana/web3.js
并拥有此代码:
const web3 = require("@solana/web3.js");
const clusterApi = process.env.SOLANA_CLUSTER;
module.exports = {
getConfirmedSignaturesForAddress: async address => {
try {
const connection = new web3.Connection(web3.clusterApiUrl(clusterApi), "confirmed");
const result = await connection.getSignaturesForAddress(address, {
limit: 25
});
return {
tx: result,
status: true
};
} catch (e) {
return {
status: false,
error: e.message
};
}
}
}
每次我调用这个函数我都会得到这个错误:
{ status: false, error: 'address.toBase58 is not a function' }
我试图发送已经转换为 Base58 的它,但它不起作用。怎么了?
我就是这样解决这个问题的。一般来说,你需要转换它不只是转换成纯Base58
,而是像这样:
const web3 = require("@solana/web3.js");
const bip39 = require("bip39");
const getKeyFromMemonic = async mnemonic => {
return new Promise((resolve, reject) => {
bip39
.mnemonicToSeed(mnemonic)
.then(buffer => {
const a = new Uint8Array(buffer.toJSON().data.slice(0, 32));
const key = web3.Keypair.fromSeed(a);
resolve(key);
})
.catch(err => reject(err));
});
};
getSignaturesForAddress: async address => {
try {
const key = await getKeyFromMemonic(address);
const connection = new web3.Connection(web3.clusterApiUrl(clusterApi), "confirmed");
const result = await connection.getSignaturesForAddress(key.publicKey);
return {
tx: result,
status: true
};
} catch (e) {
return {
status: false,
error: e.message
};
}
}