Skip to content

Commit

Permalink
[Examples] Add new examples for AddDepositOfferTx and DepositTx + min…
Browse files Browse the repository at this point in the history
…or refactorings
  • Loading branch information
knikos committed Jun 28, 2023
1 parent 8727866 commit 6d6ecab
Show file tree
Hide file tree
Showing 9 changed files with 450 additions and 4 deletions.
131 changes: 131 additions & 0 deletions examples/platformvm/buildAddDepositOfferTx-Msig.ts
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()
94 changes: 94 additions & 0 deletions examples/platformvm/buildAddDepositOfferTx.ts
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()
1 change: 1 addition & 0 deletions examples/platformvm/buildAddressStateTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const main = async (): Promise<any> => {
)

const unsignedTx: UnsignedTx = await pchain.buildAddressStateTx(
0,
undefined,
pAddressStrings,
pAddressStrings,
Expand Down
103 changes: 103 additions & 0 deletions examples/platformvm/buildDepositTx-v1-MsigOwner.ts
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()
1 change: 1 addition & 0 deletions examples/platformvm/buildDepositTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const main = async (): Promise<any> => {
)

const unsignedTx: UnsignedTx = await pchain.buildDepositTx(
0,
undefined,
pAddressStrings,
pAddressStrings,
Expand Down
1 change: 0 additions & 1 deletion examples/platformvm/getUpgradePhases.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Avalanche } from "caminojs/index"
import { PlatformVMAPI } from "caminojs/apis/platformvm"
import { ExamplesConfig } from "../common/examplesConfig"
import { json } from "stream/consumers"

const config: ExamplesConfig = require("../common/examplesConfig.json")
const avalanche: Avalanche = new Avalanche(
Expand Down
2 changes: 1 addition & 1 deletion src/apis/platformvm/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2554,7 +2554,7 @@ export class PlatformVMAPI extends JRPCAPI {
amountToLock: BN,
changeThreshold: number = 1
): Promise<UnsignedTx> => {
const caller = "buildRegisterNodeTx"
const caller = "buildDepositTx"

const fromSigner = this._parseFromSigner(fromAddresses, caller)

Expand Down
Loading

0 comments on commit 6d6ecab

Please sign in to comment.