stacks.js
stacks.js copied to clipboard
Add `getNetworkFromAddress` method
It might make sense to add network mode that determines how addresses are encoded. This is used in the explorer Like a getNetworkFromAddress? It would result in Testnet for Devnet/Mocknet addresses, since there's no difference. Is that acceptable?
Issue brought up by @friedger in #1470
Hi @janniks! can you explain what needs to be done with an example?
Hi, would you like to contribute? ✨
In short I would:
Add a function networkFromAddress(address: string) to the transactions package (packages/transactions/src/utils.ts) which decodes the given address (using c32addressDecode) and compares the version (of the address) to mainnet vs testnet, then returns a new instance of that network.
Add some simple test cases (to utils.test.ts) which cover an address for each network (devnet/mocknet also just use testnet address versions)
pseudo example
const network = networkFromAddress("SP8931298312");
//^? instance of StacksMainnet()
Although maybe it's better to add other helpers like
isMainnet(address)which could be combined easily. 🤷♂
How can you differentiate between devnet, testnet, etc when they all use the same address
Exactly, that doesn’t work and why I believe some other method might be better suited. 😕 The network would have the correct version, but shouldn’t be used for broadcast etc.
hey @janniks! i have implemented it as something like this:
export const networkFromAddress=(address: string): {network:string,address:string} =>{
try {
const decode=c32addressDecode(address);
if(isTestnetAddress(decode)){
return {
network:"Testnet",
address: address
}
}else{
return{
network:"Testnet",
address: address
}
}
}
catch(e){
return e
}
}
Hi, maybe we should instead add a new method like isMainnet(address: string) which can easily be combined with others -- let's hold off on the actual implementation and gather some feedback? 🤔
e.g. easy use of isMainnet to create new helpers
const networkFromAddress = (address: string) => (isMainnet(address) ? new StacksMainnet() : new StacksTestnet());
Yeah ok works!