-
Notifications
You must be signed in to change notification settings - Fork 13
/
countersign.go
61 lines (47 loc) · 1.63 KB
/
countersign.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2019 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package rpccalls
import (
"encoding/hex"
"golang.org/x/crypto/ed25519"
"github.com/bitmark-inc/bitmarkd/command/bitmark-cli/configuration"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/transactionrecord"
)
// CountersignData - data for a countersignature request
type CountersignData struct {
Transaction string
NewOwner *configuration.Private
}
// Countersign - countersign a transfer
func (client *Client) Countersign(countersignConfig *CountersignData) (interface{}, error) {
b, err := hex.DecodeString(countersignConfig.Transaction)
if nil != err {
return nil, err
}
bCs := append(b, 0x01, 0x00) // one-byte countersignature to allow unpack to succeed
r, _, err := transactionrecord.Packed(bCs).Unpack(client.testnet)
if nil != err {
return nil, err
}
// attach signature
signature := ed25519.Sign(countersignConfig.NewOwner.PrivateKey.PrivateKeyBytes(), b)
switch tx := r.(type) {
case *transactionrecord.BitmarkTransferCountersigned:
tx.Countersignature = signature[:]
return client.CountersignTransfer(tx)
case *transactionrecord.BlockOwnerTransfer:
tx.Countersignature = signature[:]
return client.CountersignBlockTransfer(tx)
case *transactionrecord.ShareGrant:
tx.Countersignature = signature[:]
return client.CountersignGrant(tx)
case *transactionrecord.ShareSwap:
tx.Countersignature = signature[:]
return client.CountersignSwap(tx)
default:
return nil, fault.ErrNotACountersignableRecord
}
}