[Caver-js] API 참조 문서 - 1

박세연·2020년 11월 11일
0

Klaytn SDK

목록 보기
2/4

1. caver.account

class

계정(Account)

const account = new caver.account(address, accountKey)

계정(Account)는 클레이튼 계정의 AccountKey를 업데이트하는데 필요한 정보를 담은 class이다. caver.account package의 default class이다.

명칭형식설명
address문자열업데이트할 계정 주소
accountKeyobject계정에서 사용할 new accountKey

accountKey 유형에는 AccountKeyLegacy, AccountKeyPublic, AccountKeyFail, AccountKeyWeightedMultiSig, AccountKeyRoleBased 가 있다.

AccountKeyLegacy

const accountKeyLegacy = new caver.account.accountKey.accountKeyLegacy()

AccountKeyPublic

const accountKeyPublic = new caver.account.accountKey.accountKeyPublic(publicKey)

AccountKeyFail

const accountKeyFail = new caver.account.accountKey.accountKeyFail()

AccountKeyWeightedMultiSig

const accountKeyWeightedMultiSig = new caver.account.accountKey.accountKeyWeightedMultiSig(threshold, weightedPublicKeys)
// weightedPublicKeys : WeightedPublicKey의 배열

AccountKeyRoleBased

const accountKeyRoleBased = new caver.account.accountKey.accountKeyRoleBased(accountKeyArray)
// role: AccountKeyLegacy, AccountKeyPublic, AccountKeyFail, AccountKeyWeightedMultiSig.
// 각각의 role에 사용할 accountkey를 정의한 배열

WeightedPublicKey

const weightedPublicKey = new caver.account.accountKey.weightedPublicKey(weight, publicKey)
// 각 public key에 가중치(weight)설정

WeightedMultiSigOptions

const weightedMultiSigOptions = new caver.account.weightedMultiSigOptions(threshold, weights)
// threshold: 임계값
// weights: public key의 가중치의 배열

Public Key 출력 참고용

const Caver = require('caver-js')
const caver = new Caver('https://api.baobab.klaytn.net:8651/')
async function testFunction() {
    //public key 출력하기
    const PubKey = caver.klay.accounts.createAccountForUpdate('0x{address}', '0x{private key}')
    console.log(PubKey)
}
testFunction()

caver.account.create

caver.account.create(address, accountKey [, options])
// return 하는 값(형식)은 계정(Account)

address와 accountkey로 계정 인스턴스를 생성한다.

* accountKey가 PublicKey문자열인 경우 AccountKeyPublic가 AccountKey
* accountKey가 PublicKey문자열의 배열인 경우, AccountKeyWeightedMultisig가 AccountKey인 계정 인스턴스가 생성된다.
  마지막 인자인 options가 정의되지 않으면 default option(threshold:1, weight: 1 for each key)
* accountKey가 역할을 포함하는 계정키라면, 계정 인스턴스는 AccountKeyRoleBased로 생성된다.
  여기서도 마지막 option이 정의되지 않으면 default로 기본키로 역할이 정의된다.

정리하면 accountKey 위치에
1. public key string인 경우
2. public key들의 array인 경우
3. public key와 각각의 role이 포함된 2D array인 경우
//Create an Account instance with a public key string -> Account with AccountKeyPublic
const Caver = require('caver-js')
const caver = new Caver('https://api.baobab.klaytn.net:8651/')

async function testFunction() {
    const account = caver.account.create('0x{address}', '0x{public key}')
    console.log(account)
}

testFunction()

위 예시는 account key가 public key string인 경우로, AccountKeyPublic 인스턴스를 생성한다.

caver.account.createFromRLPEncoding

caver.account.createFromRLPEncoding(address, rlpEncodedKey)
// return 하는 값(형식)은 계정(Account)
// rlpEncodedKey: AccountKey를 RLP인코딩한 문자열

caver.account.createWithAccountKeyLegacy

caver.account.createWithAccountKeyLegacy(address)
// return 하는 값(형식)은 계정(Account)

AccountKeyLegacy가 account key인 계정 인스턴스를 생성한다.

// test.js
const Caver = require('caver-js')
const caver = new Caver('https://api.baobab.klaytn.net:8651/')

async function testFunction() {
    const account = caver.account.createWithAccountKeyLegacy('0xb4c25d57583bc3103d01459105a94d53c6f62393')
    console.log(account)
}

testFunction()

caver.account.createWithAccountKeyPublic

caver.account.createWithAccountKeyPublic(address, publicKey)
// return 하는 값(형식)은 계정(Account)

AccountKeyPublic이 account key인 계정 인스턴스를 생성한다.

// test.js
const Caver = require('caver-js')
const caver = new Caver('https://api.baobab.klaytn.net:8651/')

async function testFunction() {
    const account = caver.account.createWithAccountKeyPublic('0xb4c25d57583bc3103d01459105a94d53c6f62393', '0x02a5fae9355520c10caccbd482e05d216d4b8f170ab6fe7a0c3233974dd3ced0f670ed650c82acbdf1594e1d6bb53706276a1d143414c206d71f6d8d6aa39649')
    console.log(account)
}

testFunction()

caver.account.createWithAccountKeyFail

caver.account.createWithAccountKeyFail(address)
// return 하는 값(형식)은 계정(Account)

AccountKeyFail이 account key인 계정 인스턴스를 생성한다.
(트랜잭션 유효성 검증이 항상 실패하는 account key? 였던거 같은데...)

// test.js
const Caver = require('caver-js')
const caver = new Caver('https://api.baobab.klaytn.net:8651/')

async function testFunction() {
    const account = caver.account.createWithAccountKeyFail('0xb4c25d57583bc3103d01459105a94d53c6f62393')
    console.log(account)
}

testFunction()

caver.account.createWithAccountKeyWeightedMultiSig

caver.account.createWithAccountKeyWeightedMultiSig(address, publicKeyArray [, options])
// publicKeyArray: public key 문자열들을 포함 (multiple)
// options: MultiSigOption을 선택할 수 있음
// return 하는 값(형식)은 계정(Account)

AccountKeyWeightedMultiSig이 account key인 계정 인스턴스를 생성한다.

// test.js
const Caver = require('caver-js')
const caver = new Caver('https://api.baobab.klaytn.net:8651/')

async function testFunction() {
    const options = new caver.account.weightedMultiSigOptions (2, [1, 1, 1])
    const account = caver.account.createWithAccountKeyWeightedMultiSig('0x4889fae0e032022cbcbe3da60bea36dd3cd25b18',
    ['0x03b4a0fc5b28db4cf5f763acbb59f6215c4b68860124fa09dd96d31cf8fbf2901e919bc9677540a20400c4c4d3fc07b3ba5c9eb013e57d8d2dd6dde659faef6f', '0xeb02869e44df206860689f411c41cf5f1dffaa7bef283198379e4cf930e762b021a0dfe3e1b14c56a4f8bead906720161131b307cedd955836e4e11c81e7af95', '0xb9d7cfc837f8c2b6d36a3eaefbe65f78207966e30986fe72513e0e0dd963e27beaf97dcaa70dafd976db262ae5d9334c86b0bd9704fe6cc267d8b42dd4beb2d4'], options)
    console.log(account)
    console.log(account._accountKey)
}

testFunction()

caver.account.createWithAccountKeyRoleBased

caver.account.createWithAccountKeyRoleBased(address, roledBasedPublicKeyArray [, options])
// roledBasedPublicKeyArray: public key의 배열과 각각의 role이 저장된 2차원 배열
// return 하는 값(형식)은 계정(Account)

caver.account.accountKey.decode

caver.account.accountKey.decode(rlpEncodedAccountKey)
// 인자는 accountKey의 RLP-인코딩한 문자열
// return 하는 값(형식)은 AccountKey 인스턴스 : AccountKeyLegacy | AccountKeyPublic | AccountKeyFail | AccountKeyWeightedMultiSig | AccountKeyRoleBased

RLP인코딩한 문자열을 decoding해서 accountkey의 형식을 반환한다.

// test.js
const Caver = require('caver-js')
const caver = new Caver('https://api.baobab.klaytn.net:8651/')

async function testFunction() {
    const accountKey = caver.account.accountKey.decode('0x02a102c10b598a1a3ba252acc21349d61c2fbd9bc8c15c50a5599f420cccc3291f9bf9')
    console.log(accountKey)
}
testFunction()

account.getRLPEncodingAccountKey

account.getRLPEncodingAccountKey()
// AccountKey를 RLP-인코딩한 문자열을 return
// test.js
const Caver = require('caver-js')
const caver = new Caver('https://api.baobab.klaytn.net:8651/')

async function testFunction() {
    const account = caver.account.create('0xb4c25d57583bc3103d01459105a94d53c6f62393', '0x0302a5fae9355520c10caccbd482e05d216d4b8f170ab6fe7a0c3233974dd3ced0')
    console.log(account.getRLPEncodingAccountKey())
}
testFunction()

다시 decode를 수행하면?

profile
안녕하세요

0개의 댓글

관련 채용 정보