|
| 1 | +# Orb VDR |
| 2 | +Orb vdr used to manage DID operation. |
| 3 | + |
| 4 | + |
| 5 | +## New VDR |
| 6 | +``` |
| 7 | +import ( |
| 8 | + "crypto" |
| 9 | + "github.com/hyperledger/aries-framework-go-ext/component/vdr/orb" |
| 10 | +) |
| 11 | +
|
| 12 | +type keyRetrieverImpl struct { |
| 13 | + nextRecoveryPublicKey crypto.PublicKey |
| 14 | + nextUpdatePublicKey crypto.PublicKey |
| 15 | + updateKey crypto.PrivateKey |
| 16 | + recoverKey crypto.PrivateKey |
| 17 | +} |
| 18 | +
|
| 19 | +func (k *keyRetrieverImpl) GetNextRecoveryPublicKey(didID string) (crypto.PublicKey, error) { |
| 20 | + return k.nextRecoveryPublicKey, nil |
| 21 | +} |
| 22 | +
|
| 23 | +func (k *keyRetrieverImpl) GetNextUpdatePublicKey(didID string) (crypto.PublicKey, error) { |
| 24 | + return k.nextUpdatePublicKey, nil |
| 25 | +} |
| 26 | +
|
| 27 | +func (k *keyRetrieverImpl) GetSigningKey(didID string, ot orb.OperationType) (crypto.PrivateKey, error) { |
| 28 | + if ot == orb.Update { |
| 29 | + return k.updateKey, nil |
| 30 | + } |
| 31 | +
|
| 32 | + return k.recoverKey, nil |
| 33 | +} |
| 34 | +
|
| 35 | +
|
| 36 | +keyRetrieverImpl := &keyRetrieverImpl{} |
| 37 | +
|
| 38 | +vdr, err := orb.New(keyRetrieverImpl, orb.WithDomain("https://testnet.devel.trustbloc.dev")) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +## Create DID |
| 45 | +For creating DID use vdr create and pass DID document. To discover orb instance there are two ways explicitly or |
| 46 | +through domain. |
| 47 | + |
| 48 | +``` |
| 49 | +import ( |
| 50 | +"crypto" |
| 51 | +"crypto/ed25519" |
| 52 | +"crypto/rand" |
| 53 | +"fmt" |
| 54 | +
|
| 55 | +ariesdid "github.com/hyperledger/aries-framework-go/pkg/doc/did" |
| 56 | +"github.com/hyperledger/aries-framework-go/pkg/doc/jose" |
| 57 | +vdrapi "github.com/hyperledger/aries-framework-go/pkg/framework/aries/api/vdr" |
| 58 | +
|
| 59 | +"github.com/hyperledger/aries-framework-go-ext/component/vdr/orb" |
| 60 | +) |
| 61 | +
|
| 62 | +recoveryKey, recoveryKeyPrivateKey, err := ed25519.GenerateKey(rand.Reader) |
| 63 | +if err != nil { |
| 64 | + return err |
| 65 | +} |
| 66 | +
|
| 67 | +updateKey, updateKeyPrivateKey, err := ed25519.GenerateKey(rand.Reader) |
| 68 | +if err != nil { |
| 69 | + return err |
| 70 | +} |
| 71 | +
|
| 72 | +didPublicKey, _, err := ed25519.GenerateKey(rand.Reader) |
| 73 | +if err != nil { |
| 74 | + return err |
| 75 | +} |
| 76 | +
|
| 77 | +jwk, err := jose.JWKFromKey(didPublicKey) |
| 78 | +if err != nil { |
| 79 | + return err |
| 80 | +} |
| 81 | +
|
| 82 | +vm,err:=ariesdid.NewVerificationMethodFromJWK("key1", "Ed25519VerificationKey2018", "", jwk) |
| 83 | +if err != nil { |
| 84 | + return err |
| 85 | +} |
| 86 | +
|
| 87 | +didDoc := &ariesdid.Doc{} |
| 88 | +
|
| 89 | +// add did keys |
| 90 | +didDoc.Authentication = append(didDoc.Authentication, *ariesdid.NewReferencedVerification(vm, |
| 91 | + ariesdid.Authentication)) |
| 92 | +
|
| 93 | +// add did services |
| 94 | +didDoc.Service = []ariesdid.Service{{ID: "svc1", Type: "type", ServiceEndpoint: "http://www.example.com/"}} |
| 95 | +
|
| 96 | +// create did |
| 97 | +createdDocResolution, err := vdr.Create(didDoc, |
| 98 | + vdrapi.WithOption(orb.RecoveryPublicKeyOpt, recoveryKey), |
| 99 | + vdrapi.WithOption(orb.UpdatePublicKeyOpt, updateKey), |
| 100 | + // No need to use this option because we already use domain |
| 101 | + // vdrapi.WithOption(orb.OperationEndpointsOpt, []string{"https://orb-1.devel.trustbloc.dev/sidetree/v1/operations"}), |
| 102 | + vdrapi.WithOption(orb.AnchorOriginOpt, "https://orb-2.devel.trustbloc.dev/services/orb")) |
| 103 | +if err != nil { |
| 104 | + return err |
| 105 | +} |
| 106 | +
|
| 107 | +fmt.Println(createdDocResolution.DIDDocument.ID) |
| 108 | +
|
| 109 | +// recovery private key be will used to sign next recovery request |
| 110 | +keyRetrieverImpl.recoverKey = recoveryKeyPrivateKey |
| 111 | +// update private key will be used to sign next update request |
| 112 | +keyRetrieverImpl.updateKey = updateKeyPrivateKey |
| 113 | +
|
| 114 | +
|
| 115 | +// if you need did to be discoverable just add domain to did URI (did:orb:123 => did:orb:domain.com:123) |
| 116 | +discoverableDID := strings.ReplaceAll(createdDocResolution.DIDDocument.ID, "did:orb", "did:orb:testnet.devel.trustbloc.dev") |
| 117 | +fmt.Println(discoverableDID) |
| 118 | +``` |
| 119 | + |
| 120 | +## Resolve DID |
| 121 | +For resolving DID use vdr read and pass DID URI. To discover orb instance there are two ways explicitly or |
| 122 | +through did URI. |
| 123 | + |
| 124 | +``` |
| 125 | +docResolution, err := vdr.Read(discoverableDID) |
| 126 | +if err != nil { |
| 127 | + return err |
| 128 | +} |
| 129 | +
|
| 130 | +fmt.Println(docResolution.DIDDocument.ID) |
| 131 | +``` |
| 132 | + |
| 133 | +## Update DID |
| 134 | +For updating DID use vdr update and pass DID document. To discover orb instance there are two ways explicitly or |
| 135 | +through domain. |
| 136 | + |
| 137 | +``` |
| 138 | +updateKey, updateKeyPrivateKey, err := ed25519.GenerateKey(rand.Reader) |
| 139 | +if err != nil { |
| 140 | + return err |
| 141 | +} |
| 142 | +
|
| 143 | +// this key will used for next update request |
| 144 | +keyRetrieverImpl.nextUpdatePublicKey = updateKey |
| 145 | +
|
| 146 | +didPublicKey, _, err := ed25519.GenerateKey(rand.Reader) |
| 147 | +if err != nil { |
| 148 | + return err |
| 149 | +} |
| 150 | +
|
| 151 | +jwk, err := jose.JWKFromKey(didPublicKey) |
| 152 | +if err != nil { |
| 153 | + return err |
| 154 | +} |
| 155 | +
|
| 156 | +vm,err:=ariesdid.NewVerificationMethodFromJWK("key1", "Ed25519VerificationKey2018", "", jwk) |
| 157 | +if err != nil { |
| 158 | + return err |
| 159 | +} |
| 160 | +
|
| 161 | +
|
| 162 | +didDoc := &ariesdid.Doc{ID: discoverableDID} |
| 163 | +
|
| 164 | +didDoc.Authentication = append(didDoc.Authentication, *ariesdid.NewReferencedVerification(vm, |
| 165 | + ariesdid.Authentication)) |
| 166 | +
|
| 167 | +didDoc.CapabilityInvocation = append(didDoc.CapabilityInvocation, *ariesdid.NewReferencedVerification(vm, |
| 168 | + ariesdid.CapabilityInvocation)) |
| 169 | +
|
| 170 | +didDoc.Service = []ariesdid.Service{ |
| 171 | + { |
| 172 | + ID: "svc1", |
| 173 | + Type: "typeUpdated", |
| 174 | + ServiceEndpoint: "http://www.example.com/", |
| 175 | + }, |
| 176 | + { |
| 177 | + ID: "svc2", |
| 178 | + Type: "type", |
| 179 | + ServiceEndpoint: "http://www.example.com/", |
| 180 | + }, |
| 181 | +} |
| 182 | +
|
| 183 | +if err := vdr.Update(didDoc); err != nil { |
| 184 | + return err |
| 185 | +} |
| 186 | +
|
| 187 | +// update private key will be used to sign next update request |
| 188 | +keyRetrieverImpl.updateKey = updateKeyPrivateKey |
| 189 | +``` |
| 190 | + |
| 191 | +## Recover DID |
| 192 | +For recovering DID use vdr recover and pass DID document. To discover orb instance there are two ways explicitly or |
| 193 | +through domain. |
| 194 | + |
| 195 | +``` |
| 196 | +recoveryKey, recoveryKeyPrivateKey, err := ed25519.GenerateKey(rand.Reader) |
| 197 | +if err != nil { |
| 198 | + return err |
| 199 | +} |
| 200 | +
|
| 201 | +// this key will used for next recover request |
| 202 | +keyRetriever.nextRecoveryPublicKey = recoveryKey |
| 203 | +
|
| 204 | +didDoc := &ariesdid.Doc{ID: discoverableDID} |
| 205 | +
|
| 206 | +didPublicKey, _, err := ed25519.GenerateKey(rand.Reader) |
| 207 | +if err != nil { |
| 208 | + return err |
| 209 | +} |
| 210 | +
|
| 211 | +jwk, err := jose.JWKFromKey(didPublicKey) |
| 212 | +if err != nil { |
| 213 | + return err |
| 214 | +} |
| 215 | +
|
| 216 | +vm,err:=ariesdid.NewVerificationMethodFromJWK("key1", "Ed25519VerificationKey2018", "", jwk) |
| 217 | +if err != nil { |
| 218 | + return err |
| 219 | +} |
| 220 | +
|
| 221 | +
|
| 222 | +didDoc.CapabilityInvocation = append(didDoc.CapabilityInvocation, *ariesdid.NewReferencedVerification(vm, |
| 223 | + ariesdid.CapabilityDelegation)) |
| 224 | +
|
| 225 | +didDoc.Service = []ariesdid.Service{{ID: "svc1", Type: "type", ServiceEndpoint: "http://www.example.com/"}} |
| 226 | +
|
| 227 | +if err := e.vdr.Update(didDoc, |
| 228 | + vdrapi.WithOption(orb.RecoverOpt, true), |
| 229 | + vdrapi.WithOption(orb.AnchorOriginOpt, "https://orb-2.devel.trustbloc.dev/services/orb")); err != nil { |
| 230 | + return err |
| 231 | +} |
| 232 | +
|
| 233 | +// recover private key will be used to sign next recover request |
| 234 | +keyRetrieverImpl.recoverKey = recoveryKeyPrivateKey |
| 235 | +``` |
| 236 | + |
| 237 | +## Deactivate DID |
| 238 | +For deactivating DID use vdr recover and pass DID URI. To discover orb instance there are two ways explicitly or |
| 239 | +through domain. |
| 240 | + |
| 241 | +``` |
| 242 | +if err:=vdr.Deactivate(discoverableDID);err!=nil{ |
| 243 | + return err |
| 244 | +} |
| 245 | +``` |
0 commit comments