forked from ava-labs/avalanchejs
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Examples] Add new examples for AddDepositOfferTx and DepositTx + min…
…or refactorings
- Loading branch information
Showing
9 changed files
with
450 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import { Avalanche, BinTools, Buffer } from "caminojs/index" | ||
import { | ||
PlatformVMAPI, | ||
KeyChain, | ||
UnsignedTx, | ||
Tx, | ||
DepositOffer, | ||
OfferFlag, | ||
PlatformVMConstants | ||
} from "caminojs/apis/platformvm" | ||
import { UnixNow, PChainAlias } from "caminojs/utils" | ||
import { ExamplesConfig } from "../common/examplesConfig" | ||
import BN from "bn.js" | ||
import { | ||
MultisigKeyChain, | ||
MultisigKeyPair, | ||
OutputOwners | ||
} from "caminojs/common" | ||
import createHash from "create-hash" | ||
|
||
const config: ExamplesConfig = require("../common/examplesConfig.json") | ||
const avalanche: Avalanche = new Avalanche( | ||
config.host, | ||
config.port, | ||
config.protocol, | ||
config.networkID | ||
) | ||
const bintools = BinTools.getInstance() | ||
const multiSigAliasMember1PrivateKey = | ||
"PrivateKey-2Vtf2ZhTRz6WcVcSH7cS7ghKneZxZ2L5W8assdCcaNDVdpoYfY" // P-kopernikus1jla8ty5c9ud6lsj8s4re2dvzvfxpzrxdcrd8q7 | ||
const msigAlias = "P-kopernikus1fwrv3kj5jqntuucw67lzgu9a9tkqyczxgcvpst" | ||
const depositOwner = "P-kopernikus13kyf72ftu4l77kss7xm0kshm0au29s48zjaygq" | ||
|
||
const depositOwnerPrivateKey = | ||
"PrivateKey-Ge71NJhUY3TjZ9dLohijSnNq46QxobjqxHGMUDAPoVsNFA93w" | ||
let pchain: PlatformVMAPI | ||
let pKeychain: KeyChain | ||
let pAddresses: Buffer[] | ||
let pAddressStrings: string[] | ||
|
||
const InitAvalanche = async () => { | ||
await avalanche.fetchNetworkSettings() | ||
pchain = avalanche.PChain() | ||
pKeychain = pchain.keyChain() | ||
pKeychain.importKey(multiSigAliasMember1PrivateKey) | ||
pKeychain.importKey(depositOwnerPrivateKey) | ||
|
||
pAddresses = pchain.keyChain().getAddresses() | ||
pAddressStrings = pchain.keyChain().getAddressStrings() | ||
} | ||
|
||
const main = async (): Promise<any> => { | ||
await InitAvalanche() | ||
|
||
const msigAliasBuffer = pchain.parseAddress(msigAlias) | ||
const owner = await pchain.getMultisigAlias(msigAlias) | ||
const startTime = UnixNow().add(new BN(600 * 1)) | ||
const endTime: BN = startTime.add(new BN(26300000)) | ||
|
||
const depositOfferMsigCreator = msigAlias // Warning: this address must have the role OFFERS_CREATOR | ||
const depositOfferMsigCreatorAuth: [number, string | Buffer][] = [ | ||
[0, depositOfferMsigCreator] | ||
] | ||
const offer: DepositOffer = { | ||
upgradeVersion: 1, | ||
id: undefined, | ||
interestRateNominator: new BN(1000000000), | ||
start: startTime, | ||
end: endTime, | ||
minAmount: new BN(1000000), // min deposit amount = 1 milliAvax | ||
totalMaxAmount: undefined, | ||
depositedAmount: undefined, | ||
minDuration: 60, | ||
maxDuration: 360, | ||
unlockPeriodDuration: 20, | ||
noRewardsPeriodDuration: 10, | ||
memo: "post-genesis-deposit-offer", | ||
flags: new BN(OfferFlag.NONE), | ||
totalMaxRewardAmount: new BN(1000000), | ||
rewardedAmount: new BN(0), | ||
ownerAddress: depositOwner | ||
} | ||
const unsignedTx: UnsignedTx = await pchain.buildAddDepositOfferTx( | ||
undefined, | ||
[[depositOfferMsigCreator], pAddressStrings], | ||
[depositOfferMsigCreator], | ||
offer, | ||
depositOfferMsigCreator, | ||
depositOfferMsigCreatorAuth, | ||
Buffer.from("new-deposit-offer") | ||
) | ||
|
||
// Create the hash from the tx | ||
const txbuff = unsignedTx.toBuffer() | ||
const msg: Buffer = Buffer.from(createHash("sha256").update(txbuff).digest()) | ||
|
||
// create MSKeychain to create proper sigindx | ||
const msKeyChain = new MultisigKeyChain( | ||
avalanche.getHRP(), | ||
PChainAlias, | ||
msg, | ||
PlatformVMConstants.SECPMULTISIGCREDENTIAL, | ||
unsignedTx.getTransaction().getOutputOwners(), | ||
new Map([ | ||
[ | ||
msigAliasBuffer.toString("hex"), | ||
new OutputOwners( | ||
owner.addresses.map((a) => bintools.parseAddress(a, "P")), | ||
new BN(owner.locktime), | ||
owner.threshold | ||
) | ||
] | ||
]) | ||
) | ||
|
||
for (let address of pAddresses) { | ||
// We need the keychain for signing | ||
const keyPair = pKeychain.getKey(address) | ||
// The signature | ||
const signature = keyPair.sign(msg) | ||
// add the signature | ||
msKeyChain.addKey(new MultisigKeyPair(msKeyChain, address, signature)) | ||
} | ||
|
||
msKeyChain.buildSignatureIndices() | ||
const tx: Tx = unsignedTx.sign(msKeyChain) | ||
const txid: string = await pchain.issueTx(tx) | ||
console.log(`Success! TXID: ${txid}`) | ||
} | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { Avalanche, Buffer } from "caminojs/index" | ||
import { | ||
PlatformVMAPI, | ||
KeyChain, | ||
UnsignedTx, | ||
Tx, | ||
DepositOffer, | ||
OfferFlag | ||
} from "caminojs/apis/platformvm" | ||
import { | ||
PrivateKeyPrefix, | ||
DefaultLocalGenesisPrivateKey, | ||
UnixNow | ||
} from "caminojs/utils" | ||
import { ExamplesConfig } from "../common/examplesConfig" | ||
import BN from "bn.js" | ||
|
||
const config: ExamplesConfig = require("../common/examplesConfig.json") | ||
const avalanche: Avalanche = new Avalanche( | ||
config.host, | ||
config.port, | ||
config.protocol, | ||
config.networkID | ||
) | ||
|
||
/** | ||
* @ignore | ||
*/ | ||
let privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}` | ||
const ownerPrivKey: string = | ||
"PrivateKey-Ge71NJhUY3TjZ9dLohijSnNq46QxobjqxHGMUDAPoVsNFA93w" | ||
let pchain: PlatformVMAPI | ||
let pKeychain: KeyChain | ||
let pAddresses: Buffer[] | ||
let pAddressStrings: string[] | ||
|
||
const InitAvalanche = async () => { | ||
await avalanche.fetchNetworkSettings() | ||
pchain = avalanche.PChain() | ||
pKeychain = pchain.keyChain() | ||
// P-local18jma8ppw3nhx5r4ap8clazz0dps7rv5u9xde7p | ||
pKeychain.importKey(privKey) | ||
// P-kopernikus13kyf72ftu4l77kss7xm0kshm0au29s48zjaygq | ||
pKeychain.importKey(ownerPrivKey) | ||
|
||
pAddresses = pchain.keyChain().getAddresses() | ||
pAddressStrings = pchain.keyChain().getAddressStrings() | ||
} | ||
|
||
const main = async (): Promise<any> => { | ||
await InitAvalanche() | ||
const startTime = UnixNow().add(new BN(600 * 1)) | ||
const endTime: BN = startTime.add(new BN(26300000)) | ||
|
||
const depositOfferCreator = pAddressStrings[0] // Warning: this address must have the role OFFERS_CREATOR | ||
const depositOfferCreatorAuth: [number, string | Buffer][] = [ | ||
[0, depositOfferCreator] | ||
] | ||
const depositOwner = pAddressStrings[1] | ||
const offer: DepositOffer = { | ||
upgradeVersion: 1, | ||
id: undefined, | ||
interestRateNominator: new BN(1000000000), | ||
start: startTime, | ||
end: endTime, | ||
minAmount: new BN(1000000), // min deposit amount = 1 milliAvax | ||
totalMaxAmount: undefined, | ||
depositedAmount: undefined, | ||
minDuration: 60, | ||
maxDuration: 360, | ||
unlockPeriodDuration: 20, | ||
noRewardsPeriodDuration: 10, | ||
memo: "post-genesis-deposit-offer", | ||
flags: new BN(OfferFlag.NONE), | ||
totalMaxRewardAmount: new BN(1000000), | ||
rewardedAmount: new BN(0), | ||
ownerAddress: depositOwner | ||
} | ||
const unsignedTx: UnsignedTx = await pchain.buildAddDepositOfferTx( | ||
undefined, | ||
[depositOfferCreator], | ||
[depositOfferCreator], | ||
offer, | ||
depositOfferCreator, | ||
depositOfferCreatorAuth, | ||
Buffer.from("new-deposit-offer") | ||
) | ||
|
||
const tx: Tx = unsignedTx.sign(pKeychain) | ||
const txid: string = await pchain.issueTx(tx) | ||
console.log(`Success! TXID: ${txid}`) | ||
} | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { Avalanche, BinTools, Buffer } from "caminojs/index" | ||
import { | ||
PlatformVMAPI, | ||
KeyChain, | ||
UnsignedTx, | ||
Tx | ||
} from "caminojs/apis/platformvm" | ||
import { OutputOwners } from "caminojs/common/output" | ||
import { PrivateKeyPrefix, DefaultLocalGenesisPrivateKey } from "caminojs/utils" | ||
import { ExamplesConfig } from "../common/examplesConfig" | ||
import BN from "bn.js" | ||
import createHash from "create-hash" | ||
import { SignerKeyPair } from "caminojs/common" | ||
|
||
const config: ExamplesConfig = require("../common/examplesConfig.json") | ||
const avalanche: Avalanche = new Avalanche( | ||
config.host, | ||
config.port, | ||
config.protocol, | ||
config.networkID | ||
) | ||
|
||
const msigAlias = "P-kopernikus1fwrv3kj5jqntuucw67lzgu9a9tkqyczxgcvpst" | ||
const multiSigAliasMember1PrivateKey = | ||
"PrivateKey-2Vtf2ZhTRz6WcVcSH7cS7ghKneZxZ2L5W8assdCcaNDVdpoYfY" // P-kopernikus1jla8ty5c9ud6lsj8s4re2dvzvfxpzrxdcrd8q7 | ||
|
||
const bintools: BinTools = BinTools.getInstance() | ||
const privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}` | ||
let pchain: PlatformVMAPI | ||
let pKeychain: KeyChain | ||
let pAddressStrings: string[] | ||
|
||
const InitAvalanche = async () => { | ||
await avalanche.fetchNetworkSettings() | ||
pchain = avalanche.PChain() | ||
pKeychain = pchain.keyChain() | ||
// P-local18jma8ppw3nhx5r4ap8clazz0dps7rv5u9xde7p | ||
pKeychain.importKey(privKey) | ||
pKeychain.importKey(multiSigAliasMember1PrivateKey) | ||
pAddressStrings = pchain.keyChain().getAddressStrings() | ||
} | ||
|
||
const main = async (): Promise<any> => { | ||
await InitAvalanche() | ||
const depositOwner = msigAlias | ||
const msigAliasBuffer = pchain.parseAddress(msigAlias) | ||
const amountToLock = new BN(10000000) | ||
const depositOfferID = "27pLgKVoXqtSWqGtBTtGkVKmZUM3MYTsjDjNzuLEr9gWiVGXy3" | ||
const depositDuration = 110 | ||
const memo: Buffer = Buffer.from("DepositTx v1 with msig deposit offer owner") | ||
const owners = new OutputOwners( | ||
pchain.keyChain().getAddresses(), | ||
undefined, | ||
1 | ||
) | ||
|
||
const depositOfferCreator = pAddressStrings[0] // Warning: this address must have the role OFFERS_CREATOR | ||
const depositOfferCreatorAuth: [number, string | Buffer][] = [ | ||
[0, depositOfferCreator] | ||
] | ||
|
||
const depositOfferOwnerAuth: [number, string | Buffer][] = [[0, depositOwner]] | ||
|
||
// hash concatenated bytes of offer id and deposit owner address | ||
const msgHashed: Buffer = Buffer.from( | ||
createHash("sha256") | ||
.update( | ||
Buffer.concat([ | ||
bintools.cb58Decode(depositOfferID), | ||
pchain.parseAddress(depositOfferCreator) | ||
]) | ||
) | ||
.digest() | ||
) | ||
const keypair: SignerKeyPair = pKeychain.getKey( | ||
pKeychain.getAddresses()[1] // msig member 1 | ||
) | ||
// sign the hash | ||
const signatureBuffer: Buffer = keypair.sign(msgHashed) | ||
|
||
const unsignedTx: UnsignedTx = await pchain.buildDepositTx( | ||
1, | ||
undefined, | ||
pAddressStrings, | ||
pAddressStrings, | ||
depositOfferID, | ||
depositDuration, | ||
owners, | ||
depositOfferCreator, | ||
depositOfferCreatorAuth, | ||
[signatureBuffer], | ||
depositOfferOwnerAuth, | ||
memo, | ||
new BN(0), | ||
amountToLock | ||
) | ||
|
||
const tx: Tx = unsignedTx.sign(pKeychain) | ||
const txid: string = await pchain.issueTx(tx) | ||
console.log(`Success! TXID: ${txid}`) | ||
} | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.