Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add dec2uint8array
  • Loading branch information
wk3368 committed Oct 27, 2021
1 parent 814857c commit 952ce1f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
11 changes: 11 additions & 0 deletions src/utils/helper.ts
Expand Up @@ -10,6 +10,17 @@ export function bin2dec(bin: string): number {
return Number.parseInt(Number.parseInt(bin, 2).toString(10), 10);
}

// the 3rd and 31st positions are set.
// 268435457
// 0b00010000000000000000000000000001
// [0b0001_0000, 0b0000_0000, 0b0000_0000, 0b0000_0001]
// Uint8Array(4)[ 16, 0, 0, 1 ]
export function dec2uint8array(n: number): Uint8Array {
const arr = dec2bin(n).match(/.{1,8}/g);
const bitmap = Uint8Array.from(arr.map((str) => bin2dec(str)))
return bitmap
}

// index from left to right
export function setBit(n: number, idx: number): number {
if (idx > 31 || idx < 0) {
Expand Down
33 changes: 20 additions & 13 deletions src/utils/multi-sign.spec.ts
Expand Up @@ -15,7 +15,7 @@ import {
encodeScriptFunctionByResolve
} from "./tx";
import { createMultiEd25519KeyShard, signMultiEd25519KeyShard } from "./multi-sign";
import { dec2bin, bin2dec, setBit, isSetBit } from "./helper";
import { dec2bin, bin2dec, setBit, isSetBit, dec2uint8array } from "./helper";
import { showMultiEd25519Account } from "./account";
import * as starcoin_types from "../lib/runtime/starcoin_types";

Expand All @@ -24,23 +24,24 @@ test('2-3 multi sign', async () => {
const thresHold = 2;

const alice = {
'account': '0xd597bcfa4d3464b98bea990ce21aca06',
'address': '0xd597bcfa4d3464b98bea990ce21aca06',
'public_key': '0x547c6a1ef36e9e99865ce7ac028ee79aff404d279b568272bc7154802d4856bb',
'private_key': '0xa9e47d270d2ce33b1475f500f3b9a773eb966f3f8ab5ceb738d52262bbe10cb2'
}

const bob = {
'account': '0xdcd7ae3232acb938c68ee088305b83f6',
'address': '0xdcd7ae3232acb938c68ee088305b83f6',
'public_key': '0xe8cdd5b17a37fe7e8fe446d067e7a9907cf7783aca204ccb623972176614c0a0',
'private_key': '0x7ea63107b0e214789fdb0d6c6e6b0d8f8b8c0be7398654ddd63f3617282be97b'
}

const tom = {
'account': '0x14417edb1fe8c4591d739fee0a91ce42',
'address': '0x14417edb1fe8c4591d739fee0a91ce42',
'public_key': '0xc95ddc2b2926d1a451ea68fa74274aa04af97d8e2aefccb297e6ef61992d42e8',
'private_key': '0x359059828e89fe42dddd5f9571a0c623b071379fc6287c712649dcc8c77f5eb4'
}

// step 1: 准备3个多签账号
// in alice's terminal
// account import-multisig --pubkey 0xe8cdd5b17a37fe7e8fe446d067e7a9907cf7783aca204ccb623972176614c0a0 --pubkey 0xc95ddc2b2926d1a451ea68fa74274aa04af97d8e2aefccb297e6ef61992d42e8 --prikey 0xa9e47d270d2ce33b1475f500f3b9a773eb966f3f8ab5ceb738d52262bbe10cb2 -t 2
const publicKeys1 = [bob.public_key, tom.public_key];
Expand Down Expand Up @@ -114,14 +115,14 @@ test('2-3 multi sign', async () => {
console.log(error);
}

// 我们来发起一个多签交易:从多签账户往 bob 转账 1 个 STC。
// 1. 在 tom 的 starcoin console 中执行
// step2: 我们来发起一个多签交易:从多签账户往 bob 转账 1 个 STC。
// 2.1 在 tom 的 starcoin console 中执行
// account sign-multisig-txn -s 0xb555d8b06fed69769821e189b5168870 --function 0x1::TransferScripts::peer_to_peer_v2 -t 0x1::STC::STC --arg 0xdcd7ae3232acb938c68ee088305b83f6 --arg 1000000000u128
// 该命令会生成原始交易,并用多签账户(由tom的私钥生成)的私钥签名,生成的 txn 会以文件形式保存在当前目录下,文件名是 txn 的 short hash。

// const senderAddress = multiAccountBob.address
// const senderPrivateKey = multiAccountBob.privateKey
// const receiverAddress = '0xdcd7ae3232acb938c68ee088305b83f6'
// const senderAddress = multiAccountTom.address
// const senderPrivateKey = multiAccountTom.privateKey
// const receiverAddress = bob.address
// const amount = 1000000000
// const functionId = '0x1::TransferScripts::peer_to_peer_v2'
// const typeArgs = ['0x1::STC::STC']
Expand Down Expand Up @@ -171,25 +172,31 @@ test('2-3 multi sign', async () => {
// console.log({ partial_signed_txn })
// console.log(partial_signed_txn.authenticator)

// 2. alice 拿到上述的交易文件后,在自己的 starcoin cosole 中签名
// 2.2 alice 拿到上述的交易文件后,在自己的 starcoin cosole 中签名
// account submit-multisig-txn ~/starcoin/work/starcoin-artifacts/4979854c.multisig-txn
// 该命令会用多签账户(由alice的私钥生成)的私钥签名生成另一个交易文件,该交易同时包含有 tom 和 alice 的签名。
// 返回信息提示用户,该多签交易已经收集到足够多的签名,可以提交到链上执行了。
})

test('bit operator', () => {
const b1 = bin2dec('11111111')
const b2 = 0b11111111
console.log({ b1, b2 })
const test = 268435457
console.log({ test })
const bitmap = dec2bin(test)
const bitmap = dec2uint8array(test)
console.log({ bitmap })
const n = bin2dec(bitmap)
const bin = dec2bin(test)
console.log({ bin })
const n = bin2dec(bin)
console.log({ n })
const t2 = bin2dec('00000000000000000000000000000000') | bin2dec('00000000000000000000000000000001') | bin2dec('00010000000000000000000000000000')
console.log(t2, dec2bin(t2))
const n0 = 0b00000000000000000000000000000000
const n1 = setBit(n0, 3)
const n2 = setBit(n1, 31)
console.log(n0, n2, dec2bin(n2))
console.log(n0, n1, n2, dec2bin(n2))
console.log(dec2uint8array(n0), dec2uint8array(n1), dec2uint8array(n2))
console.log(0b101, typeof 0b101)
console.log(1 << 32 - 3 - 1, dec2bin(1 << 32 - 3 - 1))
console.log(1 << 32 - 31 - 1, dec2bin(1 << 32 - 31 - 1))
Expand Down

0 comments on commit 952ce1f

Please sign in to comment.